import java.util.*;
public class CyclicShiftApp{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<Integer>();
while(scan.hasNextInt()){
list.add(scan.nextInt());
}
Integer[] nums = new Integer[list.size()];
nums = list.toArray(nums);
for(int i = 0;i < nums.length; i++){
System.out.println(nums[i]);
}
}
感谢穷人调试,我发现while(scan.hasNextInt())
实际上并没有添加任何东西。可能出了什么问题?是我的谷歌能力弱还是缺乏专业知识让我失望?我对编程相当陌生,所以不熟悉列表,所以认为这将是一个很好的第一步,但有些东西并没有增加。它也可以很好地编译,所以它不再是语法了。也许是选角问题?
山姆威斯大师,这有用吗?
import java.util.*;
public class CyclicShiftApp{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<Integer>();
System.out.print("Enter integers please ");
System.out.println("(EOF or non-integer to terminate): ");
while(scan.hasNextInt()){
list.add(scan.nextInt());
}
Integer [] nums = list.toArray(new Integer[0]);
for(int i = 0; i < nums.length; i++){
System.out.println(nums[i]);
}
}
}
我假设您需要将列表作为数组是有原因的,否则不需要转换为数组。正如 Jon Skeet 在评论中提到的,只有当流没有下一个 int 时,循环才会终止,即。如果您使用“java CyclicShiftApp
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)