在使用JOptionPane的Java的岩石纸剪刀比赛

时间:2012-12-14 15:17:41

标签: java swing

我正在尝试为我们的主题解决这个任务。我已经尝试了它而没有使用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);
            }   

}

希望你们能提供帮助。谢谢:))

1 个答案:

答案 0 :(得分:2)

  • 正如所指出的,你永远不会改变computerPlay,这应该是随机选择的。
  • showInputDialog应该在循环中,否则游戏永远不会结束......
  • switch没用。
  • Scanner也是如此。只需:personPlay = JOptionPane.showInputDialog("Lets play a game! [...]");