2017-04-20 58 views
-1
public static void main(String []args){ 
    Random gen = new Random(); 
int[] numbers = new int[6]; 
int sum, product; 
for (int i = 1; i < numbers.length; i++){ 
    int pick = gen.nextInt(10); 
    numbers[i] = pick; 
    sum = (numbers[1]+numbers[2]+numbers[3]+numbers[4]+numbers[5]); 
    product = sum *2; 
    System.out.println("Random number: " + numbers[i]); 
    System.out.println("Product is: " + product); 
} 
} 

它打印這樣的:如何才能打印總價值?

  • 隨機數:0
  • 產品是:0
  • 隨機數:8
  • 產品是:16
  • 隨機數是:9
  • 產品是:34
  • 隨機編號爲:3
  • 產品是40
  • 隨機數是:9
  • 產品是58

這很好,但我只想總數爲58簡單的東西:/我是新在這。

+3

把'System.out.println'外循環。 –

回答

1

的時候無論你通過迭代你正在調用System.out.println方法的循環,這就是爲什麼你要得到所有輸出,你需要從for循環中取出方法

刪除這些:

System.out.println("Random number: " + numbers[i]); 

System.out.println("Product is: " + product); 

將這個一個外面的for循環:

System.out.println("Product is: " + product); 
+0

就是那麼簡單的T_T。先生非常感謝您! – BronyMC

+1

沒問題的傢伙 –

0

從循環中刪除以下語句:

System.out.println("Random number: " + numbers[i]); 
System.out.println("Product is: " + product); 

,並添加以下外循環:

System.out.println("Product is: " + product); 
0

得到這個System.out.println("Product is: " + product);出你的循環