【问题标题】:Why is the last else statement not printing/executing here?为什么最后一个 else 语句没有在这里打印/执行?
【发布时间】:2019-07-03 17:26:38
【问题描述】:

最后一个 else 语句不会打印任何内容... 只需输入并停止程序。我做错了什么?

我是全新的,刚开始学习java。没有任何代码方面的经验。对不起。

尝试检查语法,“我的菜鸟眼睛”看起来一切正常

import java.util.Scanner;
public class MyClass 
{
    public static void main(String[] args)

    { 
        Scanner scn = new Scanner(System.in);

        int x =7;

        System.out.println("Enter a Number from 1 to 10: ");

        int guess = scn.nextInt();

        while(x != guess) 
        {
            if(x < guess) {
            System.out.println("Guess Lower...");
            guess = scn.nextInt();
            }

            else if (x > guess) {
                System.out.println("Guess Higher...");
                guess = scn.nextInt();
            }

            else {
            System.out.println("Correct Guess!");
            }
        } 
        }

}


所以如果我输入 7 它应该说/显示 猜对了!

但它不会显示任何内容。

不过,猜测低和高都可以。

【问题讨论】:

  • 这是您的while 条件。它在else 块有机会运行之前跳出循环。
  • 但最后一个 else 包含在其中,不是吗?
  • @S.S.不,最后一个 else 在 if 内,而不是 while
  • 哦,好吧,所以 x 变成 = guess,在这种情况下它混淆了 while 条件,即 while x is NOT eq to guess?
  • @S.S 我认为@liya Bursov 想说的是,最后一次执行else 的唯一时间是x == guess。但是,您的 while 循环仅在 x != guess 时运行。

标签: java if-statement


【解决方案1】:

你的问题是while循环的条件。 看看Recursión in Java。 可以这样做:

import java.util.*;

public class MyClass 
{

 public static void main(String []args)
 {
   new_guess();

}


static void new_guess()
{
    int correct_answer = 7;
    Scanner scn = new Scanner(System.in);

    System.out.println("Enter a Number from 1 to 10: ");
    int guess = scn.nextInt();

    if(correct_answer != guess)
    {
        if(correct_answer < guess)
        {
        System.out.println("Guess Lower...");
        }
        else if (correct_answer > guess) 
        {
            System.out.println("Guess Higher...");
        }
        new_guess();
    }
    else
    {
          System.out.println("Correct Guess!");
    }

}

}

【讨论】:

  • 天啊...我以前从未见过这个。感谢分享,我什么都不懂,但是这看起来既吓人又有趣。
  • java中的递归是一个方法不断调用自身的过程。在某些条件下调用非常重要,例如在您的情况下 - 方法在用户猜测不正确时不断调用自身。
  • ohoho...不错!我不知道你能做到这一点 --> new_guess();学习时间
【解决方案2】:

当比较正确的数字时,代码会立即转义 while 循环,因此最后一个 else 语句无法访问。

你可以像下面那样做你想做的事:

import java.util.Scanner;
public class MyClass 
{
    public static void main(String[] args)

    { 
        Scanner scn = new Scanner(System.in);

        int x =7;

        System.out.println("Enter a Number from 1 to 10: ");

        int guess = scn.nextInt();

        while(x != guess) {
            if(x < guess) {
                System.out.println("Guess Lower...");
                guess = scn.nextInt();
            } else if (x > guess) {
                System.out.println("Guess Higher...");
                guess = scn.nextInt();
            }
        } 
        System.out.println("Correct Guess!");
     }
}

【讨论】:

    【解决方案3】:

    你应该让这个代码System.out.println("Correct Guess!")退出循环,因为它会一直循环,直到找到被猜测的值,而不转到代码的下一行。

    请参考下面的代码。

    Scanner scn = new Scanner(System.in);
    
        int x = 7;
    
        System.out.println("Enter a Number from 1 to 10: ");
    
        int guess = scn.nextInt();
    
        while (x != guess) {
            if (x == guess)
                System.out.println("correct");
            else if (x < guess) {
                System.out.println("Guess Lower...");
                guess = scn.nextInt();
            }
    
            else if (x > guess) {
                System.out.println("Guess Higher...");
                guess = scn.nextInt();
            }
    
        }
        System.out.println("correct");
    
    }
    

    【讨论】:

      【解决方案4】:

      它是一个while循环,它一直运行直到指定的表达式为假,所以你不需要引入一个else块来“确认”guess变量的相等性。当您的表达式返回 false 时,它​​会自动打印出下一步。

          while(x != guess) 
          {
              if(x < guess) {
              System.out.println("Guess Lower...");
              guess = scn.nextInt();
              }
      
              else if (x > guess) {
                  System.out.println("Guess Higher...");
                  guess = scn.nextInt();
              }
          } 
              System.out.println("correct!");
          }
      

      基本上你的代码应该以这种方式进行改革

      【讨论】:

        最近更新 更多