Elsa镇魂女孩 2019-02-21 07:18 采纳率: 62.5%
浏览 917
已采纳

超级小白,提问 剪刀石头布 三局游戏

剪刀石头布游戏,可以做到判定一局游戏的胜负,
若是想要玩三局 甚至五局,加 for(int i=1;i<=3; i++)
但总是达不到目的。求教各位大神,这个for循环加入到那里合适?

import java.util.Scanner;
import java.util.Random;

//剪刀,石头,布

        public static void main(String[] args) {
        int count = 0; //玩游戏的初始值
        int computerScore = 0; //计算机得分初始值
        int humanScore = 0; //人类得分初始值

        Scanner s = new Scanner(System.in); 
        Random r = new Random();//计算机随机产生选择       
        // 开始循环
        while(true) {
            //人类选择
            System.out.println("Please enter your choice: paper, scissors or stone. ");
            String human = s.nextLine();
            // 取得int格式的计算机选择
            int number = r.nextInt(3);
            String computer;
            //将计算机选择的int转换为string格式且对应 paper scissors stone
            if(number == 0) {
                computer = "paper";
            }else if(number == 1){
                computer = "scissors";
            }else {
                computer = "stone";
            }
            System.out.println("computer choose: "+computer);
           // 判定谁赢并计算得分
            if(computer.equals(human)) {
                System.out.println("Draw! ");
                count += 1;
            }else if(computer.equals("paper") && human.equals("stone")) {
                System.out.println("computer wins! ");
                count += 1;
                computerScore +=1;
            }else if(computer.equals("paper") && human.equals("scissors")) {
                System.out.println("human wins! ");
                count += 1;
                humanScore += 1;
            }else if(computer.equals("scissors") && human.equals("paper")) {
                System.out.println("computer wins! ");
                count += 1;
                computerScore += 1;
            }else if(computer.equals("scissors") && human.equals("stone")) {
                System.out.println("human wins! ");
                count += 1;
                humanScore += 1;
            }else if(computer.equals("stone") && human.equals("scissors")) {
                System.out.println("computer wins! ");
                count += 1;
                computerScore += 1;
            }else if(computer.equals("stone") && human.equals("paper")) {
                System.out.println("human wins! ");
                count += 1;
                humanScore += 1;
            }


            System.out.println("Scores: human: "+humanScore);
            System.out.println("        computer: "+computerScore);
            if(humanScore > computerScore) {
                System.out.println("human wins the final game!");
            }else if (humanScore < computerScore) {
                System.out.println("computer wins the final game!");
            }else if (humanScore == computerScore) {
                System.out.println("Draw!");
            }
          System.out.println("you totally play "+count+" times!");
        }               
    }

结果如下:

Please enter your choice: paper, scissors or stone. 
paper
computer choose: paper
Draw! 
Scores: human: 0
        computer: 0
Draw!
you totally play 1 times!
Please enter your choice: paper, scissors or stone. 
  • 写回答

1条回答 默认 最新

  • Dankin_ 2019-02-21 08:16
    关注

    import java.util.Scanner;
    import java.util.Random;
    public class count {

    //剪刀,石头,布

    public static void main(String[] args) {
        int count = 0; //玩游戏的初始值
        int computerScore = 0; //计算机得分初始值
        int humanScore = 0; //人类得分初始值
    
        Scanner s = new Scanner(System.in);
        Random r = new Random();//计算机随机产生选择
        // 开始循环
        for (int i = 1;i <=3;i++) {
            //人类选择
            System.out.println("Please enter your choice: paper, scissors or stone. ");
            String human = s.nextLine();
            // 取得int格式的计算机选择
            int number = r.nextInt(3);
            String computer;
            //将计算机选择的int转换为string格式且对应 paper scissors stone
            if (number == 0) {
                computer = "paper";
            } else if (number == 1) {
                computer = "scissors";
            } else {
                computer = "stone";
            }
            System.out.println("computer choose: " + computer);
            // 判定谁赢并计算得分
            if (computer.equals(human)) {
                System.out.println("Draw! ");
                count += 1;
            } else if (computer.equals("paper") && human.equals("stone")) {
                System.out.println("computer wins! ");
                count += 1;
                computerScore += 1;
            } else if (computer.equals("paper") && human.equals("scissors")) {
                System.out.println("human wins! ");
                count += 1;
                humanScore += 1;
            } else if (computer.equals("scissors") && human.equals("paper")) {
                System.out.println("computer wins! ");
                count += 1;
                computerScore += 1;
            } else if (computer.equals("scissors") && human.equals("stone")) {
                System.out.println("human wins! ");
                count += 1;
                humanScore += 1;
            } else if (computer.equals("stone") && human.equals("scissors")) {
                System.out.println("computer wins! ");
                count += 1;
                computerScore += 1;
            } else if (computer.equals("stone") && human.equals("paper")) {
                System.out.println("human wins! ");
                count += 1;
                humanScore += 1;
            }
        }
    
    
    
    
            System.out.println("Scores: human: "+humanScore);
            System.out.println("        computer: "+computerScore);
            if(humanScore > computerScore) {
                System.out.println("human wins the final game!");
            }else if (humanScore < computerScore) {
                System.out.println("computer wins the final game!");
            }else if (humanScore == computerScore) {
                System.out.println("Draw!");
            }
            System.out.println("you totally play "+count+" times!");
    
    }
    

    }

    就把while(true)改成for循环同时大括号要缩一下范围,缩到最后的输出成绩前就好啦

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!