【问题标题】:Why is my loop going to the else condition when nothing is entered?为什么在没有输入任何内容时我的循环会进入 else 条件?
【发布时间】:2016-11-23 10:27:08
【问题描述】:

晚上好,

我不知道为什么这个循环根本不起作用。它毁了我的整个申请。下面是代码:

    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());
    }

所以输出是输入一个no,然后是1000,如下:

请告诉我数到多少?

没有

我很抱歉,但命令未被识别!

请告诉我数到多少?

1000

请告诉我从什么开始计算?

抱歉,起点值必须小于终点值!

请告诉我从什么开始计算?

为什么它直接进入第二个书面 else 语句? 为什么它会跳过 newInitialValue 的用户输入? 请注意,如果在为 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("+"))
{...

此外,由于它打印出“但起点值必须小于终点值”,我们可以推断出它分别与 if(incre...) 循环以及 do 和 try 循环一起工作。但它跳过了 (newI... = input...) 和 if(newIntia...) 代码行。我知道这个原因,即使手动输入 newInitialValue = 2(在参数内)它仍然会进入这个 else 子句。

【问题讨论】:

  • 你的代码没有任何意义,你能指出逻辑在什么时候落入else块?
  • 设置断点,看看代码如何流动。
  • 当我运行它而不需要任何 catch 命令时,它运行良好。循环时捕获的某些内容适用于该值,但接下来的代码块被搞砸了。
  • if(newEndingValue &gt;= 0 || newEndingValue &lt;= 0) 很好
  • 如果您可以让人们更容易地查看问题,您将获得更好的帮助。 F.ex.:确保您发布的所有代码都是必要的,如果有不相关的部分,请将它们删除。使您的缩进有意义,例如没有比其中的语句更缩进的左括号。明确说出您正在谈论的else 是哪一个。说一下对应if的条件所涉及的变量的值是多少。当您这样做时,您很有可能会找出问题所在。

标签: java loops try-catch java.util.scanner do-while


【解决方案1】:

-_- 所以问题出在 while 语句中:

while(!input.hasNextint())

这会向前看并检查下一个用户输入,但是由于捕获消耗了一个,因此它会查看下一个并且变得模糊......基本上如果我不使用它它就可以工作。 相反,我使用了:

while(isError == true)

在do循环下我有嵌套的if语句:

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

在下一个循环块之前,我只是覆盖了 isError

isError=true;
//next block of code

【讨论】:

    【解决方案2】:

    这里的问题是,如果 value 1 为 true 或 value2 为 true 或 value3 为 true,你说 java 做某事,然后你在 else 上说如果使用 or 做同样的事情。你必须明白你需要使用 and && 来帮助 java 理解 else 参数。

    【讨论】:

    • 我没有关注。请详细说明。它还跳转到 try-catch 循环中的嵌套 else 语句。
    • 让我们说“Increasing”是真的,其他都是假的。Java会在做if之后首先进入if,它会移动到else,你说如果“Decreasing”是假的(如果它是假的)那么它会做其他事情。当你的 if 陈述为假时,你使用 else
    • 您错过了这个问题。我们知道编译器正在跳过一大块代码并转到打印出“...但起点值必须小于终点值!...”的 else 子句,看看我如何为测试的值设置占位符在 if 语句中应该为 true 但应该在前一行中被覆盖,由于某种原因,编译器正在跳过用户分配和 if 块并仅转到 else,而不管值是什么。
    猜你喜欢
    • 2021-06-17
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多