我是java的初学者,但之前我曾经使用过c ++ 在c ++中我们可以像这样编码:
int num;
while (cin>>num){
....
}
现在我想知道如何在java中做到这一点。
答案 0 :(得分:1)
这条线对于获得基础知识非常有用:
//this code allows long types to be assigned from entries in a file myNumbers
Scanner sc = new Scanner(new File("myNumbers"));
while (sc.hasNextLong()) {
long aLong = sc.nextLong();
}
点击此处查看Java文档:https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
答案 1 :(得分:0)
Scanner sc = new Scanner(System.in);
int num;
while((num = sc.nextInt()) != -1) {
System.out.println(num);
}
-1
将退出循环