用户从键盘只能输入整数,程序输出这些整数的乘积。
我写的但是不能输出这些整数的乘积,求解?
import java.util.*;
public class Product{
public static void main(String args[]) {
Scanner reader=new Scanner(System.in);
int Product=0;
int i=0;
while(reader.hasNextInt()){
int n=reader.nextInt();
i=i+1;
Product=Product*n;
}
System.out.printf("%d个整数的积为%d\n",i,Product*i);
}
}
------解决方案--------------------------------------------------------
你的product值为0,再怎么乘不还是0
- Java code
Scanner reader = new Scanner(System.in); int product = 1; int i = 0; while (reader.hasNextInt()) { int n = reader.nextInt(); i = i + 1; product = product * n; } System.out.printf("%d个整数的积为%d\n", i, product);
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
晕、你while里面一直没有一个条件break跳过、只要你输入就会进行while 不输入就等待、、
怎么可能执行下面的system
------解决方案--------------------------------------------------------
晕、你while里面一直没有一个条件break跳过、只要你输入就会进行while 不输入就等待、、
怎么可能执行下面的system.out呢!!
------解决方案--------------------------------------------------------
public class Product {
public static void main(String args[]) {
Scanner reader = new Scanner(System.in);
int Product = 0;
int i = 0;
while (reader.hasNextInt()) {
int n = reader.nextInt();
i = i + 1;
Product = Product * n;
if(n==5){
break;
}
}
System.out.printf("%d个整数的积为%d\n", i, Product * i);
}
}
当你输入5的时候按回车就会计算出结果。。但是由于你的produce一直为0、所以结果永远是0.