在此程序中,內循環生成100中的隨機數,然後當 隨機數爲7時停止生成它們。外循環重複100次內循環。
爲什麼我的外循環不能重做內循環?
它似乎只做了一次。學習Java中的嵌套while循環
package test;
public class Loops {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 0;
int sum = 0;
int counter = 0;
String randomNumberList = " ";
int c = 0;
while (c != 100){
while (i != 7) {
i = (int) (101 * Math.random());
sum += i;
++counter;
randomNumberList += " " + i;
}
System.out.print("\n loop repeated" + counter+ " times and generated these numbers: " + randomNumberList);
++c;
}
}
}
它爲我跑了很多次。我有100行輸出。你是否想知道爲什麼每次輸出都是一樣的? – 2011-02-12 07:06:02