baixiaotian
发贴: 8
积分: -1
|
于 2005-05-11 16:56
import java.util.*;
public class TestVector { /** * Method main * * * @param args * */ /* Vector 类与Enumeration接口 将键盘输入的一个数字序列中 的每位数字存储在Vector对象中, 然后在屏幕上打印出每位数字相 加的结果,例如:输入32打印出5 .*/ public static void main(String[] args) { // TODO: Add your code here Vector v = new Vector(); int b = 0; System.out.println("Enter your number:"); while(true) { try{ b = System.in.read(); }catch(Exception e){} if(b=='\r'||b=='\n') break; else{ int num = b - '0'; /* '0' = 48; '1'=49; */ 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); } }
|