0

这是我的代码:

import java.util.Scanner;
import static java.lang.System.*;

public class GuessingGame
{
private int upperBound;
private int count, guess, num, pct;

public GuessingGame(int stop)
{
    upperBound = stop;
}

public void setNum(int stop)
{
    upperBound = stop;
}

public void playGame()
{
    int count = 0;
    int attempt = 1;
    Scanner keyboard = new Scanner(System.in);
    //upperBound = keyboard.nextInt();
     num = (int)Math.random()*upperBound;
     guess = 0;
     out.println("Enter a number between 1 and " + upperBound);
        guess = keyboard.nextInt();
        count++;
        if(guess != num)
            attempt++;
    do{
        out.println("Enter a number between 1 and " + upperBound);
        guess = keyboard.nextInt();
        count++;
        if(guess != num)
            attempt++;
    }while(guess != num);
     pct = (count/attempt)*100;



}

public String toString()
{
    String output="";
    output = "It took you " + count + " tries to guess " + num + "\n you guessed wrong " + pct + "% of the time";
    return output;
}
}

我知道它必须在某些时候guess 必须等于 num 但代码永远不会结束当前的“游戏”但是当我使用我的示例 5 作为 stop/upperBound 时它似乎是无限循环的

这是我要求的跑步者课程:

import java.util.Scanner;
import static java.lang.System.*;

public class Lab10e
{
public static void main(String args[])
{
    Scanner keyboard = new Scanner(System.in);
    char response = ' ';

    out.print("Guessing Game - how many numbers? ");

    //read in the player value
    int stop = keyboard.nextInt();

    GuessingGame game = new GuessingGame(stop);
    game.playGame();
    out.println(game);
    out.println("would you like to play again? (y/n):: ");
    String resp =  keyboard.next();
    response = resp.charAt(0);

    do {
    out.print("Guessing Game - how many numbers? ");
    stop = keyboard.nextInt();
    game.setNum(stop);
    game.playGame();
    out.println(game);
    out.println();
    out.println("would you like to play again? (y/n):: ");
    resp =  keyboard.next();
    response = resp.charAt(0);
    //



}while(response == 'y'); 

}

}
4

4 回答 4

2

您在这里有两个问题:首先,您的随机数将始终为零。换行

num = (int)Math.random()*upperBound;

num = (int)(Math.random()*upperBound);

你的第二个问题是,即使你在第一次尝试时猜对了,它总是会问你两次。这主要源于您复制并粘贴了您的猜测代码。如果您改为从代码中删除这些行,则不会发生这种情况(不是do循环内的行):

 out.println("Enter a number between 1 and " + upperBound);
    guess = keyboard.nextInt();
    count++;
    if(guess != num)
        attempt++;

此外,由于循环终止的方式,您不需要单独的countattempt变量。您始终可以预测attempt变量将是什么(比count...大一,实际上,在您的程序中,它会大二,但这不是正确的猜测百分比)。您可以完全删除该attempt变量,而是执行

double countDouble = (double) count;
pct = (int) ((countDouble/(countDouble+1))*100.0);
于 2012-11-08T03:31:50.597 回答
1

当你这样做时:

(int)Math.random()*upperBound;

它将 Math.random() 转换为 a int,由于它是四舍五入的,它总是为零。因此 num 将始终等于 0。

于 2012-11-08T03:25:17.093 回答
1

你需要

(int)(Math.random()*upperBound)
于 2012-11-08T03:26:26.837 回答
0

从您的代码中out.println("Enter a number between 1 and " + upperBound);,我看到您想在 range 中创造num价值,[1; upperBound]但实际上是在 range中创造价值[0, upperBound-1]

如果要从 1 到 N 随机数,请使用(int) (Math.random()*N)+1. 由于Math.random()将返回范围 [0; 1)(没有 1)Math.random()*N总是会返回double范围内的值[0; N)(没有 N),但是由于我们将它转int​​换为 range 将更改为[0; N-1],所以要使它成为[1; N]你需要添加1到返回值。

于 2012-11-08T04:01:18.117 回答