三个案例
1.动态录入往数组里录入n个数字,并用冒泡排序
2.动态输入n个字母,并将输入的字母中带x的置为null,不是x的依次向前
3.动态录入学生成绩并保存到数组中,每录入一个成绩,数组扩容+1
案例一
1.动态录入往数组里录入n个数字,并用冒泡排序
package learnday5;
//动态录入往数组里录入n个数字,并用冒泡排序
import java.util.Arrays;
import java.util.Scanner;
public class Exercise01 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入n个整数");
int num = input.nextInt();
int[] array1 = new int[num];
System.out.println("n的值为:"+num);
for (int i = 0; i < array1.length; i++) {
System.out.println("请输入第" + (1 + i) + "个数字");
int number = input.nextInt();
array1[i] = number;
}
int[] array2 = maoPao(array1);
System.out.println(Arrays.toString(array2));
input.close();
}
public static int[] maoPao(int[] array) {
int c = 0;
for (int i = 0; i < array.length - 1; i++) {
boolean falg = false;//通过falg标识减少没有意义的比较
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
c = array[j];
array[j] = array[j + 1];
array[j + 1] = c;
falg = true;
}
}
if (falg = false) {
break;
}
}
return array;
}
}
案例二
2.动态输入n个字母,并将输入的字母中带x的置为null,不是x的依次向前
package learnday5;
//动态输入n个字母,并将输入的字母中带x的置为null,不是x的依次向前
import java.util.Arrays;
import java.util.Scanner;
public class Exercise0001 {
public static void main(String[] args) {
//删除数组里面所有的a
Scanner input = new Scanner(System.in);
System.out.println("请输入n个字母");
int num = input.nextInt();
String[] strArrays = new String[num];
System.out.println("字母的总个数为:" + num);
for (int i = 0; i < strArrays.length; i++) {
System.out.println("请输入第" + (1 + i) + "个字母");
String number = input.next();
strArrays[i] = number;
}
String[] newStr = new String[num];
int i = 0;
for (int index = 0,j = 0; index < num; index++) {
String s = strArrays[index];
if (!"x".equals(s)) {
newStr[j++] = s;
}
}
strArrays = newStr;
System.out.println("删除后的数组为:"+Arrays.toString(newStr));
}
}
案例三
3.动态录入学生成绩并保存到数组中,每录入一个成绩,数组扩容+1
package learnday5;标签:中带,--,System,---,int,录入,println,array,out From: https://www.cnblogs.com/wang1999an/p/16770500.html
//动态录入学生成绩并保存到数组中,每录入一个成绩,数组扩容+1
import java.util.Arrays;
import java.util.Scanner;
public class Exercise001 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请录入学生成绩");
double[] arrayScore1 = new double[1];
for (int i = 0; i <= arrayScore1.length; i++) {
System.out.println("请录入第" + (i + 1) + "个学生成绩");
double score = input.nextDouble();
arrayScore1[i] = score;
int len = arrayScore1.length;
if (len <= i + 1) {
double[] arrayScore2 = Arrays.copyOf(arrayScore1, len + 1);
arrayScore1 = arrayScore2;
}
System.out.println(Arrays.toString(arrayScore1));
}
input.close();
}
public static double[] scoreLu(double[] array) {
return array;
}
}