2022年3月26日

案例-猜数字游戏
1.游戏:
系统随机产生一个0-100内的随机数,用户输入数字猜!
如果猜大了则提示猜大了,如果猜小了则提示猜小了
如果猜对了,则打印所猜分数和次数
如果中途退出则输-1
满分100分,猜错一次扣3分
游戏难度选择,难度以产生随机数的大小为准,分为3个等级:1(0-100)、2(0-1000)、3(0-10000)

public class GuessingPro {
    public static void main(String[] args) {
        System.out.println("进行游戏");
        System.out.println("请选择游戏难度:1(0-100)、2(0-1000)、3(0-10000)");
        Scanner scanner = new Scanner(System.in);
        int rank = scanner.nextInt();
        int scope=0;
        if(rank == 1)
            scope=100;
        else if(rank==2)
            scope = 1000;
        else if(rank == 3)
            scope = 10000;
        int result = (int)(Math.random()*scope);


        int count = 0;

        int score = 100;
        while (true){
            count ++;
            System.out.println("请输入你的数字:");
            System.out.println("退出游戏请输入-1");

            int num =scanner.nextInt();
            if(num == -1){
                System.out.println("您退出了游戏!");
                System.out.println("结果是:"+result+" 您猜了"+(count-1)+"次");
                break;
            }
            if(num == result){
                System.out.println("猜对了!你猜了 "+count+" 次"+"分数为:"+score);
                break;
            }
            if(num > result){
                System.out.println("猜大了");
                score -= 3;
            }
            if(num < result){
                System.out.println("猜小了");
                score -= 3;
            }
        }

    }

编写程序,用户输入杨辉三角形的行数,在控制台打印对应的杨辉三角形

  • eg: 输入了2
*    *
   * * *
 */
public class Triangle {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入三角形的行数:");
        int line = scanner.nextInt();
        for (int i = 1; i <=line ; i++) {
            for (int j = 0; j <line-i ; j++) {
                System.out.print(" ");

            }
            for (int j = 0; j <i*2-1 ; j++) {
                System.out.print("*");
            }
            System.out.println();

        }

    }
public class RandomArray {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入数组的长度:");
        int length = scanner.nextInt();
        System.out.println("输入数组的范围:");
        int  scope = scanner.nextInt();
        int [] res =arrayRadom(length,scope);
        System.out.print("生成的数组:"+Arrays.toString(res));
        //Arrays.sort(res);
        System.out.println();
        res = arraySort(res);
        System.out.print("排序后:"+Arrays.toString(res));
        double average = avg(res);
        System.out.println();
        System.out.println("平均数为:"+average);
        prime(res);
        System.out.println();
        odd(res);

    }
    public static int [] arrayRadom(int length,int scope){
        int [] arr= new int[length];
        for (int i = 0; i <arr.length ; i++) {
            arr[i] = (int)(Math.random()*scope);

        }
        return arr;

    }
    public static int [] arraySort(int [] arr){
        for (int i = 0; i <arr.length ; i++) {
            for (int j = 0; j <arr.length-i-1 ; j++) {
                int temp;
                if(arr[j]>arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }

            }

        }
        return arr;

    }
    public  static double avg(int[] arr){
        double sum=0;
        double avg=0;
        for (int i = 0; i <arr.length ; i++) {
            sum+=arr[i];

        }
        avg= sum/arr.length;
        return avg;

    }
    public static void prime(int[] arr){
        System.out.print("质数有: ");
        int j=0;
        for (int i = 0; i <arr.length ; i++) {
            for (j = 2; j <arr[i] ; j++) {
                if(arr[i] % j == 0){
                    break;
                }


            }
            if(arr[i] == j){
                System.out.print(arr[i]+" ");
            }



        }
    }
    public static void odd(int [] arr){
        System.out.print("奇数有:");
        for (int i = 0; i <arr.length ; i++) {
            if(arr[i]%2!=0){
                System.out.print(arr[i]+" ");
            }

        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值