1

推理ゲームのプログラムを作ろうとしています。ユーザーが数字を入力すると、その数字が高すぎるか低すぎるかが通知され、もう一度推測するように通知されます。無限ループを作成しましたが、それを変更する方法がわかりません。推測が間違っている場合、プログラムは間違った値をチェックし続け、「間違った番号」メッセージを出力することを認識しています。

package guessinggame;
import java.util.Scanner;
/**
 *
 * @author
 */
public class GuessingGame {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        Scanner input = new Scanner (System.in);

        int guesses;    //number of users guesses

        int housePick;  //number the user must guess

        int guess;      //users guess

        housePick = (int)((Math.random() * 100) +1 );  
        //sets housePick to random number from 1 to 100

        System.out.println("I'm thinking of a number between 1 and 100") ;
        //print "Im thinking of a nubmer between 1 and 100"

        System.out.println("Can you guess what it is?");
        //print "can you guess what it is"

        System.out.println
                ("Enter a number from 1 to 100 (including 1 and 100)");
        //prompt user to enter number

        guess = input.nextInt();
        //save entered number as guess

        while (guess != housePick)  //while guess doesnt = housePick...
        {
            if (guess > housePick)  //and if guess > housePick...
            {
                if ((guess - 10) <= housePick )  
                    //and if guess is 10 numbers away from housePick...

                {
                    System.out.println("Close, but too high. Try again.");
                    //print "close but too high, try again"

                }
                else              //if guess is not close and guess>housePick...
                {
                    System.out.println ("Too high, try again.");  
                    //then print "Too high, Try again"
                }                         
            }
            else  //If guess<housePick
            {
            if ((guess + 10) >= housePick)  //AND if guess is close to housePick
            {
                System.out.println ("close, but too low.") ; 
                //then print "close, but too low"

            }
            else//If guess isnt close to housePick and is less than housePick...
            {
                System.out.println ("Too low.");//then print "too low"
            }

            }

        }



        System.out.println ("You win!  It took you "  + "guesses.");
        //If guess = housePick print "Yout win! It took you (# of guesses)"




    }
}
4

4 に答える 4

1

ユーザーの選択を取得して、while ループ内から推測変数の値を変更することはありません。推測が変更されない場合、これが変更されないため、ループは終了せずwhile (guess != housePick)、条件は false のままです。

解決策:
明白なことを行います: 入力 Scanner 変数を使用して while ループからユーザー入力を取得し、それを使用して推測を新しい値に再設定します。

于 2013-10-02T05:12:55.713 に答える
1

ある程度までは正しいですが、問題が発生した場合は、ループが終了する前にユーザーからの入力を取得する必要があります。そのため、while ループが終了する直前にプレーヤーからの入力を取得する必要があります。次のように修正と更新されたコード (while ループ部分のみ) を行いました。

 while (guess != housePick)  //while guess doesnt = housePick...
    {
        if (guess > housePick)  //and if guess > housePick...
        {
            if ((guess - 10) <= housePick )  
                //and if guess is 10 numbers away from housePick...

            {
                System.out.println("Close, but too high. Try again.");
                //print "close but too high, try again"

            }
            else              //if guess is not close and guess>housePick...
            {
                System.out.println ("Too high, try again.");  
                //then print "Too high, Try again"
            }                         
        }
        else  //If guess<housePick
        {
        if ((guess + 10) >= housePick)  //AND if guess is close to housePick
        {
            System.out.println ("close, but too low.") ; 
            //then print "close, but too low"

        }
        else//If guess isnt close to housePick and is less than housePick...
        {
            System.out.println ("Too low.");//then print "too low"
        }

        }
           /// this is the correction
          System.out.println
            ("Enter a number from 1 to 100 again (including 1 and 100)");
          //prompt user to enter number

           guess = input.nextInt();
    }
于 2013-10-02T05:15:06.600 に答える