總見流星過,火花轉瞬逝。何時見春雨,潤物細無聲。
導入包,例使用Scanner工具類,需要導入包:
import java.util.Scanner;
************
***********
{
private static Scanner input;
public static void main(String[] args)
{
input = new Scanner(System.in);
int score = input.nextInt();
}
}
--------------------------------------------------------------------
for語句的簡化版本:
// 定義一個整型數組,保存成績信息
int[] scores = { 89, 72, 64, 58, 93 };
// 使用foreach遍歷輸出數組中的元素
for ( int score : scores )
{
System.out.println(score);
}
--------------------------------------------------------------------
數組:
例:一維數組
String[] names; // String[] names;
names = new String[5];
int[] scores = new int[6];
int[] number = {1,2,3,4,5}; 等價於 int[] number = new int[]{1,2,3,4,5};
數組的下標是從0開始
例:二數組
數據類型[][] 數組名 = new 數據類型[行的個數][列的個數];
int[][] scores = new int[4][16];
遍歷數組:
for(int i=0; i<scores.length; i++)
{
for(int j=0; i<scores[i].length; j++)
{
scores[i][j] = 111;
}
}
需要了解的:在定義二維數組時也可以只指定行的個數,然后再為每一行分別指定列的個數。如果每行的列數不同,則創建的是不規則的二維數組,如下所示: