简体   繁体   English

当什么都没输入时,为什么我的循环转到else条件?

[英]Why is my loop going to the else condition when nothing is entered?

Good evening, 晚上好,

I'm at a loss as to why this loop isn't working at all. 我茫然地知道为什么这个循环根本不起作用。 It's ruining my entire application. 它毁了我的整个应用程序。 below is the code: 下面是代码:

    System.out.println("Please tell me what to count till?");
    do  
    {
        try
        {
            newEndingValue= input.nextInt();
            if(newEndingValue >= 0 || newEndingValue <= 0) 
            {
               break; //breaks the loop
            }
        }
        catch (InputMismatchException e) 
        {
            System.out.println("My Apologies, but COMMAND NOT RECOGNIZED!" + "\nPlease tell me what to count till?");
            input.next();       

        }           
    }   
    while(!input.hasNextInt()); 

    v.setEndingValue(newEndingValue);


System.out.println("Please tell me what to count from?");
if(increasingOrDecreasing.equalsIgnoreCase("Increasing")|| increasingOrDecreasing.equals("++") || increasingOrDecreasing.equals("+"))
    {
    do  
    {
      try 
      {
            newInitialValue = input.nextInt();
            if(newInitialValue < v.getEndingValue()) 
                {
                 break; 
                }
            else{ 
                System.out.println("My Apologies, but starting point value must be smaller than ending point value!" + "\nPlease tell me what to count from?"); 
                newInitialValue = (v.getEndingValue()+10);//overrides the value to something that forces the loop back
                }
        }
        catch (InputMismatchException e) 
            {
            System.out.println("My Apologies, but COMMAND NOT RECOGNIZED!" + "\nPlease tell me what to count from?"); 
            input.next();       
            }           
     }  
    while(!input.hasNextInt() || newInitialValue > v.getEndingValue());
     }
else
{
    do 
    {
    try 
        {
        newInitialValue = input.nextInt();
        if(newInitialValue > v.getEndingValue()) 
            {
             break; //breaks the loop
            }
        else{ 
            System.out.println("My Apologies, but starting point value must be larger than ending point value!" + "\nPlease tell me what to count from?"); 
            newInitialValue = (v.getEndingValue()-10);//overrides the value something that forces the loop back
            }
        }
    catch (InputMismatchException e)
        {
        System.out.println("My Apologies, but COMMAND NOT RECOGNIZED!" + "\nPlease tell me what to count from?"); 
        input.next(); //consumes the erroneously typed string value 
        newInitialValue = (v.getEndingValue()-10);
        }           
    }   
    while(!input.hasNextInt() || newInitialValue < v.getEndingValue());
    }

So the output is when entered a no, and then 1000 as follows: 因此,输入no时,输出为1000,如下所示:

Please tell me what to count till? 请告诉我算什么?

no 没有

My Apologies, but COMMAND NOT RECOGNIZED! 我很抱歉,但是命令无法识别!

Please tell me what to count till? 请告诉我算什么?

1000 1000

Please tell me what to count from? 请告诉我从什么算起?

My Apologies, but starting point value must be smaller than ending point value! 抱歉,起点值必须小于终点值!

Please tell me what to count from? 请告诉我从什么算起?

Why is it going straight to the second written else statement? 为什么直接进入第二个书面else语句? Why is it skipping user entry for newInitialValue? 为什么跳过newInitialValue的用户输入? Please note that if edit code after ending value block to below after entering a string for newEndingValue and then correctly enter a number, this rids me of my error but generates another one if ran again and the user cooperates: 请注意,如果在为newEndingValue输入字符串之后在结束值块之后将代码编辑到下面,然后正确输入数字,这使我摆脱了我的错误,但是如果再次运行并且用户配合,则会生成另一个错误:

...
newInitialValue = input.nextInt(); //essentially gets skipped over by compiler only when previous catch statement is triggered
System.out.println("Please tell me what to count from?");
if(increasingOrDecreasing.equalsIgnoreCase("Increasing")|| increasingOrDecreasing.equals("++") || increasingOrDecreasing.equals("+"))
{...

additionally since its printing out "but starting point value must be smaller than ending point value" we can deduce its working with if(incre...) loop and the do and try loops respectively. 另外,由于其打印输出为“但起点值必须小于终点值”,因此我们可以推论其分别与if(incre ...)循环以及do和try循环一起工作。 But its skipping the (newI... = input...) and the if(newIntia...) lines of code. 但是它跳过了(newI ... = input ...)和if(newIntia ...)代码行。 i know this cause even manually entering in newInitialValue = 2 (within paramenter) it still goes to this else clause. 我知道,即使手动输入newInitialValue = 2(在paramenter中),也仍然会转到else子句。

-_- So the issue is within the while statements: -_-因此问题在while语句内:

while(!input.hasNextint())

this looks ahead and checks the next user input, but since the catch consumed one it looks to the next next and it gets murky... essentially if i don't use this it works. 这会向前看并检查下一个用户输入,但是由于捕获消耗了一个输入,因此它会查找下一个输入,并且变得模糊...本质上,如果我不使用它,它将起作用。 instead i used: 相反,我用:

while(isError == true)

and under the do loop with the nested if statement i have: 在嵌套的if语句的do循环下,我有:

if(blah blah blah){
isError=false;
break;

and before the next loop block i simply override the isError 在下一个循环块之前,我只是覆盖isError

isError=true;
//next block of code

这里的问题是,您说要让java在value 1为true或value2为true或value3为true的情况下执行某些操作,然后再在else上说要使用or进行相同操作。您已经了解到需要使用and &&为了帮助Java理解else参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM