Java 如果用户输入错误,如何继续保持同一行?

Java 如果用户输入错误,如何继续保持同一行?,java,while-loop,Java,While Loop,当我运行代码时,如果键入字符而不是整数,它将提示重新输入。但当我们回答第二个或第三个问题时,如果回答错误,它会让你回到第一个问题。有没有办法解决这个问题?非常感谢 while (true) { try { Scanner input = new Scanner(System.in); System.out.println("Enter your monthly income (after tax and insurance): ");

当我运行代码时,如果键入字符而不是整数,它将提示重新输入。但当我们回答第二个或第三个问题时,如果回答错误,它会让你回到第一个问题。有没有办法解决这个问题?非常感谢

 while (true) {
       try {
           Scanner input = new Scanner(System.in);

           System.out.println("Enter your monthly income (after tax and insurance): ");
           float monthlyIncome = input.nextFloat();

           System.out.println("Enter monthly cost for food: ");
           float monthlyFoods = input.nextFloat();

           System.out.println("Enter monthly cost for rent: ");
           float monthlyRent = input.nextFloat();

           System.out.println("Enter monthly cost for utilities " +
                   "(e.g. gas, phone bills, Internet): ");
           float monthlyUtilities = input.nextFloat();

           System.out.println("Enter monthly cost for shopping and hobbies: ");
           float monthlyHobbies = input.nextFloat();

           System.out.println("Do you plan for saving? (Enter 0 if not): ");
           float monthlySaving = input.nextFloat();

           monthlyExpenses(monthlyIncome, monthlyFoods, monthlyRent, 
                   monthlyUtilities, monthlyHobbies, monthlySaving);
           break;

    } catch (InputMismatchException exception) {
        System.out.println("Please enter number only!");
    }
  }

//类似这样的代码使用while循环中出现错误时需要重复的代码;这使您的代码看起来像这样:

while(true){
浮动值;
试一试{
//从用户处获取正确值的代码&存储在'value'中
返回值;
}捕获(输入不匹配异常){
System.out.println(“请只输入数字!”);
}
}
现在只需将此代码移动到一个方法:

float-getFloatValue(字符串描述){
while(true){
浮动值;
试一试{
扫描仪输入=新扫描仪(System.in);
System.out.println(questionDescription+“:”);
value=input.nextFloat();
返回值;
}捕获(输入不匹配异常){
System.out.println(“请只输入数字!”);
}
}
返回Float.NaN;
}
最后,获取值的代码如下所示:

float monthlyIncome=getFloatValue(“输入您的月收入(税后和保险费)”);
float monthlyFoods=getFloatValue(“输入每月食品成本”);
float monthlyRent=getFloatValue(“输入每月食品成本”);
//获取与上面相同的其他值
月费用(月收入、月食品、月支出、,
月效益、月目标、月储蓄);

这种方法的好处是只接受有效的浮点值,代码看起来也很干净。

另一种可能是将所有问题保存到数组中,并使用变量作为索引计算传递的输入数。然后在第二个数组中保存输入,最后只需对其进行转换

优点:这是高度可定制的,可以很容易地扩展

代码应该是这样的

// Lots of Code here
Scanner scan = new Scanner(System.in);
int count = 0;
String[] questions = {"How high...?", "[Insert questions 2]"}; // Put in all your questions here
float[] answers = new float[questions.length]; // array for answers
while(count < questions.length) {
    try {
        System.out.println(questions[count]);
        answers[count] = Float.parseFloat(scan.nextLine());
        count++;
    } catch (Exception e) {
        System.out.println("Bad doggo! No food for you tonight!"); // Tell the user he was a bad guy
    }
}
scan.close();
// If you want you can convert the answer array back to variables here but I think this way is easier
// More code
//这里有很多代码
扫描仪扫描=新扫描仪(System.in);
整数计数=0;
字符串[]问题={“多高…?”,“[插入问题2]”};//把你所有的问题都放在这里
float[]答案=新的float[questions.length];//答案数组
while(计数<问题长度){
试一试{
System.out.println(问题[计数]);
answers[count]=Float.parseFloat(scan.nextLine());
计数++;
}捕获(例外e){
System.out.println(“坏狗狗!今晚没有食物给你!”;//告诉用户他是个坏人
}
}
scan.close();
//如果需要,可以在这里将答案数组转换回变量,但我认为这种方法更简单
//更多代码

Yes:更多循环。这就是方法非常方便的地方,可能是将所有问题保存到数组中,并使用变量作为索引计算传递了多少输入。然后在第二个数组中保存输入,最后只需对其进行转换。优点:这是高度可定制的,可以很容易地扩展到ANKS的家伙!我搞定了!祝你有美好的一天!非常感谢各位!现在我明白了,代码更清晰,更有意义!再次感谢你们的帮助!祝你有美好的一天!
// Lots of Code here
Scanner scan = new Scanner(System.in);
int count = 0;
String[] questions = {"How high...?", "[Insert questions 2]"}; // Put in all your questions here
float[] answers = new float[questions.length]; // array for answers
while(count < questions.length) {
    try {
        System.out.println(questions[count]);
        answers[count] = Float.parseFloat(scan.nextLine());
        count++;
    } catch (Exception e) {
        System.out.println("Bad doggo! No food for you tonight!"); // Tell the user he was a bad guy
    }
}
scan.close();
// If you want you can convert the answer array back to variables here but I think this way is easier
// More code