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);
}
}
打印出来:
哪个好,但我只想要总数,为58.简单的东西:/我是新手。
答案 0 :(得分:1)
当你遍历循环时,你正在调用System.out.println方法,这就是为什么你要获得所有输出,你需要从你的方法中取出你的方法环
删除这些:
System.out.println("Random number: " + numbers[i]);
System.out.println("Product is: " + product);
将此内容置于for循环之外:
System.out.println("Product is: " + product);
答案 1 :(得分:0)
从循环中删除以下语句:
System.out.println("Random number: " + numbers[i]);
System.out.println("Product is: " + product);
在循环外添加以下内容:
System.out.println("Product is: " + product);
答案 2 :(得分:0)
从循环中获取此System.out.println("Product is: " + product);