0

Random().nextInt() 関数を使用して乱数を取得しようとしています。それが正しいのか、それとも私のロジックがめちゃくちゃなのか、正しく読み取れていないのかはわかりません。また、 for ループは、想定どおりに機能していません。助けていただけますか?先月のように Java を始めたので、授業の課題を終わらせようとしています。

//DECLARE VARIABLES
    String rounds, userChooses;
    Random computerChooses = new Random();
    int round = 1;
    int userChoice = 0;
    final int ONE = 1;
    final int TWO = 2;
    final int THREE = 3;
    int computerChoice = computerChooses.nextInt(3) + 1;

    //ASK USER FOR NUMBER OF ROUNDS
    rounds = JOptionPane.showInputDialog(null, "How many rounds do you want to play?");
    round = Integer.parseInt(rounds);

    //TRACK NUMBER OF ROUNDS
    for (int x = 1; x <= round; x++) {
        JOptionPane.showMessageDialog(null, "This is round " + x + ".");

    //CREATE THE INPUT FOR THE USER
    try {

    //START GAME
    userChooses = JOptionPane.showInputDialog(null, "Enter 1)rock, 2)paper, or 3)scissors!");
    userChoice = Integer.parseInt(userChooses);
    if (userChoice > 3 || userChoice < 1) {
        throw new Exception();
    }

    } catch (Exception ex) {
        JOptionPane.showInputDialog(null, "That wasn't a number!", "Error!", JOptionPane.ERROR_MESSAGE);
        JOptionPane.showMessageDialog(null, "You have not entered correct number! Terminating program!");
        System.exit(0);
    }
    if (userChoice == ONE) {
        if (computerChoice == ONE) {
            JOptionPane.showMessageDialog(null, "You tied the computer.\nYou chose: rock\nComputer chose: rock");
        } else if (computerChoice == TWO) {
            JOptionPane.showMessageDialog(null, "You lost!\nYou chose: rock\nComputer chose: paper");
        } else if (computerChoice == THREE){
            JOptionPane.showMessageDialog(null, "You won!\nYou chose: rock\nComputer chose: scissors");
        }
    } else if (userChoice == TWO) {
        if (computerChoice == ONE) {
            JOptionPane.showMessageDialog(null, "You won!\nYou chose: paper\nComputer chose: rock");
        } else if (userChoice == TWO) {
            JOptionPane.showMessageDialog(null, "You tied!\nYou chose: paper\nComputer chose: paper");
        } else if (userChoice == THREE) {
            JOptionPane.showMessageDialog(null, "You lost!\nYou chose: paper\nComputer chose: scissors");
        }
    } else if (userChoice == THREE) {
        if (computerChoice == ONE) {
            JOptionPane.showMessageDialog(null, "You lost!\nYou chose: scissors\nComputer chose: rock");
        } else if (computerChoice == TWO) {
            JOptionPane.showMessageDialog(null, "You won!\nYou chose: scissors\nComputer chose: paper");
        } else if (computerChoice == THREE) {
            JOptionPane.showMessageDialog(null, "You tied!\nYou chose: scissors\nComputer chose: scissors");
        }
    }
    }

}

何かを選択するたびに、コンピューターは常に岩のオプションを選択すると表示されます。何か案が?

4

1 に答える 1

4

Randomオブジェクトを使用するだけで:

new Random().nextInt(3) + 1;

-loopに関してforは、閉じ括弧はcatch Exception-block の後ではなく後に配置する必要があります。JOptionPane.showMessageDialog(null, "This is round " + round + ".");

よりうまくいくように見えるものは次のとおりです。

import java.util.Random;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class RockPaperScissors {
    private static final int ROCK = 1;
    private static final int PAPER = 2;
    private static final int SCISSORS = 3;

    private Random computerChooses = new Random();

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new RockPaperScissors().play();
            }
        });
    }

    protected void play() {
        String rounds;
        String userChooses;
        int round = 0;
        int userChoice;

        // CREATE THE INPUT FOR THE USER
        try {
            rounds = JOptionPane.showInputDialog(null, "How many rounds do you want to play?");
            round = Integer.parseInt(rounds);

            // CREATE COUNTER
            for (int x = 0; x < round; x++) {
                int computerChoice = computerChooses.nextInt(3) + 1;
                JOptionPane.showMessageDialog(null, "This is round " + x + ".");

                // START GAME
                userChooses = JOptionPane.showInputDialog(null, "Enter 1)rock, 2)paper, or 3)scissors!");
                userChoice = Integer.parseInt(userChooses);

                String message = null;
                switch (userChoice) {
                case ROCK:
                    switch (computerChoice) {
                    case ROCK:
                        message = "You tied computer. You both chose rock";
                        break;
                    case PAPER:
                        message = "You win.";
                        break;
                    case SCISSORS:
                        message = "You loose";
                        break;

                    }
                    break;
                case PAPER:
                    switch (computerChoice) {
                    case ROCK:
                        message = "You win.";
                        break;
                    case PAPER:
                        message = "You tied computer. You both chose paper";
                        break;
                    case SCISSORS:
                        message = "You loose";
                        break;

                    }
                    break;
                case SCISSORS:
                    switch (computerChoice) {
                    case ROCK:
                        message = "You loose";
                        break;
                    case PAPER:
                        message = "You win";
                        break;
                    case SCISSORS:
                        message = "You tied computer. You both chose scissors";
                        break;

                    }
                    break;

                }
                JOptionPane.showMessageDialog(null, message);
            }
        } catch (Exception ex) {
            JOptionPane.showInputDialog(null, "That wasn't a number!", "Error!", JOptionPane.ERROR_MESSAGE);
            JOptionPane.showMessageDialog(null, "You have not entered correct number! Terminating program!");
            System.exit(0);
        }
    }
}
于 2013-01-25T17:59:05.917 に答える