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簡單的東西:/我是新在這。
把'System.out.println'外循環。 –