import java.util.*;
public class Test
{
static List<Integer> list = new LinkedList<Integer>();
public static void main(String[] args)
{
Scanner scanOne = new Scanner(System.in);
System.out.println("Enter integers to be added to the list with a space between every number : ");
String name = scanOne.next();
Scanner scanTwo = new Scanner(scanOne.nextLine());
while (scanTwo.hasNextInt())
{
list.add(scanTwo.nextInt());
}
print();
}
public static void print()
{
ListIterator<Integer> listIT = list.listIterator(); // using the list Interface's method "listIterator()" to Iterate through the listIT
while(listIT.hasNext())
{
int n = listIT.next();
System.out.print(n + " ");
}
}
}
Run Code Online (Sandbox Code Playgroud)
你犯了一个简单的错误.您调用了next()然后调用nextLine(),因此打印从第二个元素开始.
String name = scanOne.next();
Scanner scanTwo = new Scanner(scanOne.nextLine());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
304 次 |
| 最近记录: |