-1

編集 1 - すべての sc.nextInt を Integer.parseInt(sc.nextLine()) に変更しましたが、いくつかのエラーが発生しましたが、エラーは少なくなりました

編集 2 - 現在は実行されていますが、私は物事を 2 倍に印刷しています。ゲームはラウンド 10 の最後に終了せず、最後に合計勝利ラインがありません。エラーは勝利後に発生すると思います。

edit 3 - 2x コールド/ホット印刷が​​修正されました。2x ヘッダーを修正しました (wins メソッドに残っている繰り返し行)。新しい (そしてできれば最後のエラー) - ゲームの最初のラウンドでは 3 回しか試行できず、他のラウンドでは 4 回しか試行できません。サンプルの実行:

I will choose a number between 1 and 10
You have 3 tries to get it right

Round 1

Enter a new guess: 5
Cold
5
Cold
5
You lose!
You have won 0 out of 1 rounds.

Round 2

Enter a new guess: 5
Cold
5
Cold
5
Cold
5
You lose!
You have won 0 out of 2 rounds.

Round 3

Enter a new guess: 5
Cold
5
Cold
5
Cold
5
You lose!
You have won 0 out of 3 rounds.

クラス 1:

import java.util.Scanner;

public class GuessRunner 
{   
public static void main(String[] args) 
{
    Scanner sc = new Scanner(System.in);

    System.out.print("I will choose a number between 1 and 10" + 
                    '\n' + "You have 3 tries to get it right" + '\n');

    GuessCalc game = new GuessCalc();
    while (game.getRounds() <= 10)
    {   
        System.out.println('\n' + "Round " + game.getRounds() + '\n');
        System.out.print("Enter a new guess: ");
        int guess = Integer.parseInt(sc.nextLine());

        do
        {
            game.rounds(guess);
            guess = Integer.parseInt(sc.nextLine());
        } 
        while (game.roundOver == false);

        System.out.println(game.wins());
    }

    sc.close();
}
}

クラス 2:

public class GuessCalc 
{
private int wins;
private int rounds = 1;
private int num = (int) (1 + Math.random() * 10);
private int tries = 0;
public boolean roundOver;

/**
 * Runs the rounds and determines if they win
 * @return outcome the boolean true if they won or false if they lost
 */
public String rounds(int guess)
{       
    if (guess == num)   //player won
    {
        wins++;
        rounds++;
        tries = 0;
        System.out.println("You win!");
        num = (int) (1 + Math.random() * 10);   //new number
        roundOver = true;
    }

    else if (tries == 3)    //out of tries
    {
        rounds++;
        tries = 0;
        System.out.println("You lose!");
        num = (int) (1 + Math.random() * 10);   //new number
        roundOver = true;
    }

    else
    {  
        hotOrCold(guess);
        roundOver = false;   
    }
}

/**
 * Tells the player if they are hot or cold
 */
public void hotOrCold(int guess)
{
    if (guess == num - 1 || guess == num + 1)    //if they are off by 1
        System.out.println("Hot");
    else         // if they are further than 1 away
        System.out.println("Cold");

   tries++;
}

/**
 * Returns the number of wins and makes a new header
 * @return the String with the number of wins and new header
 */
public String wins()
    {return("You have won " + wins + " out of " + (rounds - 1) + " rounds.");}

/**
 * Returns the number of rounds played
 * @return rounds the number of rounds
 */
public int getRounds()
    {return rounds;}
}
4

2 に答える 2

0

追加 sc.nextLine();sc.nextInt();てみてください。その後、改行文字が消費されます

于 2013-11-09T04:05:34.247 に答える
0

スキャナーの問題は、スキャナーが非常に奇妙だということです。私は何年もそれを使用してきましたが、彼女は私が彼女を高速で汚い入力だけに使用することを嫌っています. 起こる奇妙なことについて説明し始めるつもりはありませんが、説明は通常、nextLine() 以外のメソッドが改行文字をどのように処理するか (それらが消費/無視するかどうか) に関するものです。

スキャナーに関する私のアドバイスは、 hasNextLine() および nextLine() メソッドのみを使用することです。それらは私が見つけた唯一の方法であり、それらを使用するすべての人間がメソッドの動作を予測できます。次に、それが数字 (matches("[1-9]+[0-9]*")) であるかどうかを確認するか、単にワイルドで Integer.parseInt() を直接実行できます。

ゲームを見る = new GuessCalc(guess); ループはクラス 1 では奇妙に見えます。ラウンドは常にリセットされるため、これはエラーのように見えます。

編集1:

あなたのコードが、ラウンドごとに乱数をリセットし、ラウンドごとに「試行」カウントをリセットすることを想定していない場合 (コードはラウンドごとに現在のゲームを破棄しました)、以下のコードが役立つ場合があります。

import java.util.Scanner;

public class GuessRunner {
    public static void main(String[] args) throws InterruptedException {

        System.out.print("I will choose a number between 1 and 10" + '\n'
            + "You have 3 tries to get it right" + '\n');

        GuessCalc game = new GuessCalc();
        while (game.getRounds() <= 10) {

            game.playRound();
            System.out.println(game.wins());

        }

}
}

セカンドクラス:

import java.util.Scanner;

public class GuessCalc {
    private int wins, rounds = 0, num = (int) (1 + Math.random() * 10);
    private int tries = 0;
    private Scanner sc=new Scanner(System.in);

    /**
    * Constructs the game
    * 
    * @param guess
    *            the player's guess
    */
    public GuessCalc() {

    }

    /**
    * Runs the rounds and determines if they win
    * 
    * @return outcome if they won (true) or lost (false);
    */
    public boolean playRound() {
        startNewRound();
        System.out.printf("Round %d \n\n", this.getRounds());

        while(true){
            System.out.println("Enter a new guess: ");

            int guess = Integer.parseInt(sc.nextLine());

            printHotOrCold(guess);

            if (guess == num) {
                wins++;

                System.out.println("Jackpot! Setting a new random number");

                return true;
            }else if(tries==3){
                System.out.println("Oops, didn't succeed. Better luck next time. The number was "+num);

                return false;//ran out of tries
            }else{
                //got it wrong but still has guesses left
            }
        }

    }

    public final void startNewRound() {
         rounds++;
         tries = 0;
         num = (int) (1 + Math.random() * 10);// set a new random number    

    }

    /**
    * Tells the player if they are hot or cold
    */
    public void printHotOrCold(int guess) {

        int offBy = guess - num;

        if (offBy == 1 || offBy == -1) {// if they are over or under by 1
            System.out.println("Hot");
        } else if (guess != num) {// if they are further than 1 away
             System.out.println("Cold");
        }

        tries++;

    }

    /**
    * Returns the number of wins and makes a new header
    * 
    * @return the String with the number of wins and new header
    */
    public String wins() {
        String record = String.format("You have won %d out of %d rounds. \n\n",wins,rounds);
        return record;
    }

    /**
    * Returns the number of rounds played
    * 
    * @return rounds the number of rounds
    */
    public int getRounds() {
        return rounds;
    }

}

あなたのコードは不正な形式 (コンパイルできませんでした) であり、私はあなたの意図を 100% 知りません (例: できるだけ多くの乱数を正しく取得するために 10 ラウンドであるか、10 ラウンドで 3 つの推測があるかは不明です)。幸運を。

于 2013-11-09T03:52:28.283 に答える