进行无效输入后,无效的输入循环将继续出现在此程序中。我正在寻求继续询问用户输入,直到他/她输入有效数据。然后我想使用该输入继续该程序。我很肯定有一种与此相关的循环。谢谢你的帮助。
identInput = JOptionPane.showInputDialog(null, "Please enter a student ID: ");
intID = Integer.parseInt(identInput);
savedIntID = Integer.parseInt(identInput);
for(x = 0; x < studentIDs.length; ++x)
{
if(identInput.equals(studentIDs[x]))
{
JOptionPane.showMessageDialog(null, "The first name associated with \nStudent ID "
+ intID + " is: " + firstNames[x] + "\n" + firstNames[x] + "'s current GPA is: "
+ gPAs[x]);
inputTruth = true;
break;
}
}
//The following will show up and continue to if the data is incorrect. Am not
//sure how to reuse if good data are entered.
while(inputTruth == false)
{
JOptionPane.showInputDialog(null, "The Student ID you entered "
+ savedIntID + "\nis not valid. \nPlease enter another Student ID: ");
}
答案 0 :(得分:1)
你只需要一个循环
while(!isInputValid){
//Take your input
if(check == input){
isInputValid = true;
}else{
//Please enter valid input try again.
}
}
答案 1 :(得分:1)
inputTruth = false;
while(inputTruth == false)
{
identInput = JOptionPane.showInputDialog(null, "Please enter a student ID: ");
intID = Integer.parseInt(identInput);
savedIntID = Integer.parseInt(identInput);
for(x = 0; x < studentIDs.length; ++x)
{
if(identInput.equals(studentIDs[x]))
{
JOptionPane.showMessageDialog(null, "The first name associated with \nStudent ID "
+ intID + " is: " + firstNames[x] + "\n" + firstNames[x] + "'s current GPA is: "
+ gPAs[x]);
inputTruth = true;
break;
}
}
//The following will show up and continue to if the data is incorrect. Am not
//sure how to reuse if good data are entered.
if(inputTruth == false)
{
JOptionPane.showInputDialog(null, "The Student ID you entered "
+ savedIntID + "\nis not valid. \nPlease enter another Student ID: ");
}
}
答案 2 :(得分:1)
inputTruth = false;
while(inputTruth == false)
{
for(x = 0; x < studentIDs.length; ++x)
{
if(identInput.equals(studentIDs[x]))
{
JOptionPane.showMessageDialog(null, "The first name associated with \nStudent ID "
+ intID + " is: " + firstNames[x] + "\n" + firstNames[x] + "'s current GPA is: "
+ gPAs[x]);
inputTruth = true;
break;
}
}
if (inputTruth == false) {
identInput = JOptionPane.showInputDialog(null, "The Student ID you entered "
+ savedIntID + "\nis not valid. \nPlease enter another Student ID: ");
}
}