第1關(guān):初識數(shù)組
編程要求
在Begin-End
區(qū)域中定義一個int
類型數(shù)組 scores
,錄入三個值,91
,88
,60
,最后輸出數(shù)組中的三個值,效果如圖:
package step1;
public class HelloWorld {
public static void main(String[] args) {
/********** Begin **********/
int[] scores={91,88,60};
System.out.println("數(shù)組的第一個值為:"+scores[0] ); //在這里輸出數(shù)組的第一個值
System.out.println("數(shù)組的第二個值為:" +scores[1] ); //在這里輸出數(shù)組的第二個值
System.out.println("數(shù)組的第三個值為:" +scores[2] ); //在這里輸出數(shù)組的第三個值
/********** End **********/
}
}
第2關(guān):數(shù)組的使用
package step2;
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
/********** Begin **********/
//在這里定義一個長度為4的字符串?dāng)?shù)組,用來存放學(xué)生姓名
String[] stuNames = new String[4];
//在這里給stuNames數(shù)組賦值 分別為 張三,張無忌,張三豐,張歲山
stuNames[0]="張三";
stuNames[1]="張無忌";
stuNames[2]="張三豐";
stuNames[3]="張歲山";
//在這里輸出stuNames數(shù)組中的數(shù)據(jù)
System.out.println("數(shù)組中的第一個數(shù)據(jù)為:" + stuNames[0]);
System.out.println("數(shù)組中的第二個數(shù)據(jù)為:" + stuNames[1]);
System.out.println("數(shù)組中的第三個數(shù)據(jù)為:" +stuNames[2] );
System.out.println("數(shù)組中的第四個數(shù)據(jù)為:" +stuNames[3] );
int[] scores;
Scanner sc = new Scanner(System.in);
//在這里使用Scanner獲取系統(tǒng)輸入的整數(shù),并用獲取到的數(shù)據(jù)來設(shè)置scores數(shù)組的長度
int length = sc.nextInt() ;
scores = new int[length] ;
/********** End **********/
System.out.println("數(shù)組scores的長度為:" + scores.length);
}
}
第3關(guān):選擇題(1)
-
1、
以下數(shù)組聲明有誤的是(C)
A、int[] num;
B、String num[];
C、double[] num=new double[];
D、String num[]=new String[5]; -
2、
定義數(shù)組如下
A、abString[] s={“ab”,”cd”,”ef”};
運(yùn)行語句System.out.println(s[3]);
程序運(yùn)行的結(jié)果為(D)
B、cd
C、ef
D、程序出錯了 -
3、
數(shù)組初始化有錯誤的是(ABCD)
A、int[] num={12,53.7,’6’};
B、String sewd[]=new String[]{12,52,63};
C、char car[]={‘’1,’2’,6’’};
D、double[] dou=new int[10]; -
第4關(guān):數(shù)組練習(xí)-平均值和最大值
編程要求
根據(jù)提示,在右側(cè)編輯器
Begin-End
處補(bǔ)充代碼,計(jì)算并輸出數(shù)組的平均值和最大值。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()]; //循環(huán)給數(shù)組賦值 for(int i = 0 ; i< scores.length;i++){ scores[i] = sc.nextInt(); } /********** Begin **********/ //在這里計(jì)算數(shù)組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 **********/ } }
第5關(guān):二維數(shù)組
編程要求
1.在右側(cè)
Begin-End
區(qū)域中定義如下二維數(shù)組,使用for
循環(huán)輸出數(shù)組中所有的數(shù)據(jù):2.
使用for
循環(huán)將上述數(shù)組中的數(shù)據(jù)全部改為:
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關(guān):選擇題(2)
-
1、
聲明數(shù)組如下:
A、2float[][] f=new float[2][3];
那么該數(shù)組一共有(C )個元素
B、4
C、6
D、8 -
2、
以下的程序是否有錯(B)
A、不能運(yùn)行
B、編譯不通過
C、會正常運(yùn)行
D、以上說法都不對?文章來源地址http://www.zghlxwxcb.cn/news/detail-740683.html
?文章來源:http://www.zghlxwxcb.cn/news/detail-740683.html
?
到了這里,關(guān)于JAVA頭哥作業(yè)07 Java入門 - 數(shù)組基礎(chǔ)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!