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

用Scanner能不能实现这样的功能?
用户输入的内容为:   1   3   5   2   67   23     //注:整数的个数是随机的
用Scanner将这些数字存放到一个list里面,
程序大概框架是这样
main()   {
ArrayList <Integer>   l   =   new   ArrayList <Integer> ();
System.out.println( "请输入若干个整数,中间用空格分开: ");
//接收代码
.......

System.out.println( "程序结束 ");
}

------解决方案--------------------
Scanner std=new Scanner(System.in);
ArrayList <Integer> a=new ArrayList <Integer> ();
String []str=std.nextLine().split( " ");
for(int i=0;i <str.length;i++)
{
a.add(Integer.parseInt(str[i]));
}
Iterator <Integer> iter=a.iterator();
while(iter.hasNext())
System.out.print(iter.next()+ " ");
是这个意思吧。。。
------解决方案--------------------
还是用
BufferedReader in =new BufferedReadre(new InputStream(System.in));
我觉得可能还好用一点

------解决方案--------------------
就是用这个接收数据啊
或者你也可以用那个

public static void main(String[] args) {}
这里的args[]数组接收
后面直接写args 就可以啊
在RUN PROJECT 输入你的数据
------解决方案--------------------
Scanner scan = new Scanner(System.in);
List list = new ArrayList();
while(scan.hasNextInt()){
list.add(scan.nextInt());
}
我没试过,不过大致应该差不多,你可以试试!
------解决方案--------------------
可以的以下代码使用户能够从 System.in 中读取一个数:

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

不过你这种情况还是用nextLine()或者其他类的readLine()方法
然后用得到的字串split( " ")