栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2022年3月26日

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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  
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[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 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/781104.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号