代码永远不会终止

时间:2012-11-08 03:19:43

标签: java loops while-loop do-while

这是我的代码:

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

我知道它必须在某些时候猜测必须等于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 个答案:

答案 0 :(得分: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);

答案 1 :(得分:1)

执行此操作时:

(int)Math.random()*upperBound;

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

答案 2 :(得分:1)

你需要

(int)(Math.random()*upperBound)

答案 3 :(得分:0)

从您的代码out.println("Enter a number between 1 and " + upperBound);我看到您希望num范围内的[1; upperBound]值,但实际上是在[0, upperBound-1]范围内。{/ p>

如果您想要从1到N的随机数,请使用(int) (Math.random()*N)+1。由于Math.random()将返回范围[0; 1)(没有1)Math.random()*N总是会返回范围double中的[0; N)值(没有N)但是因为我们将其转换为int范围将更改为{{1} },为了使其[0; N-1],您需要将[1; N]添加到返回的值。