|
|
1.初识数组
|
|
|
package step1;
|
|
|
|
|
|
public class HelloWorld {
|
|
|
public static void main(String[] args) {
|
|
|
/********** Begin **********/
|
|
|
int[] scores = {91,88,60};
|
|
|
|
|
|
System.out.println("数组的第一个值为:"+scores[0]); //在这里输出数组的第一个值
|
|
|
System.out.println("数组的第二个值为:"+scores[1]); //在这里输出数组的第二个值
|
|
|
System.out.println("数组的第三个值为:"+scores[2]); //在这里输出数组的第三个值
|
|
|
/********** End **********/
|
|
|
}
|
|
|
}
|
|
|
2.数组的使用
|
|
|
package step2;
|
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
public class HelloWorld {
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
|
|
|
/********** Begin **********/
|
|
|
//在这里定义一个长度为4的字符串数组,用来存放学生姓名
|
|
|
String[] stuNames = new String[4];
|
|
|
|
|
|
//在这里给stuNames数组赋值 分别为 张三,张无忌,张三丰,张岁山
|
|
|
stuNames[0] = "张三";
|
|
|
stuNames[1] = "张无忌";
|
|
|
stuNames[2] = "张三丰";
|
|
|
stuNames[3] = "张岁山";
|
|
|
|
|
|
|
|
|
//在这里输出stuNames数组中的数据
|
|
|
System.out.println("数组中的第一个数据为:" +stuNames[0] );
|
|
|
System.out.println("数组中的第二个数据为:" + stuNames[1]);
|
|
|
System.out.println("数组中的第三个数据为:" + stuNames[2]);
|
|
|
System.out.println("数组中的第四个数据为:" + stuNames[3]);
|
|
|
|
|
|
|
|
|
int[] scores;
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
//在这里使用Scanner获取系统输入的整数,并用获取到的数据来设置scores数组的长度
|
|
|
int length = sc.nextInt();
|
|
|
scores = new int[length];
|
|
|
/********** End **********/
|
|
|
|
|
|
System.out.println("数组scores的长度为:" + scores.length);
|
|
|
}
|
|
|
}
|
|
|
3.选择题
|
|
|
double[] num=new double[];
|
|
|
程序出错了
|
|
|
ABCD
|
|
|
4.数组练习-平均值和最大值
|
|
|
package step3;
|
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
public class HelloWorld {
|
|
|
public static void main(String[] args) {
|
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
|
|
int[] scores = new int[sc.nextInt()];
|
|
|
|
|
|
//循环给数组赋值
|
|
|
for(int i = 0 ; i< scores.length;i++){
|
|
|
scores[i] = sc.nextInt();
|
|
|
}
|
|
|
/********** Begin **********/
|
|
|
//在这里计算数组scores的平均值和最大值
|
|
|
float sum = 0;
|
|
|
int max = scores[0];
|
|
|
float avg;
|
|
|
for(int i = 0; i < scores.length; i++){
|
|
|
sum = sum + scores[i];
|
|
|
|
|
|
}
|
|
|
for(int i = 1; i < scores.length; i++){
|
|
|
if(scores[i]>scores[i-1]){
|
|
|
max = scores[i];
|
|
|
}else{
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
avg = sum / scores.length;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("平均值:"+avg);
|
|
|
System.out.println("最大值:"+max);
|
|
|
/********** End **********/
|
|
|
}
|
|
|
}
|
|
|
5.二维数组
|
|
|
package step4;
|
|
|
|
|
|
public class HelloWorld {
|
|
|
public static void main(String[] args) {
|
|
|
/********** Begin **********/
|
|
|
int[][] scores = {{92,85},{91,65},{90,33}};
|
|
|
|
|
|
for(int i=0; i<scores.length; i++){ //行循环次数scores.length(每一列的长度)
|
|
|
for(int j=0; j<scores[i].length; j++){ //列循环次数scores[i].length(每一行的长度)
|
|
|
System.out.println(scores[i][j]);
|
|
|
}
|
|
|
//System.out.println();
|
|
|
}
|
|
|
//scores[][] = {{1,2},{1,2},{1,2}}; //是错误的
|
|
|
for(int i=0; i<scores.length; i++){
|
|
|
scores[i][0] = 1;
|
|
|
scores[i][1] = 2;
|
|
|
}
|
|
|
for(int i=0; i<scores.length; i++){ //行循环次数
|
|
|
for(int j=0; j<scores[i].length; j++){ //列循环次数
|
|
|
System.out.println(scores[i][j]);
|
|
|
}
|
|
|
//System.out.println();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/********** End **********/
|
|
|
}
|
|
|
}
|
|
|
6.选择题(2)
|
|
|
6;编译不通过 |