这个程序从键盘读取数字并对其求和,直到用户写完"总计",然后我得到java.lang.NumberFormatException
。
Boolean isTotal = false;
int sum = 0;
while(!isTotal)
{
java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
String s = br.readLine();
if(s=="total")
{
isTotal = true;
}
if(!isTotal)
sum = sum + Integer.parseInt(s);
}
System.out.println(sum);
答案 0 :(得分:0)
Boolean isTotal = false;
int sum = 0;
while(!isTotal)
{
java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
String s = br.readLine();
if(s.equals("total"))
{
isTotal = true;
}
if(!isTotal)
sum = sum + Integer.parseInt(s);
}
System.out.println(sum);