-1

编辑 1 - 我将所有 sc.nextInt 更改为 Integer.parseInt(sc.nextLine()) 我现在有一些错误但更少

编辑 2 - 它现在运行,但我的东西打印了 2x,游戏在第 10 轮结束时没有结束,我在最后错过了总胜线。我认为错误发生在获胜后。

编辑 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.

第一类:

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

Scanner 的问题是 Scanner 非常非常奇怪。我已经用了很多年了,她讨厌我只用她来做快速而肮脏的输入。我什至不想开始描述发生的奇怪的事情,但解释通常是关于 non-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 回答
0

尝试添加 sc.nextLine();之后 sc.nextInt();将消耗换行符

于 2013-11-09T04:05:34.247 回答