2017-04-20 55 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);出你的循环