Java 猜数游戏奇怪的输出

Java number guessing game weird output

public class Test {
    public static void main(String[] args) {
        System.out.println("Welcome to Siddharth's number guessing game. \nThink of a number between 1 and 100 and press 1 to continue");
        Scanner x= new Scanner(System.in);
        if (x.nextInt()==1)
        {
        int high=100;   
        int low=0;
        int guess=(high+low)/2;
        System.out.println("Is your number "+ guess+"? Press 0 if yes, press 1 if your number is higher than this number or press 2 if your number is lower than this number!");
        Scanner y= new Scanner(System.in);
        int ans=y.nextInt();
           while(ans!=0)
           {
               if (ans==1)
               {
                   low=ans;
                    guess=(high+low)/2;
                    System.out.println("Is your number "+ guess+"? Press 0 if yes, press 1 if your number is higher than this number or press 2 if your number is lower than this number!");
                    Scanner y1= new Scanner(System.in);
                    ans=y1.nextInt();
               }
               else if (ans==2)
               {
                   high=ans;
                    guess=(high+low)/2;
                    System.out.println("Is your number "+ guess+"? Press 0 if yes, press 1 if your number is higher than this number or press 2 if your number is lower than this number!");
                    Scanner y2= new Scanner(System.in);
                    ans=y2.nextInt();
               }

           }
           System.out.println("The number you thought is"+guess+"! Thanks for playing!");
        }
        else
        {
            System.out.println("No problem. Restart the program and press 1 when ready!");
        }
    }

}

我想到了一个数字 22,当它问我我的号码是不是 50 时,我输入了 2。然后它马上就变成了 1。无论我输入什么,它都显示 1 作为输出,而不是像 50, 25、12 等。我在 Python 和 C 中做了同样的事情,它们工作得很好。

high = anslow = ans替换为high = guesslow = guess——将相应的"guessing range"边界移动到输入值(1或2) ), 但对先前猜测的值,如在二进制搜索中。

好的,所以,第一个问题是您将 ans 分配给 highlow 而不是 guess

那么,如果我可以给你一些建议:

  1. 不要每次都创建一个新的扫描器。相反,始终使用相同的 x 扫描仪
  2. 不要在每个 if/else 块中放置共同的动作。相反,将常用操作放在外面。
  3. 您可以给出一次说明(1 表示更高等),然后您只需询问 "is your number XX? (0:yes, 1:higher, 2:lower)"。它更具可读性。

这是一个改进版本,但我相信您可以做得更好:

public static void main( String[] args ){
    System.out.println( "Welcome to Siddharth's number guessing game. \nThink of a number between 1 and 100 and "
            + "press 1 to continue" );

    Scanner x = new Scanner( System.in );

    if( x.nextInt() == 1 ){

        int high = 100;
        int low = 0;
        int guess = ( high + low ) / 2;

        System.out.println( "Is your number " + guess + "? Press 0 if yes, press 1 if your number is higher than " +
                "this number or press 2 if your number is lower than this number!" );


        while( true ){

            int ans = x.nextInt();
            if( ans == 0 ){
                break;

            }else if( ans == 1 ){
                low = guess;

            }else if( ans == 2 ){
                high = guess;

            }

            guess = ( high + low ) / 2;
            System.out.printf( "Is your number %d ? (0: yes, 1: higher, 2:lower) ", guess );
        }

        System.out.println( "The number you thought of is " + guess + "! Thanks for playing!" );

    }else{
        System.out.println( "No problem. Restart the program and press 1 when ready!" );
    }
}