// 第一题
static void t1() {
System.out.println("第一题");
// 接收输入
System.out.println("请输入各个值,以英文逗号分隔,回车结束:\r\n");
Scanner scanner = new Scanner(System.in); // 示例:19.8,12.3,45,67.6,23,15.98,2.55,46.5
乱神 String src = scanner.next();
String[] srcArray = src.split(",");
double[] doubleArray = new double[srcArray.length];
// 转成double数组
for (int i = 0; i < srcArray.length; i++) {
高前 doubleArray[i] = Double.parseDouble(srcArray[i]);
}
// 取最大值和最小值
double max = doubleArray[0];
double min = doubleArray[0];
for (double d : doubleArray) {
if (d > max) {
max = d;
}
if (d < min) {
min = d;
}
}
System.out.println("最大值:" + max + ", 最小值:" + min);
}
// 第二题
static void t2() {
System.out.println("第二题");
// 接收输入
System.out.println("请输入一个二维数组,同一行元素之间以英文逗号分割,不同行元素之间以英文分号分割,回车结束:");
Scanner scanner = new Scanner(System.in); // 示例: 1,2,3,4;2,3,4,5;3,4,5,6
String src = scanner.next();
// 给数组赋值
String[] srcArray = src.split(";");
int[][] a = null;
int[][] b = null;
System.out.println("输入数组为:");
for (int i = 0; i < srcArray.length; i++) {
System.out.println("[" + srcArray[i] + "]");
String[] eleArray = srcArray[i].split(",");
for (int j = 0; j < eleArray.length; j++) {
if (i == 0 && j == 0) {
哗念亏 a = new int[srcArray.length][eleArray.length];
b = new int[eleArray.length][srcArray.length];
}
a[i][j] = Integer.parseInt(eleArray[j]);
}
}
// 转置
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
b[j][i] = a[i][j];
}
}
// 输出b
System.out.println("输出数组为:");
for (int i = 0; i < 4; i++) {
System.out.print("[");
for (int j = 0; j < 3; j++) {
if (j == 2)
System.out.print(b[i][j] + "]\n");
else
System.out.print(b[i][j] + ",");
}
}
}
// 第三题
static void t3() {
System.out.println("第三题");
// 接收输入
System.out.println("请输入各个值,以英文逗号分隔,回车结束:\r\n");
Scanner scanner = new Scanner(System.in); // 示例:1,3,4,5,0,0,6,6,0
String src = scanner.next();
System.out.println("输入数组为[" + src + "]");
String[] srcArray = src.split(",");
int[] intArray = new int[0];
// 去掉0
int index = 0;
for (int i = 0; i < srcArray.length; i++) {
int d = Integer.parseInt(srcArray[i]);
if(d != 0) {
int[] tempArray = intArray;
System.out.println(tempArray.length);
intArray = new int[tempArray.length+1];
for(int j=0; j intArray[j] = tempArray[j];
}
intArray[index] = d;
index++;
}
}
//输出
for(int d : intArray) {
System.out.print(d + ",");
}
}
//第四题
static void t4() {
System.out.println("第三题");
// 接收输入
System.out.println("请输入各个成绩,以英文逗号分隔,回车结束:\r\n");
Scanner scanner = new Scanner(System.in); // 示例:60,67,75,58,78,90,80,54,45,38
String src = scanner.next();
String[] srcArray = src.split(",");
int[] intArray = new int[srcArray.length];
//赋值
for(int i=0; i intArray[i] = Integer.parseInt(srcArray[i]);
}
//计算总成绩
int sum = 0;
for(int d : intArray) {
sum += d;
}
//计算平均成绩
int avg = (int)(sum / (srcArray.length));
//输出
System.out.println("平均成绩为:" + avg + "\n高于平均成绩的分数有:");
for(int d : intArray) {
if(d > avg) {
System.out.print(d + " ");
}
}
}