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循环同时大括号要缩一下范围,缩到最后的输出成绩前就好啦

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

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集