2

我正在尝试为我们的主题解决这个任务。我已经在不使用的情况下尝试过它JOptionPane并且它有效,但是当我将它编辑为有一个对话框时,它没有用。第一个对话框应该包含一个标题和要输入的选项,最后一个应该允许用户退出程序。用户输入他/她的选择后,应出现一个对话框,指示用户的选择和计算机的选择。计算机的选择应该是随机的。如果用户没有输入正确的选项,则会出现一个对话框,告诉用户输入有效的选项。这是我到目前为止所拥有的:

package assignment;

import java.util.Random;
import java.util.Scanner;
import javax.swing.*;
public class RockPaperScissorGame 
{



public static void main(String[] args) 
{

    String inputStr;
    String personPlay="";    
    String computerPlay="1,2,3";     
    int computerInt;
   Scanner input = new Scanner(System.in);
    Random generator = new Random();


            inputStr = JOptionPane.showInputDialog("Lets play a game! \nEnter 1 for rock \nEnter 2 for paper \nEnter 3 for scissors \nEnter 4 to quit");
            personPlay = input.next(inputStr);

            switch (computerInt = 0)
            {
                }
            do
            {
                if (personPlay.equals(computerPlay))  
                    JOptionPane.showMessageDialog(null, "It's a TIE! ", "TIE!", JOptionPane.INFORMATION_MESSAGE);

                else if (personPlay.equals("1"))
                    {
                    if (computerPlay.equals("3"))
                    JOptionPane.showMessageDialog(null, "Rock beats Scissors. \nYOU WIN! ", "YOU WIN!", JOptionPane.INFORMATION_MESSAGE);
                    else
                    JOptionPane.showMessageDialog(null, "Paper beats Rock. \nYOU LOSE! ", "YOU LOSE!", JOptionPane.INFORMATION_MESSAGE);
                    }
                else if (personPlay.equals("2"))
                    {
                    if (computerPlay.equals("3"))
                    JOptionPane.showMessageDialog(null, "Scissor beats Paper. \nYOU LOSE!", "YOU LOSE!", JOptionPane.INFORMATION_MESSAGE);
                    else
                    JOptionPane.showMessageDialog(null, "Paper beats Rock. \nYOU WIN! ", "YOU WIN!", JOptionPane.INFORMATION_MESSAGE);
                    }
                else if (personPlay.equals("3"))
                    {
                     if (computerPlay.equals("1"))
                    JOptionPane.showMessageDialog(null, "Scissor beats Paper. \nYOU WIN!", "YOU WIN!", JOptionPane.INFORMATION_MESSAGE);
                     else
                    JOptionPane.showMessageDialog(null, "Rock beats Scissors. \nYOU LOSE! ", "YOU LOSE!", JOptionPane.INFORMATION_MESSAGE);
                    }
                else if (personPlay.equals("4"))
                    {
                    JOptionPane.showMessageDialog(null, "GOOD BYE", " BYE!", JOptionPane.INFORMATION_MESSAGE);
                    }


            }while(true);
            }   

}

希望大家能帮忙。谢谢 :)

4

1 回答 1

2
  • 正如所指出的,你永远不会改变computerPlay,这应该是随机选择的。
  • showInputDialog应该在循环内,否则游戏永远不会结束......
  • switch是没用的。
  • 也是如此Scanner。做就是了:personPlay = JOptionPane.showInputDialog("Lets play a game! [...]");
于 2012-12-14T15:30:23.113 回答