2014-05-06 77 views
-1

在我让用户输入扫描仪后,我正在创建一个程序,我有一个新的数组类型“教师[]”之前有几个如果选项为用户的整数输入。 基本上它是:当用户输入扫描仪只是空行,程序停止

System...println("Hello, here are your options:..); 
Scanner user = new Scanner(System.in); 
boolean valid = false; 
while(valid==false) 
{ 
    .... make sure it becomes valid... 
} 
Instructor[] list = new Instructor[2]; 
if(user.nextInt()==1) 
{ 
    do this 
} 
else if(==2) 
{ 
    do this 
} 
... if(==n) 
... 
else 
{ 
    System...println("wow"); 
} 

所以3个选项我想出现,如果我进入while循环作品之外的价值,让我回来,但后来当我输入一个有效的int程序以一个空行停止,我相信这是因为我创建了新的Instructor []数组。事情是我需要使它在3个选项之外,因为我需要为每个选项使用相同的选项。 我试过没有while循环,它仍然是空白的,没有Instructor []它的作品,但我怎么能组织这个,以便它继续运行,当我在if语句之外做一个新的Instructor []?

+0

您可以发布整个代码吗? – oschlueter

+1

请发布您的真实代码。我想我知道你的问题是什么,但我不能确定,因为这只是伪代码。 – 2rs2ts

回答

1

如果您只想在每个循环提示用户一次,则可能需要将user.nextInt()设置为等于变量。它看起来就像你现在这样做,程序要求每个if语句都有新的用户输入。所以尝试是这样的:

input = user.nextInt(); if (input==1){ do this } else if (input==2){ do this } else if (input==3){ do this }

这是什么意思?

+0

是的,谢谢!先将扫描仪转换为int,解决了问题! – user3586832