猜猜游戏错误(主要是扫描仪)

时间:2013-11-09 03:25:55

标签: java runtime-error

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

第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;}
}

2 个答案:

答案 0 :(得分:0)

Scanner的问题是Scanner非常非常奇怪。我已经使用它多年了,她讨厌我只使用她进行快速和肮脏的输入。我甚至不想开始描述发生的奇怪事情,但解释通常是关于非nextLine()方法如何处理换行符(无论它们是否消耗/忽略它们)

我对扫描仪的建议是只使用其hasNextLine()和nextLine()方法。它们是我发现的每个人使用它们的唯一方法可以预测方法的行为。然后你可以检查它是否是一个数字(匹配(“[1-9] + [0-9] *”))或者只是狂野并直接进行Integer.parseInt()。

看到     game = 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次猜测,则不知道)。祝你好运。

答案 1 :(得分:0)

尝试添加 sc.nextLine();之后的sc.nextInt();消耗新行字符