簡體   English   中英

始終帶有joptionpane的Java數字猜謎游戲說我的輸入為0,即使錯誤我也贏了?

[英]java number guessing game with joptionpane allways says my input is 0 and i win even if its wrong?

我必須創建一個程序,要求輸入1到10之間的數字。會生成一個1到10之間的隨機數,如果我正確猜出了我的猜出的數字和顯示的秘密數字,則應該輸出該數字。 另外,如果我對顯示的機密號碼猜得太高或太低,都應該輸出。 我必須在主類中使用joptionpane並在可實例化的類中進行計算和比較。 我也必須使用elseif-statement 我已經編寫了代碼,但是無論選擇什么數字,它都會輸出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類,所以我是新手。 任何幫助將不勝感激。 提前致謝。

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

這里有一些提示。

  1. 您應該首先生成一個介於1到10之間的隨機數。

  2. 填寫輸入的猜測數字

  3. 將輸入的猜測數字與隨機數字進行比較。 如果它們相等,則顯示祝賀信息。

如果它們不相等。 然后將猜測數字與最高數字和最低數字進行比較。 而且我認為tooHigh和tooLow shuold被聲明為布爾類型。

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

}

}

您沒有在Guess類中設置字段(guessNum,correct,tooHigh等),因此在計算內部,您基本上總是將guessNum設置為0。然后說getCorrect返回0,然后將0與0進行比較。隨機值。 因此,您總會得到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
}

但是,如果您想使用該模型進行多個回合,則必須每次都創建一個新對象,或者使該類能夠生成新的數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM