zoukankan      html  css  js  c++  java
  • JavaSE教程-04Java中循环语句for,while,do···while-练习2

    1.编写一个剪子石头布对战小程序

    该法是穷举法:将所有情况列出来

    import java.util.*;
    
    public class Game{
        public static void main(String[] args){
            Scanner input=new Scanner(System.in);
            System.out.println("经典小游戏:剪刀石头布");
            System.out.println("游戏规则:1:剪刀;2:石头;3:布");
            System.out.println("您的起始分数为:10分");
            int score=10;
            System.out.println("请输入要大战几百回合:");
            int count=input.nextInt();
    
    
            //外层我出的什么
            for(int i=1;i<=count;i++){
                System.out.println("请出拳:");
                //玩家出拳
                int fist=input.nextInt();
                //电脑出拳
                int computer=(int)(Math.random()*3)+1;
                //考虑电脑产生随机数的原理,可以扩大取值范围,但只使用中间的三个数值
                //如变为1—9,但只用456,此时需要将规则换为:4:剪刀;5:石头;6:布
                //只需要在switch的外层加一个if判断条件,限制computer取值:computer>=4&&computer<=6;即可
    
                //与电脑PK比较
                switch(fist){
                    case 1:
                        if(computer==1){
                            System.out.println("你们打平了,电脑出的剪刀");
                        }else if(computer==2){
                            System.out.println("你输了,电脑出的石头!");
                            score--;
                        }else{
                            System.out.println("你赢了,电脑出的布!");
                            score++;
                        }
                        break;
                    case 2:
                        if(computer==1){
                            System.out.println("你赢了,电脑出的剪刀!");
                            score++;
                        }else if(computer==2){
                            System.out.println("你们打平了,电脑出的石头!");
                        }else{
                            System.out.println("你输了,电脑出的布!");
                            score--;
                        }
                        break;
                    case 3:
                        if(computer==1){
                            System.out.println("你输了,电脑出的剪刀");
                            score--;
                        }else if(computer==2){
                            System.out.println("你赢了,电脑出的石头!");
                            score++;
                        }else{
                            System.out.println("你们打平了,电脑出的布!");
                        }
                        break;
                }
            }
            System.out.println("您最后的得分为:"+score);
    
        }
    
    }

    方法二:只需要比较

    import java.util.Scanner;
    
    public class Test6{
        public static void main(String[] args){
            //做一个剪刀石头布的对战小程序
            //1代表剪刀,2代表石头,3代表布
            Scanner input=new Scanner(System.in);
            int computer=(int)(Math.random()*3)+1;
    
            //方案一:下面是无限循环,还以优化,由用户开控制玩的局数
            //考虑电脑产生随机的概率问题,可以将取值范围变为1-5,我们用2,3,4来代表特定的含义,遇到1和5就舍弃
            for(;;){
                System.out.println("来玩剪刀石头布吧!1代表剪刀,2代表石头,3代表布,请输入:");
                int pk=input.nextInt();
                if((pk==1&&computer==3)||(pk==3&&computer==1)){
                    if(pk>computer){
                        System.out.println("你出的:"+pk+",电脑出的:"+computer+",你赢了");
                    }else{
                        System.out.println("你出的:"+pk+",电脑出的:"+computer+",电脑赢了");
                    }
                }else if(pk==computer){
                    System.out.println("你出的:"+pk+",电脑出的:"+computer+",你们不分上下");
                }else{
                    if(pk<computer){
                        System.out.println("你出的:"+pk+",电脑出的:"+computer+",你赢了");
                    }else{
                        System.out.println("你出的:"+pk+",电脑出的:"+computer+",电脑赢了");
                    }
                }
            }
    
            //最笨的方法:将9种情况用if···else罗列出来
    
        }
    }

    2.要求循环录入2个班的学员成绩

    假设每个班都有3个学员,依次录入,统计超过90分的学员人数,以及这批超过90分的学员平均分。
    这里写图片描述

    import java.util.Scanner;
    
    public class Test5{
        public static void main(String[] args){
            //要求循环录入2个班的学员成绩,假设每个班都有3个学员,
            //依次录入,统计超过90分的学员人数,以及这批超过90分的学员平均分。
            Scanner input=new Scanner(System.in);
    
            int score=0;
            int count=0;
            double sum=0;
            //外层为班级
            for(int i=1;i<=2;i++){
                //内层为班中的学员
                for(int j=1;j<=3;j++){
                    System.out.println("请输入"+i+"班第"+j+"个学员成绩:");
                    score=input.nextInt();
                    if(score>90){
                        count++;
                        sum+=score;
                    }
                }
            }
            System.out.println("两个班超过90的学员人数:"+count);
    
            //对于没有人超过90情况的处理
            if(count==0){
                System.out.println("两个班超过90的学员成绩平均分为:0");
            }else{
                System.out.println("两个班超过90的学员成绩平均分为:"+sum/count);
            }
        }
    }
  • 相关阅读:
    关于用Max导出Unity3D使用的FBX文件流程注解
    EffectManager
    Python Socket API 笔记
    UIScreenAdaptive
    CentOS 5 全功能WWW服务器搭建全教程 V3.0
    CentOS 6.4 图文安装教程(有些设置大部分教程没出现过)
    Django---分页器、中间件
    Django-form表单
    Django-认证系统
    Django——model基础
  • 原文地址:https://www.cnblogs.com/TCB-Java/p/6770159.html
Copyright © 2011-2022 走看看