Java 之 实验六 -- 条件和循环语句(二)

实验六

条件和循环语句(二)

练习一

  很多情况下程序需要生成一定范围内的一个随机数。Java类库中的Random类为程序员提供了这样的功能,能够同时生成一系列的随机数。以下变量声明Random类型的变量generator,并使用new操作符进行初始化:
   Random generator = new Random();
  generator对象能够生成整型或者浮点型的随机数字,需要使用到nextInt方法或者nextFloat方法。nextInt()返回一个任意的整型随机数字,而nextInt(n)返回一个在0至n-1之间的随机整数。nextFloat()和nextDouble()返回0至1之间的一个浮点数。
假设需要生成30至99之间的整数,可以采用以下的方法:
  使用nextInt():Math.abs(generator.nextInt())%70 + 30;
  使用nextInt(70): generator.nextInt(70)+30;
  使用nexFloat: (int)(generator.nextFloat()*70) + 30

完成以下程序。该程序用于生成随机数字。注意需要把java.util.Random类导入程序。

    import java.util.Random;
    
    public class LuckyNumbers {
        public static void main(String[] args){
            Random generator = new Random();
            int lucky1,lucky2,lucky3;
            lucky1 = Math.abs(generator.nextInt())%30+50;
            lucky2 = generator.nextInt(10)+90;
            lucky3 = (int)(generator.nextFloat(20)+11);
            System.out.println("Your lucky numbers are " + lucky1 +","+lucky2+" and "+lucky3);
        }
    }

练习二 -- Rock, Paper, Scissors

  程序Rock.java是石头剪子布游戏的一个框架。将该程序保存至本地目录,按照提示补充程序语句。该程序允许用户输入一个项目,计算机随机产生一个项目,对两个项目对比,给出胜负结果。
  用户可以输入R, P, S或者r, p, s来表示石头,布,剪刀三个项目。用户输入的项目保存在字符串变量中,以便于大小写的转换。使用一个switch语句将一个随机整数转换为游戏中计算机给出的项目。

    import java.util.Random;
    import java.util.Scanner;
    
    public class Rock {
        public static void main(String[] args){
            String personPlay;
            String computerPlay = "R";
            int computerInt;
            String StrcomputerInt;
            System.out.println("输入rps");
            Scanner scan = new Scanner(System.in);
            Random generator  = new Random();
            personPlay = scan.nextLine();
            personPlay.toUpperCase();
            computerInt = generator.nextInt(3);
            StrcomputerInt = Integer.toString(computerInt);
            personPlay = scan.next();
            switch(computerInt){
                case 0:computerPlay="R";            personPlay = personPlay.toUpperCase();            break;
                case 1:computerPlay="P";            personPlay = personPlay.toUpperCase();            break;
                case 2:computerPlay="S";            personPlay = personPlay.toUpperCase();            break;
            }
            System.out.println("computerPlay:"+computerPlay);
            if (personPlay.equals(computerPlay)){
                System.out.println("It's a tie !");
            }else if(personPlay.equals("R")){
                if (computerPlay.equals("S")){
                    System.out.println("Rock crushes Scissors. You win!!");
                }else{
                    System.out.println("Rock crushes Paper. You close!!");
                }
            }else if(personPlay.equals("P")){
                if (computerPlay.equals("R")){
                    System.out.println("Paper crushes Rock. You win!!");
                }else{
                    System.out.println("Paper crushes Scissors. You close!!");
                }
            }else if(personPlay.equals("S")){
                if (computerPlay.equals("P")){
                    System.out.println("Scissors crushes Paper. You win!!");
                }else{
                    System.out.println("Scissors crushes Rock. You close!!");
                }
            }
        }
    }

END

posted @ 2023-09-18 17:48  Ivan丶ky  阅读(227)  评论(0)    收藏  举报