日期:2014-05-20  浏览次数:20712 次

请详细注释代码
请详细注释:

Java code
public class TestVector
{
int b=0;
Vector v=new Vector();
System.out.println("please enter number:");
while(true)
{
    try
    {
        b=System.out.in.read();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    if(b=='\r'||b=='\n')
    {
        break;
    }
    else
    {
        int num=b-'0';
        v.addElement(new Integer(num));
    }
    int sun=0;
    Enumeration e=v.elements();
    while(e.hasMoreElements())
    {
        Integer intObj=(Integer)e.nextElement();
        sum+=intObj.intValue();
    }
    System.out.println(sum);
}


------解决方案--------------------
你新手吧?建议你先看看基础的东西,要不写上再详细的注释你也看不懂!这段代码不难的!
------解决方案--------------------
Java code
import java.util.Enumeration; //引入包
import java.util.Vector;//引入包

public class TestVector {
    public static void main(String[] args) { //应该写到main方法中或者其他的类方法中 ,不允许直接写到类中
        int b = 0;
        Vector v = new Vector(); //定义一个Vector
        System.out.println("please enter number:"); //提示输入
        while (true) {
            try {
                b = System.in.read();// 读入一个整形, 1位数字 ,例如输入 :111回车 ,则认为输入了3个1 
            } catch (Exception e) {//防止读入的不是有效的整数
                e.printStackTrace();
            }
            if (b == '\r' || b == '\n') {//回车换行则跳出
                break ;//此处应该是继续输入,而不是跳出
                //System.out.println("") ;
                //continue;//是继续输入
            } else {
                int num = b - '0';
                v.addElement(new Integer(num));//往 Vector中加入元素
            }
            int sum = 0;// 此处应该是 sum 而不是 sun
            Enumeration e = v.elements(); //将Vector中的内容返回放到一个Enumeration中而已
            while (e.hasMoreElements()) {//判断是否存在更多内容
                Integer intObj = (Integer) e.nextElement();//得到当前内容
                sum += intObj.intValue();//累加
            }
            System.out.println(sum);//输出
        }
    }
}

------解决方案--------------------
Java code

import java.util.Enumeration;
import java.util.Vector;

public class TestVector {
    /**
     * 对从控制台输入的单个字符数字进行累加,回车结束。
     * 例如输入:1493
     * 会依次打印:
     * 1
     * 5
     * 14
     * 17
     */
    public static void main(String[] args) {
        int b = 0;
        //定义一个数组,用于存放从控制台输入的数字
        Vector v = new Vector();
        System.out.println("please enter number:");
        while (true) {
            try {
                //读取控制台输入的单个数字字符。
                //PS:由于下列程序没对数字以外的其他字符abcd等做异常处理,请忽略,也别输入
                b = System.in.read();
            } catch (Exception e) {
                e.printStackTrace();
            }
            //当控制台读到回车的字符时,退出循环。结束程序
            if (b == '\r' || b == '\n') {
                break;
            } else {
                //当读取到非回车时,取得数字,并加入数组。
                //PS.读取到得是数字的字符而非数字,所以(数字的值 = 数字字符 - '0')
                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);
        }
    }
}