2017-02-10 76 views
-3

你好,我一直在试图做一个程序,问ASK 1)有多少学生? 2)询问一个名字和测试标记如何使用java输入数组

有人可以帮我解决这个问题。进出口新的编程这里是我的代码

Scanner input = new Scanner(System.in); 

System.out.println("How many Students in class?"); 
int n = input.nextInt(); 
System.out.println("Enter the " + n + " names: "); 

String [] names = new String[n]; 

for (int i = 0; i < names.length; i++) 
{ 
    names[i] = input.nextLine();  
} 
+2

k.joe,欢迎来到StackOverflow。我想你会得到这个问题的提示,因为它不符合网站的精神和规则。你已经提出了这个问题,但请确保你提出你在代码中面临的问题/错误,并为此提出解决方案。请不要指望人们为你写逻辑。 –

+0

欢迎,请参阅[问],你没有解释你的问题。 – AxelH

+1

使用next(),nextInt()或其他nextFoo()方法后,[Scanner正在跳过nextLine()](http:/ /stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-nextint-or-other-nextfoo) –

回答

1

这是一个“问题”与nextInt,该方法仅读出数字,让你打电话nextLine<enter>所以接下来的时间内,<enter>存在,它会读取这个,所以这个值是空的。您需要先清除输入,只需阅读该行。

System.out.println("How many Students in class?"); 
int n = input.nextInt(); 
input.nextLine(); //will consume the \n 
相关问题