当前位置: 代码迷 >> Java相关 >> 大家帮小弟我看上这代码最后运行时如何不进行加法运算
  详细解决方案

大家帮小弟我看上这代码最后运行时如何不进行加法运算

热度:3273   发布时间:2013-02-25 21:43:44.0
大家帮我看下这代码最后运行时怎么不进行加法运算
package testVector;

import java.io.IOException;
import java.util.*;



public class TestVector {
public static void main(String[] args)
{
int b=0;
Vector v=new Vector();
System.out.println("please enter number:");
while(true)
{
try {
b=System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
if(b == 'r'||b == 'n')
break;
else
{
int num=b-'0';
v.addElement(new Integer(num));
}
}
int sum=0;
Enumeration e=v.elements();
while(e.hasMoreElements())
{
Integer intObj=(Integer)e.nextElement();
sum += intObj.intValue();
}
System.out.println(sum);
}

}

------最佳解决方案--------------------------------------------------------
能加,输入时要这样:
1234ar


------其他解决方案--------------------------------------------------------
不过这代码是要将输入的数字加起来,如输入1234,则得出的是10,像2L给出的答案得出来的就是59
  相关解决方案