1

我必须创建一个程序,要求输入 1 到 10 之间的数字。生成一个 1 到 10 之间的随机数,如果我猜对了我的猜测数字并显示了密码,它应该输出。此外,如果我猜测的密码太高或太低,它应该输出。我必须joptionpane在主类中使用在可实例化类中完成的计算和比较。我也必须使用elseand if-statements。我已经编写了代码,但是无论我选择什么数字,它都会输出 0 作为猜测的数字,它还告诉我即使我猜错了,我也猜对了。这是我写的两组代码。

import javax.swing.JOptionPane;

public class GuessApp{

public static void main(String args[]){
    int guessNum, secretNum, correct, tooHigh, tooLow;

    Guess myGuess;

    myGuess = new Guess();

    guessNum = Integer.parseInt(JOptionPane.showInputDialog(null,"Please enter a number between 1 and 10"));

    myGuess.setGN(guessNum);
    myGuess.compute();
    guessNum = myGuess.getGuessNum();
    secretNum = myGuess.getSecretNum();
    tooHigh = myGuess.getTooHigh();
    tooLow = myGuess.getTooLow();
    correct = myGuess.getCorrect();

    if (guessNum==correct){
        JOptionPane.showMessageDialog(null,"Congratulations your number is"+guessNum+"and the secret number is"+secretNum+"you have won the game");
    }
    else if (tooHigh==guessNum){
        JOptionPane.showMessageDialog(null,"I'm sorry you have guessed too high, your number is"+guessNum+"and the secret number is"+secretNum);
    }
    else if (tooLow==guessNum){
    JOptionPane.showMessageDialog(null,"I'm sorry you guessed too low, your number is"+guessNum+"and the secret number is"+secretNum);
        }
    }

}

public class Guess{

   private int guessNum, correct, tooHigh, tooLow, secretNum;

   public Guess(){
       guessNum = 0;
   }

   public void setGN(int guessNum){
   this.guessNum = guessNum;
   }

   public void setSN(int secretNum){
       this.secretNum = secretNum;
   }

   public void setCT(int correct){
   this.correct = correct;
   }

   public void setTH(int tooHigh){
   this.tooHigh = tooHigh;
   }

   public void setTL(int tooLow){
   this.tooLow = tooLow;
   }

public void compute(){
    guessNum = guessNum;
    secretNum = (int)(Math.random()*((10 - 1) +1)+1);
    if ((secretNum<guessNum)){
        guessNum = tooHigh;
    }
    else if ((secretNum>guessNum)){
        guessNum = tooLow;
    }
    else if ((secretNum==guessNum)){
        guessNum = correct;
    }
}

    public int getGuessNum(){
    return guessNum;
    }
    public int getSecretNum(){
        return secretNum;
    }
    public int getTooHigh(){
        return tooHigh;
    }
    public int getTooLow(){
        return tooLow;
    }
    public int getCorrect(){
        return correct;
    }
}

我刚刚开始了一个 java 类,所以我是新手。任何帮助将不胜感激。提前致谢。

4

3 回答 3

0

您没有在 Guess 类中设置字段(guessNum、正确、tooHigh 等),因此在计算中您基本上总是将guessNum 设置为0。然后您说getCorrect 返回0,然后将0 与0 进行比较。您确实设置了secretNum为随机值。所以你总是会遇到这样的情况,guessNum 和 correct 都返回 0,但 secretNum 是一个随机数。

如果不自己设置 int 字段(由类“拥有”而不是在方法范围内的变量),它们会自动初始化为 0。方法中的原始类型不会自动初始化,但字段的规则不同。原始数字数组还将所有元素初始化为 0,无论它们是字段还是方法。

我建议简化您的 Guess 课程(如果出于某种原因需要课程)。这是一个非常简化的版本:

public class GuessMySecret {
    private int secret = (int)(Math.random() * 10) + 1;

    // constructors can be omitted if they don't have to do anything

    public boolean isTooLow(int guess) {
        return guess < secret;
    }
    public boolean isTooHigh(int guess) {
        return guess > secret;
    }
}

// in main

int yourGuess = /* get your number */ ;

GuessMySecret mySecret = new GuessMySecret();

if (mySecret.isTooLow(yourGuess)) {
    // respond
} else if (mySecret.isTooHigh(yourGuess)) {
    // respond
} else {
    // presume to be correct
}

但是,如果您想使用此模型进行多轮循环,则必须每次都创建一个新对象,或者让该类能够生成一个新数字。

于 2013-11-07T03:09:39.633 回答
0

int 初始化为 0。在您的计算方法中,无论将什么guessnum 设置为 0,因为它将是这三件事之一。然后第一个 if 将始终为真,因为此时的所有内容都设置为 0。我建议稍微重写它并使用太高太低和正确的布尔值而不是整数。这将使您更清楚地找到答案。

public void compute(){
    guessNum = guessNum;
    secretNum = (int)(Math.random()*((10 - 1) +1)+1);
    if ((secretNum<guessNum)){
        tooHigh = true;
    }
    else if ((secretNum>guessNum)){
        tooLow = true;
    }
    else if ((secretNum==guessNum)){
        correct = true;
    }
}
于 2013-11-07T02:52:03.990 回答
0

这里有一些提示。

  1. 您应该首先生成一个介于 1 和 10 之间的随机数。

  2. 填充输入的猜测数

  3. 将输入的猜测数与随机数进行比较。如果它们相等,则显示祝贺信息。

如果他们不相等。然后将猜测数与最高数和最低数进行比较。而且我认为 tooHigh 和 tooLow 应该被声明为布尔类型。

4.使用步骤#3中填充的布尔标志,您可以知道数字是否正确或过高或过低。

在您的代码中,tooHigh、tooLow 等的值是多少。它们始终为零。

在 Guess 类中更改方法计算中的一些逻辑。然后试一试。

下面是一个例子:

猜类

public class Guess {

private int secretNum = 0;

public Guess() {
    generateSecretNum();
}

/**
 * generate a secret number
 */
public void generateSecretNum() {
    secretNum = (int) (Math.random() * ((10 - 1) + 1) + 1);
}

public boolean isTooHigh(int guessNum) {
    return guessNum > secretNum;
}

public boolean isTooLow(int guessNum) {
    return guessNum < secretNum;
}

/**
 * @return the secretNum
 */
public int getSecretNum() {
    return secretNum;
}

/**
 * @param secretNum
 *            the secretNum to set
 */
public void setSecretNum(int secretNum) {
    this.secretNum = secretNum;
}

}

GuessApp 类

import javax.swing.JOptionPane;

public class GuessApp {

public static void main(String args[]) {

    // Store the value user entered
    int guessNum = 0;

    // When an instance of Guess is created. A secret number is generated as
    // well.
    Guess myGuess = new Guess();

    guessNum = Integer.parseInt(JOptionPane.showInputDialog(null,
            "Please enter a number between 1 and 10"));

    if (myGuess.isTooHigh(guessNum)) {
        // Too High
        JOptionPane.showMessageDialog(null,
                "I'm sorry you have guessed too high, your number is"
                        + guessNum + "and the secret number is " 
                        + myGuess.getSecretNum());

    } else if (myGuess.isTooLow(guessNum)) {
        //Too Low
        JOptionPane.showMessageDialog(null,
                "I'm sorry you guessed too low, your number is" + guessNum
                        + "and the secret number is "
                        + myGuess.getSecretNum());
    } else {
        //Equal to secret number
        JOptionPane.showMessageDialog(null,
                "Congratulations your number is" + guessNum
                        + "and the secret number is "
                        + myGuess.getSecretNum() + "you have won the game");
    }

}

}
于 2013-11-07T02:57:36.343 回答