對些許代碼感到疑惑
public class HelloWorld {
? ??
? ? public static void main(String[] args) {
? ? ? ??
? ? ? ? // 定義一個整型數組,長度為10
int[] nums =new int[10];
? ? ? ??
? ? ? ? //通過循環給數組賦值
for (int i = 0; i < nums.length; i++) {
? ? ? ? ? ? // 產生10以內的隨機數
int x = (int)(Math.random()*10);
? ? ? ? ? ??
nums[i] = x;// 為元素賦值
}
? ? ? ??
// 使用foreach循環輸出數組中的元素
for (int num:nums) {
System.out.print(num+ " ");
}
}
}
2020-05-05
for循環和foreach循環的區別而已,可以簡化代碼,在底層直接實現遍歷輸出,適用于簡單循環,復雜的循環方式還是建議使用for
2020-02-05
for循環遍歷數組的一種簡單寫法 其實和for (int i = 0; i < nums.length; i++)效果一樣的。
2019-08-30
int 是數據類型;num 相當于for循環中的 i? ?例如for(int i = 1 ; i<5 ; i++),nums 是要循環的對象。就是上面定義的數組名,可以變。
2019-08-21
for(int num : nums)
int是每循環是什么類型
num是每次循環的變量都放在這里
nums是從哪里獲取數據
2019-08-14
這個是遍歷num數組,然后實現循環輸出
2019-08-13
這三行,,,