Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java try语句未用于循环的第二次反迭代_Java_Try Catch_Infinite Loop_Do While - Fatal编程技术网

Java try语句未用于循环的第二次反迭代

Java try语句未用于循环的第二次反迭代,java,try-catch,infinite-loop,do-while,Java,Try Catch,Infinite Loop,Do While,我正在制作一个方法,以获得用户想要求和的数字。如果他们想把数字1,2和3相加。他们想把3个数字相加。因此,当我问他们想要加起来多少时,我使用try-catch来捕捉他们是否输入了小数点。因为不能将3.5个数字相加,所以可以添加3个或4个数字。问题是,如果用户输入一个十进制数,程序将无限循环运行除try语句外的所有内容。我怎样才能解决这个问题 以下是该方法的代码: private static int requestMaxInputForFloat(Scanner scanner){ bo

我正在制作一个方法,以获得用户想要求和的数字。如果他们想把数字1,2和3相加。他们想把3个数字相加。因此,当我问他们想要加起来多少时,我使用try-catch来捕捉他们是否输入了小数点。因为不能将3.5个数字相加,所以可以添加3个或4个数字。问题是,如果用户输入一个十进制数,程序将无限循环运行除try语句外的所有内容。我怎样才能解决这个问题

以下是该方法的代码:

private static int requestMaxInputForFloat(Scanner scanner){
    boolean appropriateAnswer = true; // assume appropriate catch not appropriate to loop again
    int howManyInputs = 1000000; // hold value to return how many inputs. if this value we will not run.

    //request an appropriate number of inputs until appropriate answer = true;
    do
    {
        appropriateAnswer = true; //if looped again reset value to true
        try{
            System.out.print("How many decimal place numbers would you like to sum? ");
            howManyInputs = scanner.nextInt();
        }catch(Exception e){
            System.out.println("Sorry but you can only request to average a whole number set of data.\nTry Again.");
            appropriateAnswer = false;
        }//end of try-catch
        if (howManyInputs <= 0) //invalid answer
        {
            System.out.println("Sorry but "  + howManyInputs + " is equal to or below 0. Please try again.");
        }else{
            appropriateAnswer = false;
        }
    }while(!appropriateAnswer);//end of while loop

    return howManyInputs; //return the value 
}// end of getMaxInput
private static int requestMaxInputForFloat(扫描仪){
boolean-propertearsh=true;//假定适当的捕获不适合再次循环
int howManyInputs=1000000;//保留值以返回多少输入。如果此值为空,我们将不运行。
//请求适当数量的输入,直到适当的回答=真;
做
{
properteAnswer=true;//如果再次循环,则将值重置为true
试一试{
System.out.print(“您希望求和多少位小数?”);
howManyInputs=scanner.nextInt();
}捕获(例外e){
System.out.println(“对不起,您只能请求对整组数据求平均值。\n请再试一次。”);
正确答案=错误;
}//试捕结束
如果(howManyInputsAdd
scanner.nextLine();
在catch块中。我认为问题在于,如果
nextInt()
出现错误,扫描仪的“指针”仍然指向错误字符,如果您只是尝试
nextInt())
再次,它将尝试再次扫描同一个坏字符。您必须采取措施使扫描仪跳过它。在这里,您只需扔掉用户键入的任何内容,因此跳过整个输入行剩余部分的
nextLine()

还有一件事:我会改变

if (howManyInputs <= 0) //invalid answer

if(多少次输入)在执行
scanner.nextInt()
之后添加新行并再次运行。我仍然得到一个无限循环。即使在nextInt()之后有新行;我现在注意到它正在运行Try循环并打印打印();但它似乎跳过了.nextInt();部分。Try
scanner.nextLine()
catch
块中。我认为问题在于,如果
nextInt()
出现错误,扫描仪的“指针”仍然指向错误字符(如小数点),如果您尝试
nextInt()
再次,它将尝试再次扫描同一个坏字符。您必须做一些事情使扫描仪跳过它。谢谢Ajb!修复了它。感谢您的帮助。好的,我将把它作为答案发布……是的,这很有效,您所建议的更改是正确的。非常感谢!
if (appropriateAnswer && howManyInputs <= 0) //invalid answer