0

そのため、乱数生成器を作成して、生成された数が正しくなるまでユーザーに推測させようとしているときに、いくつかの問題に遭遇しました。

もう一度プレイするオプションを追加しようとするまで、すべてが正常に機能していました。問題は、彼らが間違った数を推測した場合、推測が高すぎたり、低すぎたり、または無効な入力であった場合に、同じエラーメッセージを際限なく繰り返し続けることです。数字が最初に正しく推測された場合、すべてが正常に機能し、もう一度再生するように求められ、完全に機能します.

問題は、推測を開始するときに 2 つの for ループを使用したことです。

「このプログラムは、0 から 100 までの乱数を生成します。これを推測する必要があります。」

間違った答えを推測するたびに出力されるようにするには、もう一度再生するオプションを選択したときにのみ出力されるようにしたいため、ネストされた for ループを作成しました。しかし、問題は、同じ答えを表示する最初の間違った推測の後に、内側の for ループが無限のサイクルに入ることです。

import javax.swing.JOptionPane;
import java.util.Random;
import javax.swing.UIManager;
import java.awt.*;

public class RandomNumberGuesser{
    public static void main(String[] args){
        UIManager m1=new UIManager();
        Color g = Color.gray;
        Color lg = g.brighter();
        m1.put("OptionPane.background", lg);
        m1.put("Panel.background", lg);

        int x;
        for(x = 1; true; x++){
        Random random = new Random();
        int randomNumber = random.nextInt(100);
        System.out.println(randomNumber);
        JOptionPane.showMessageDialog(null,
            "This program will generate a random number from 0 to 100 which you have to guess.",
            "Number Guesser",
            JOptionPane.INFORMATION_MESSAGE);
            String guess = JOptionPane.showInputDialog(null,
                "Guess a number.",
                "Guess",
                JOptionPane.QUESTION_MESSAGE);
                if(guess == null){
                    System.out.println("The user has terminated the program");
                    System.exit(0);
                    }
            int guess1 = Integer.parseInt(guess);

            int y;
            for(y = 1; true; y++){
                if(guess1 > 100 || guess1 < 0)
                JOptionPane.showMessageDialog(null,
                    "Guess is out of range!\nPlease enter valid input.",
                    "Invalid Input",
                    JOptionPane.WARNING_MESSAGE);

            else if(randomNumber > guess1)
                JOptionPane.showMessageDialog(null,
                    "You guessed too low.\nGuess again!",
                    "Your guess",
                    JOptionPane.INFORMATION_MESSAGE);

            else if(randomNumber < guess1)
                JOptionPane.showMessageDialog(null,
                    "You guessed too high.\nGuess again!",
                    "Your guess",
                    JOptionPane.INFORMATION_MESSAGE);

            else{
                JOptionPane.showMessageDialog(null,
                    "You guessed the number right!\nIt took you "+y+" attempt(s) to guess it.",
                    "Congratulations!",
                    JOptionPane.INFORMATION_MESSAGE);
                if (JOptionPane.showConfirmDialog(null, "Want to play again?", "Play again?",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
                    System.out.println("Play again soon!");
                    System.exit(0);
                }
                else{
                    y = 0;
                    break;
                    }
                }
            }
        }
    }
}
4

3 に答える 3

0

これを試すことができます:

public class RandomNumberGuesser {
public static void main(String[] args) {
    // UIManager m1=new UIManager();
    Color g = Color.gray;
    Color lg = g.brighter();
    UIManager.put("OptionPane.background", lg);
    UIManager.put("Panel.background", lg);

    Random random = new Random();
    int attempts = 0; // Number of attempts

    int randomNumber = random.nextInt(100);
    System.out.println(randomNumber);

    // This outside the loop so is showed just ONE time
    JOptionPane.showMessageDialog(null,
                    "This program will generate a random number from 0 to 100 which you have to guess.", "Number Guesser", JOptionPane.INFORMATION_MESSAGE);

    while (true) {

        attempts++;

        String guess = JOptionPane.showInputDialog(null, "Guess a number.",
                "Guess", JOptionPane.QUESTION_MESSAGE);
        if (guess == null) {
            System.out.println("The user has terminated the program");
            System.exit(0);
        }
        int guess1 = Integer.parseInt(guess);

            if (guess1 > 100 || guess1 < 0)
                JOptionPane
                        .showMessageDialog(
                                null,
                                "Guess is out of range!\nPlease enter valid input.",
                                "Invalid Input",
                                JOptionPane.WARNING_MESSAGE);

            else if (randomNumber > guess1)
                JOptionPane.showMessageDialog(null,
                        "You guessed too low.\nGuess again!", "Your guess",
                        JOptionPane.INFORMATION_MESSAGE);

            else if (randomNumber < guess1)
                JOptionPane.showMessageDialog(null,
                        "You guessed too high.\nGuess again!",
                        "Your guess", JOptionPane.INFORMATION_MESSAGE);

            else {
                JOptionPane
                        .showMessageDialog(null,
                                "You guessed the number right!\nIt took you "
                                        + attempts + " attempt(s) to guess it.",
                                "Congratulations!",
                                JOptionPane.INFORMATION_MESSAGE);
                if (JOptionPane.showConfirmDialog(null,
                        "Want to play again?", "Play again?",
                        JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
                    System.out.println("Play again soon!");
                    System.exit(0);
                } else {
                    randomNumber = random.nextInt(100);
                    System.out.println(randomNumber);
                    attempts = 0;
                }
            }
        }
    }
}
于 2013-10-27T22:47:47.003 に答える
0

正しい数が推測されない場合、無限ループに陥りました。コードにいくつかの変更を加えました。これが役立つかどうかを確認してください。

    package com.ananth.stackoverflow.help;

import java.awt.Color;
import java.util.Random;

import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class RandomNumberGuesser {
    public static void main(String[] args) {
        UIManager m1 = new UIManager();
        Color g = Color.gray;
        Color lg = g.brighter();
        m1.put("OptionPane.background", lg);
        m1.put("Panel.background", lg);

        int x;
        for (x = 1; true; x++) {
            Random random = new Random();
            int randomNumber = random.nextInt(100);
            System.out.println(randomNumber);
            JOptionPane.showMessageDialog(null,
                    "This program will generate a random number from 0 to 100 which you have to guess.", "Number Guesser",
                    JOptionPane.INFORMATION_MESSAGE);
            String guess = getInputFromUser();
            int guess1 = Integer.parseInt(guess);

            int y;
            for (y = 1; true; y++) {
                if (guess1 > 100 || guess1 < 0) {
                    JOptionPane.showMessageDialog(null, "Guess is out of range!\nPlease enter valid input.",
                            "Invalid Input", JOptionPane.WARNING_MESSAGE);
                } else if (randomNumber == guess1) {
                    JOptionPane.showMessageDialog(null, "You guessed the number right!\nIt took you " + y
                            + " attempt(s) to guess it.", "Congratulations!", JOptionPane.INFORMATION_MESSAGE);
                    if (JOptionPane.showConfirmDialog(null, "Want to play again?", "Play again?", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
                        System.out.println("Play again soon!");
                        System.exit(0);
                    } else {
                        y = 0;
                        break;
                    }
                } else if (randomNumber > guess1) {
                    JOptionPane.showMessageDialog(null, "You guessed too low.\nGuess again!", "Your guess",
                            JOptionPane.INFORMATION_MESSAGE);
                    guess = getInputFromUser();
                    guess1 = Integer.parseInt(guess);
                } else if (randomNumber < guess1) {
                    JOptionPane.showMessageDialog(null, "You guessed too high.\nGuess again!", "Your guess",
                            JOptionPane.INFORMATION_MESSAGE);
                    guess = getInputFromUser();
                    guess1 = Integer.parseInt(guess);
                }
            }
        }
    }

    private static String getInputFromUser() {
        String guess = "";
        guess = JOptionPane.showInputDialog(null, "Guess a number.", "Guess", JOptionPane.QUESTION_MESSAGE);
        if (guess == null) {
            System.out.println("The user has terminated the program");
            System.exit(0);
        }
        return guess;
    }
}
于 2013-10-27T22:50:01.370 に答える