赞
踩
编程要求
在Begin-End
区域中定义一个int
类型数组 scores
,录入三个值,91
,88
,60
,最后输出数组中的三个值,效果如图:
- 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 **********/
- }
- }

- 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关:选择题(1)
1、
以下数组声明有误的是(C)
A、int[] num;2、
定义数组如下 String[] s={“ab”,”cd”,”ef”};
运行语句System.out.println(s[3]);
程序运行的结果为(D)
3、
数组初始化有错误的是(ABCD)
A、int[] num={12,53.7,’6’};编程要求
根据提示,在右侧编辑器Begin-End
处补充代码,计算并输出数组的平均值和最大值。
- 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的平均值和最大值
- int a=0;
- double b=0;
- for(int i = 0 ; i< scores.length;i++){
- a=a+scores[i];
- }
- b=(double)a/(scores.length);
- int c=scores[0];
- for(int i = 0 ; i< scores.length;i++){
-
- if(scores[i]>c){
- c=scores[i];
- }
- }
-
-
-
-
-
-
- System.out.println("平均值:" +b );
- System.out.println("最大值:"+c );
- /********** End **********/
- }
- }

编程要求
1.在右侧Begin-End
区域中定义如下二维数组,使用for
循环输出数组中所有的数据:
2.
使用for
循环将上述数组中的数据全部改为:
- package step4;
-
- public class HelloWorld {
- public static void main(String[] args) {
- /********** Begin **********/
- int[][] arr={
- {92,85},
- {91,65},
- {90,33}
- };
- for(int i=0;i<arr.length;i++){
- for(int j=0;j<arr[i].length;j++){
- System.out.println(arr[i][j]);
- }
- }
-
- int [][] scores={{1,2},{1,2},{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]);
- }
- }
-
-
-
-
-
- /********** End **********/
- }
- }

第6关:选择题(2)
1、
声明数组如下: float[][] f=new float[2][3];
那么该数组一共有(C )个元素
2、
以下的程序是否有错(B)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。