Sy1_ 2021-12-10 08:09 采纳率: 50%
浏览 46
已结题

要求用java做一个石头剪子布的游戏. 这是我现在写的, 请问一下要怎么输出输赢? 我现在这个石头剪刀布后没有任何后续了

public class FirstRPSGame {
public static void main(String args[]) throws Exception {
//Variable declaration
int yourChoice, computerChoice;
String yourInput = "";

  //Welcome screen and show the basic rule to the user
  JOptionPane.showMessageDialog(null, "Welcome to Paper, Scissor and Rock");
  JOptionPane.showMessageDialog(null, "Let me remind you the rule first:\nScissor cuts paper, paper covers rock, and rock breaks scissors.");
  JOptionPane.showMessageDialog(null, "0: Rock\n1: Paper\n2: Scissor");

  //Get your choice
  try {
      yourInput = JOptionPane.showInputDialog("Now tell me your choice!");
      yourChoice = Integer.parseInt(yourInput);
      
      if("0".equals(yourInput) || "1".equals(yourInput) || "2".equals(yourInput)) {
              
      }else {
          throw new MyException("Please enter choice 0-2");
      }
  }
  catch(MyException e) {
      System.out.println(e);
  }             
  
  //computer choice
  computerChoice = (int)(Math.random()*10); 
  computerChoice %= 3; 
  
  //Compare your Choice with computer’s choice and output the result
  //Case I: Both you and computer pick the same choice, then it’s a tie!
  if ("0".equals(yourInput) && "0".equals(computerChoice)){
      JOptionPane.showMessageDialog(null, "It’s a tie!"); 
  }else if("1".equals(yourInput) && "1".equals(computerChoice)) {
      JOptionPane.showMessageDialog(null, "It’s a tie!"); 
  }else if("2".equals(yourInput) && "2".equals(computerChoice)) {
      JOptionPane.showMessageDialog(null, "It’s a tie!"); 
  }

  //Case II: You WIN :)
  if ("0".equals(yourInput) && "2".equals(computerChoice)) {
      JOptionPane.showMessageDialog(null, "You win!");
  }else if("1".equals(yourInput) && "0".equals(computerChoice)){
      JOptionPane.showMessageDialog(null, "You win!");
  }else if("2".equals(yourInput) && "1".equals(computerChoice)) {
      JOptionPane.showMessageDialog(null, "You win!");
  }     
  
  //Case III: You lose :
  if ("0".equals(computerChoice) && "2".equals(yourInput)) {
      JOptionPane.showMessageDialog(null, "You lose!");
  }else if("1".equals(computerChoice) && "0".equals(yourInput)) {
      JOptionPane.showMessageDialog(null, "You lose!");
  }else if("2".equals(computerChoice) && "1".equals(yourInput)) {
      JOptionPane.showMessageDialog(null, "You lose!");
  }

}
}

img


img

  • 写回答

3条回答 默认 最新

  • CSDN专家-sinJack 2021-12-10 08:15
    关注

    computerChoice是int类型,直接用==比较

    import javax.swing.*;
    
    public class FirstRPSGame {
        public static void main(String args[]) throws Exception {
    //Variable declaration
            int yourChoice, computerChoice;
            String yourInput = "";
    
            //Welcome screen and show the basic rule to the user
            JOptionPane.showMessageDialog(null, "Welcome to Paper, Scissor and Rock");
            JOptionPane.showMessageDialog(null, "Let me remind you the rule first:\nScissor cuts paper, paper covers rock, and rock breaks scissors.");
            JOptionPane.showMessageDialog(null, "0: Rock\n1: Paper\n2: Scissor");
    
            //Get your choice
            try {
                yourInput = JOptionPane.showInputDialog("Now tell me your choice!");
                yourChoice = Integer.parseInt(yourInput);
                System.out.println("你的选择:"+yourInput+"  "+yourChoice);
                if("0".equals(yourInput) || "1".equals(yourInput) || "2".equals(yourInput)) {
    
                }else {
                    throw new MyException("Please enter choice 0-2");
                }
            }
            catch(Exception e) {
                System.out.println(e);
            }
    
            //computer choice
            computerChoice = (int)(Math.random()*10);
            computerChoice %= 3;
            System.out.println(computerChoice);
            if ("0".equals(yourInput) && 0==computerChoice){
                JOptionPane.showMessageDialog(null, "It’s a tie!");
            }else if("1".equals(yourInput) && 1==computerChoice) {
                JOptionPane.showMessageDialog(null, "It’s a tie!");
            }else if("2".equals(yourInput) && 2==computerChoice) {
                JOptionPane.showMessageDialog(null, "It’s a tie!");
            }
    
            //Case II: You WIN :)
            if ("0".equals(yourInput) && 2==computerChoice) {
                JOptionPane.showMessageDialog(null, "You win!");
            }else if("1".equals(yourInput) && 0==computerChoice){
                JOptionPane.showMessageDialog(null, "You win!");
            }else if("2".equals(yourInput) && 1==computerChoice) {
                JOptionPane.showMessageDialog(null, "You win!");
            }
    
            //Case III: You lose :
            if (0==computerChoice && "2".equals(yourInput)) {
                JOptionPane.showMessageDialog(null, "You lose!");
            }else if(1==computerChoice && "0".equals(yourInput)) {
                JOptionPane.showMessageDialog(null, "You lose!");
            }else if(2==computerChoice && "1".equals(yourInput)) {
                JOptionPane.showMessageDialog(null, "You lose!");
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 12月19日
  • 已采纳回答 12月11日
  • 修改了问题 12月10日
  • 创建了问题 12月10日

悬赏问题

  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan