【JAVA基础】重新系统学习Java(五)案例一

Java基础知识案例

运算符

身高遗传

身高是具有遗传性的,子女的身高和父母的身高有一定的关系。假定,父母和子女的身高遗传关系如下:

​ 儿子身高(厘米)=(父亲身高+母亲身高) ×1.08÷2

​ 女儿身高(厘米)=(父亲身高×0.923+母亲身高) ÷2

求子女身高分别预计为多少?

package com.cat.type;


import java.util.Scanner;

public class type {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double f = sc.nextDouble();
        double m = sc.nextDouble();
        double s = (f + m) * 1.08 / 2;
        double d = (f * 0.923 + m) / 2;
        System.out.println(s + "cm");
        System.out.println(d + "cm");
    }
}

红茶绿茶

红茶妹妹有21元钱,她攒了几天钱之后自己的钱比原来的两倍还多三块。绿茶妹妹有24元钱,她攒了几天钱之后自己的钱正好是原来的两倍。那么红茶和绿茶现在的钱一样多,请问对么?

package com.cat.test;

public class Test1 {
    public static void main(String[] args) {
        int red = 21;
        int green = 24;
        red = red * 2 + 3;
        green = green * 2;
        System.out.println(red == green);
    }
}

外卖

某小伙想定一份外卖,商家的优惠方式如下:鱼香肉丝单点24元,油炸花生米单点8元,米饭单点3元。订单满30元8折优惠。鱼香肉丝优惠价16元,但是优惠价和折扣不能同时使用。那么这个小伙要点这三样东西,最少要花多少钱?

package com.cat.test;

public class Test1 {
    public static void main(String[] args) {
        double all= 24+8+3;
        all=(all*0.8)>=30?all*0.8:all;
        double all2=16+8+3;
        double money=all<all2?all:all2;
        System.out.println(money);
    }
}

if语句

手机以旧换新

李雷想买一个价值7988元的新手机,她的旧手机在二手市场能卖1500元,而手机专卖店推出以旧换新的优惠,把她的旧手机交给店家,新手机就能够打8折优惠。为了更省钱,李雷要不要以旧换新?请在控制台输出。

package com.cat.test;

public class Test1 {
    public static void main(String[] args) {
        int phone = 7988 - 1500;
        double buy = 7988 * 0.8;
        if (phone < buy) {
            System.out.println("卖旧手机更省钱" + phone + "¥");
        } else {
            System.out.println("以旧换新更省钱" + buy + "¥");
        }
    }
}

三数最小

让用户依次录入三个整数,求出三个数中的最小值,并打印到控制台。

package com.cat.test;

import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入第一个数字:");
        int num1 = sc.nextInt();
        System.out.println("输入第二个数字:");
        int num2 = sc.nextInt();
        System.out.println("输入第三个数字:");
        int num3 = sc.nextInt();
        int min;
        if (num1 < num2 && num1 < num3) {
            min = num1;
        } else if (num2 < num1 && num2 < num3) {
            min = num2;
        } else {
            min = num3;
        }
        System.out.println("最小值是:" + min);
    }
}

本息计算

某银行推出了整存整取定期储蓄业务,其存期分为一年、两年、三年、五年,到期凭存单支取本息。存款年利率表如下:

​ 存期 年利率(%)

​ 一年 2.25

​ 两年 2.7

​ 三年 3.25

​ 五年 3.6

请存入一定金额(1000起存),存一定年限(四选一),计算到期后得到的本息总额。

提示:

​ 存入金额和存入年限均由键盘录入

​ 本息计算方式:本金+本金×年利率×年限

package com.cat.test;

import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入存入金额:");
        double money = sc.nextDouble();
        double all;
        double rate;
        if (money >= 1000) {
            System.out.println("输入存入年限:");
            int year = sc.nextInt();
            if (year == 1 || year == 2 || year == 3 || year == 5) {
                if (year == 1) {
                    rate = 0.0225;
                } else if (year == 2) {
                    rate = 0.027;
                } else if (year == 3) {
                    rate = 0.0325;
                } else {
                    rate = 0.036;
                }
                all = money + money * rate * year;
                System.out.println("到期后得到的本息总额为:" + all);
            } else {
                System.out.println("没有您输入的存储年限");
            }
        } else {
            System.out.println("存入金额不得少于1000");
        }
    }
}

顾客优惠

某商场购物可以打折,具体规则如下:

​ 普通顾客购不满100元不打折,满100元打9折;

​ 会员购物不满200元打8折,满200元打7.5折;

​ 不同打折规则不累加计算。

请根据此优惠计划进行购物结算,键盘录入顾客的类别(0表示普通顾客,1表示会员)和购物的折前金额(整数即可),输出应付金额(小数类型)。

package com.cat.test;

import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入顾客类型:");
        int type = sc.nextInt();
        System.out.println("输入折前金额:");
        int money = sc.nextInt();
        double out;
        if (type == 0) {
            if (money >= 100) {
                out = money * 0.9;
            } else {
                out = money;
            }
            System.out.println("应付金额:" + out);
        } else if (type == 1) {
            if (money >= 200) {
                out = money * 0.75;
            } else {
                out = money * 0.8;
            }
            System.out.println("应付金额:" + out);
        } else {
            System.out.println("error");
        }

    }
}

个人所得税

2019年1月1日起,国家推出新的个人所得税政策,起征点上调值5000元。也就是说税前工资扣除三险一金(三险一金数额假设是税前工资的10%)后如果不足5000元,则不交税。如果大于5000元,那么大于5000元的部分按梯度交税,具体梯度比例如下:

​ 0 ~ 3000元的部分,交税3%

​ 3000 ~ 12000元的部分,交税10%

​ 12000 ~ 25000的部分 , 交税20%

​ 25000 ~ 35000的部分,交税25%

​ 35000 ~ 55000的部分,交税30%

​ 55000 ~ 80000的部分,交税35%

​ 超过80000的部分,交税45%

比如:某人入职一家企业后,税前工资是15000,则他每月该交个税的部分是15000-1500-5000=8500元,个税缴纳数额是3000×3%+5500×10%=640元。税后工资12860元。

请完成一个个税计算程序,在用户输入税前工资后,计算出他对应的纳税数额,以及税后工资为多少?

package com.cat.test;

import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入工资:");
        double money = sc.nextDouble();
        double outmoney;
        double last;
        money -= money * 0.1;
        if (money >= 5000) {
            money -= 5000;
            if (money > 0 && money <= 3000) {
                outmoney = money * 0.03;
            } else if (money > 3000 && money <= 12000) {
                outmoney = 3000 * 0.03 + (money - 3000) * 0.1;
            } else if (money > 12000 && money <= 35000) {
                outmoney = 3000 * 0.03 + 9000 * 0.1 + (money - 12000) * 0.2;
            } else if (money > 25000 && money <= 55000) {
                outmoney = 3000 * 0.03 + 9000 * 0.1 + 13000 * 0.2 + (money - 25000) * 0.25;
            } else if (money > 35000 && money <= 55000) {
                outmoney = 3000 * 0.03 + 9000 * 0.1 + 13000 * 0.2 + 10000 * 0.25 + (money - 35000) * 0.3;
            } else if (money > 55000 && money <= 80000) {
                outmoney = 3000 * 0.03 + 9000 * 0.1 + 13000 * 0.2 + 10000 * 0.25 + 20000 * 0.3 + (money - 55000) * 0.35;
            } else {
                outmoney = 3000 * 0.03 + 9000 * 0.1 + 13000 * 0.2 + 10000 * 0.25 + 20000 * 0.3 + 25000 * 0.35 + (money - 80000) * 0.45;
            }
            last = money - outmoney + 5000;
            System.out.println("应该纳税数额为:" + outmoney + "\n" + "税后工资为:" + last);
        } else {
            System.out.println("税前工资扣除三险一金少于5000元,不用交税");
        }
    }
}

switch语句

加减乘除

模拟计算器功能,对键盘录入的两个int类型的数据进行加、减、乘、除的运算,并打印运算结果。

要求:

​ 键盘录入三个整数,其中前两个整数代表参加运算的数据,第三个整数为要进行的运算(1:表示加法运算,2:表示减法运算,3:表示乘法运算,4:表示除法运算),演示效果如下:

​ 请输入第一个整数: 30

​ 请输入第二个整数: 40

​ 请输入您要进行的运算(1:表示加法,2:表示减法,3:表示乘法,4:表示除法): 1

​ 控制台输出:30+40=70

package com.cat.test;

import java.util.Scanner;

public class Test2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入第一个整数:");
        int num1 = sc.nextInt();
        System.out.println("请输入第二个整数:");
        int num2 = sc.nextInt();
        System.out.println("请输入您要进行的运算(1:表示加法,2:表示减法,3:表示乘法,4:表示除法):");
        int num3 = sc.nextInt();
        switch (num3) {
            case 1:
                System.out.println(num1 + "+" + num2 + "=" + (num1 + num2));
                break;
            case 2:
                System.out.println(num1 + "-" + num2 + "=" + (num1 - num2));
                break;
            case 3:
                System.out.println(num1 + "*" + num2 + "=" + (num1 * num2));
                break;
            case 4:
                System.out.println(num1 + "/" + num2 + "=" + (num1 / num2));
                break;
            default:
                System.out.println("数据输入错误");

        }
    }
}

循环语句

猪年年份

已知2019年是猪年,请在控制台输出从1949年到2019年中所有是猪年的年份。

package com.cat.test;

public class Test6 {
    public static void main(String[] args) {
        for(int year=1949;year<=2019;year++){
            if((2019-year)%12==0){
                System.out.println("猪年有:"+year);
            }
        }
    }
}

输出长方形

有一个输出语句System.out.print(“@”)。使用这个语句,在控制台打印出一个四行五列的长方形,效果如下:

​ @@@@@
​ @@@@@
​ @@@@@
​ @@@@@

package com.cat.test;

public class Test6 {
    public static void main(String[] args) {
        for (int i=1;i<=4;i++){
            for (int j=1;j<=5;j++){
                System.out.print("@");
            }
            System.out.println("");
        }
    }
}

输出三角形

有一个输出语句System.out.print(“@”)。使用这个语句,在控制台打印出一个五行的三角形,效果如下:
@
@@
@@@
@@@@
@@@@@

package com.cat.test;

public class Test6 {
    public static void main(String[] args) {
        for (int i=1;i<=5;i++){
            //内层循环代表每行打印i个
            for (int j=0;j<i;j++){
                System.out.print("@");
            }
            System.out.println("");
        }
    }
}

九九乘法表

使用for循环打印出九九乘法表。打印效果如下:
1×1=1
1×2=2 2×2=4

1×8=8 2×8=16 3×8=24 4×8=32 5×8=40 6×8=48 7×8=56 8×8=64
1×9=9 2×9=18 3×9=27 4×9=36 5×9=45 6×9=54 7×9=63 8×9=72 9*9=81

package com.cat.test;

public class Test6 {
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "*" + i + "=" + (i * j) + "\t");
            }
            System.out.println("");
        }
    }
}

兑换零钱

小明手中有一张10元纸币。需要到商店兑换零钱。商店只有1元纸币和5角硬币。那么请列举出所有的兑换方式。打印效果如下:
换1元纸币1张,5角硬币18个
换1元纸币2张,5角硬币16个
换1元纸币3张,5角硬币14个
… …
换1元纸币9张,5角硬币2个

自己代码

package com.cat.test;

public class Test6 {
    public static void main(String[] args) {
        for (int one = 0; one <= 10; one++) {
            for (int five = 20; five >= 0; five--) {
                if (one * 2 + five == 20) {
                    System.out.print("换一元纸币:" + one + "张,五角硬币" + five + "个");
                }
            }
            System.out.println("");
        }
    }
}

标准代码

public class Demo1 {
    public static void main(String[] args) {
        //1.定义循环一共十种结果
        for(int i =1; i < 10; i++) {
            //2.打印每种情况
            System.out.println("换1元纸币" + i + "张。5角硬币" + (10-i)*2 +"个");
        }
    }
}

闰年

中国有闰年的说法。闰年的规则是:四年一闰,百年不闰,四百年再闰。(年份能够被4整除但不能被100整除算是闰年,年份能被400整除也是闰年)。请打印出1988年到2019年的所有闰年年份。

package com.cat.test;

public class Test6 {
    public static void main(String[] args) {
        for (int y = 1988; y <= 2019; y++) {
            if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0) {
                System.out.println(y + "是闰年");
            }
        }
    }
}

灌水

有一个容量为10L的空水桶。水桶的上面开始往里灌水,同时下面开始往出流水。第一分钟灌水的速度是1L/min,第二分钟灌水的速度是2L/min,第三分钟灌水的速度是3L/min,以此类推。而流水的速度固定是3L/min。那么几分钟之后,水桶里能保持灌满水的状态?

package com.cat.test;

public class Test6 {
    public static void main(String[] args) {
        int w = 0;
        int m = 0;
        while (w < 10) {
            m++;
            w = w + m - 3;
            if (w < 0) {
                w = 0;
            }
        }
        System.out.println("桶里的灌满水需要" + m + "分钟");
    }
}

数组

遍历数组

请创建一个长度为6的整数数组,并为数组中的元素赋值。遍历数组,打印所有元素,元素之间用空格隔开。

package com.cjcat.create;

public class Test1 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}


最小值

现有一个小数数组{12.9, 53.54, 75.0, 99.1, 3.14}。请编写代码,找出数组中的最小值并打印。

package com.cjcat.create;

public class Test1 {
    public static void main(String[] args) {
        double[] arr = {12.9, 53.54, 75.0, 99.1, 3.14};
        double min = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] < min) {
                min = arr[i];
            }
        }
        System.out.println("最小值为:" + min);
    }
}

随机数和

创建一个长度为6的整数数组。请编写代码,随机生成六个0(包含)-100(不包含)之间的整数存放到数组中,然后计算出数组中所有元素的和并打印。

我的代码

package com.cjcat.create;

import java.util.Random;

public class Test1 {
    public static void main(String[] args) {
        int[] arr = new int[6];
        int sum = 0;
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = r.nextInt(100);
            System.out.println(arr[i]);
            sum += arr[i];
        }
        System.out.println("数组中所有元素的和是:" + sum);
    }
}

参考答案

public class Demo3 {
    public static void main(String[] args) throws IOException {
        //1.创建随机数对象
        Random r = new Random();
        //2.定义长度为6的数组
        int[] arr = new int[6];
        //3.循环给数组赋值
        for (int i = 0; i < arr.length; i++) {
            int num = r.nextInt(100);
            arr[i] = num;
        }
        //4.定义求和变量
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        //5.打印结果
        System.out.println("和是" + sum);
    }
}

拼接整数

现有一个整数数组,数组中的每个元素都是[0-9]之间的数字,从数组的最大索引位置开始到最小索引位置,依次表示整数的个位、十位、百位。。。依次类推。请编写程序计算,这个数组所表示的整数值。例如:

数组:{2, 1, 3, 5, 4}
表示的整数为:21354 // 注:是整数类型的两万一千三百五十四,不是字符串拼起来的。
package com.cjcat.create;


public class Test1 {
    public static void main(String[] args) {
        int[] arr = {2, 1, 3, 5, 4};
        int num = 0;
        for (int i = 0; i < arr.length; i++) {
            //高位乘以10再加上当前位的值
            num = num * 10 + arr[i];
        }
        System.out.println(num);
    }
}

求平均

定义一个数组来存储10个学生的成绩,例如:{72, 89, 65, 87, 91, 82, 71, 93, 76, 68}。计算并输出学生的平均成绩。

package com.cjcat.create;


public class Test1 {
    public static void main(String[] args) {
        int[] score = {72, 89, 65, 87, 91, 82, 71, 93, 76, 68};
        double avg = 0;
        for (int i = 0; i < score.length; i++) {
            avg += score[i];
        }
        avg /= score.length;
        System.out.println("平均成绩为:" + avg);
    }
}

新增数据并排序

有一个数组,其中有十个元素从小到大依次排列 {12,14,23,45,66,68,70,77,90,91}。再通过键盘录入一个整数数字。要求:把数字放入数组序列中,生成一个新的数组,并且数组的元素依旧是从小到大排列的。

package com.cjcat.create;


import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        int[] arr = {12, 14, 23, 45, 66, 68, 70, 77, 90, 91};
        int[] arr2 = new int[11];
        int index = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数:");
        int num = sc.nextInt();
        for (int i = 0; i < arr.length; i++) {
            if (num >= arr[i]) {
                arr2[i] = arr[i];
                index = i + 1;
            } else {
                arr2[i + 1] = arr[i];
            }
        }
        arr2[index] = num;
        for (int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i] + "\t");
        }
    }
}

股票亏盈

小李用自己的钱买了十只股票,年末他看了各只股票的收益分别是10.4%,-3%,-6.2%,1.2%,-6.1%,-19%,-3.8%,0.9%,-4.5%,5.5%。请使用数组相关知识编程,帮他统计赚钱的股票和赔钱的股票分别有多少只?

package com.cjcat.create;


import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        int add = 0;
        int less = 0;
        double[] arr = {0.104, -0.03, -0.062, 0.012, -0.061, -0.19, -0.038, 0.009, -0.045, 0.055};
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] >= 0) {
                add++;
            } else {
                less++;
            }
        }
        System.out.println("赚钱的股票一共有:" + add + "只");
        System.out.println("赔钱的股票一共有:" + less + "只");
    }
}

奇左偶右

定义一个数组其中包含多个数字。用自己的方式最终实现,奇数放在数组的左边,偶数放在数组的右边。(可以创建其他数组,不必须在原数组中改变)

package com.cjcat.create;


import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] arr2 = new int[9];
        int j = 0;
        int k = 0;
        for (int i = 0; i < arr.length; i++) {
            if ((arr[i] % 2) != 0) {
                arr2[j] = arr[i];
                j++;
            } else {
                arr2[arr2.length - k - 1] = arr[i];
                k++;
            }
        }
        for (int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i] + "\t");
        }
    }
}

方法

找出最小值

定义一个方法,该方法能够找出两个小数中的较小值并返回。在主方法中调用方法进行测试。

package com.cjcat.demo;

import java.util.Scanner;

public class test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入第一个小数:");
        double a = sc.nextDouble();
        System.out.println("输入第二个小数:");
        double b = sc.nextDouble();
        double c = num(a, b);
        System.out.println("较小值为:");
    }

    public static double num(double a, double b) {
        if (a > b) {
            return b;
        } else {
            return a;
        }
    }

}

返回最大值

定义一个方法,该方法能够找出三个整数中的最大值并返回。在主方法中调用方法测试执行。

package com.cjcat.demo;

import java.util.Scanner;

public class test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入第一个整数:");
        int a = sc.nextInt();
        System.out.println("请输入第二个整数:");
        int b = sc.nextInt();
        System.out.println("请输入第三个整数:");
        int c = sc.nextInt();
        int max = num(a, b, c);
        System.out.println("最大值为:" + max);
    }

    public static int num(int a, int b, int c) {
        if (a > b && a > c) {
            return a;
        } else if (b > a && b > c) {
            return b;
        } else {
            return c;
        }
    }
}

输出长方形

通过键盘录入两个整数n和m。n代表行数,m代表列数。定义一个方法,方法的功能是打印n行m列的@符号。

package com.cjcat.demo;

import java.util.Scanner;

public class test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入行数:");
        int hang = sc.nextInt();
        System.out.println("请输入列数:");
        int lie = sc.nextInt();
        chang(hang, lie);
    }

    public static void chang(int hang, int lie) {
        for (int i = 0; i < hang; i++) {
            for (int j = 0; j < lie; j++) {
                System.out.print("@");
            }
            System.out.println("");
        }
    }
}

输出nn乘法表

我们知道数学中有九九乘法表。请定义一个方法,方法的功能是打印nn乘法表。在主方法中键盘录入取值范围为[1-9]的变量n,测试方法。

package com.cjcat.demo;

import java.util.Scanner;

public class test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个1-9之间的整数");
        int num = sc.nextInt();
        play(num);
    }

    public static void play(int num) {
        for (int i = 1; i <= num; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "x" + i + "=" + (i * j) + "\t");
            }
            System.out.println();
        }
    }

}

排序

在主方法中通过键盘录入三个整数。定义一个方法,方法接收三个整数变量,在方法中从大到小依次打印三个变量。

package com.cjcat.demo;

import java.util.Scanner;

public class test1 {
    public static void main(String[] args) {
        int[] arr = new int[3];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入第" + (i + 1) + "个整数");
            arr[i] = sc.nextInt();
        }
        System.out.print("从小到大的顺序是:" + "\t");
        array(arr);

    }

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

        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "\t");
        }
    }
}

绝对值

数字是有绝对值的,负数的绝对值是它本身取反,非负数的绝对值是它本身。请定义一个方法,方法能够得到小数类型数字的绝对值并返回。请定义方法并测试。

package com.cjcat.demo;

import java.util.Scanner;

public class test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个小数:");
        double ab = sc.nextDouble();
        double abs = num(ab);
        System.out.println(ab + "的绝对值是" + abs);
    }

    public static double num(double a) {
        if (a >= 0) {
            return a;
        } else {
            return -a;
        }
    }
}

四舍五入

请定义一个方法,对正数的小数进行四舍五入的操作(不考虑负数情况)。四舍五入之后的结果是一个int整数类型并返回。最后对方法进行测试。

package com.cjcat.demo;

import java.util.Scanner;

public class test1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入一个正数:");
        double num = sc.nextDouble();
        int num1 = intnum(num);
        System.out.println("四舍五入的结果是:" + num1);
    }

    public static int intnum(double num) {
        double a = num % 1;
        if (a >= 0.5) {
            return (int) num + 1;
        } else {
            return (int) num;
        }
    }
}

模拟抽奖

一个大V直播抽奖,奖品是现金红包,分别有{2,588,888,1000,10000}五个奖金。请使用代码模拟抽奖,打印出每个奖项,奖项的出现顺序要随机且不重复。

我的答案与标准答案不符

//我的答案
package com.cjcat.demo;

import java.util.Random;
import java.util.Scanner;

public class test1 {
    public static void main(String[] args) {
        int[] arr = {2, 588, 888, 1000, 10000};
        num(arr);
    }

    public static void num(int[] arr) {
        Random r = new Random();
        int temp = 0;
        for (int i = 0; i < arr.length; i++) {
            int num = r.nextInt(arr.length);
            temp = arr[num];
            arr[num] = arr[i];
            arr[i] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i] + "元的奖金被抽出");
        }


    }
}

//参考答案
public class Demo8 {
    public static void main(String[] args) {
        //1.定义存放奖金的数组
        int[] arr = {10000,1000,588,888,2};
        //2.定义数组存放已经被取过的奖金
        int[] brr = new int[5];
        //3.定义变量表示brr的索引
        int index = 0;
        //4.创建随机数对象
        Random r = new Random();
        //5.定义循环反复获取随机数,如果index小于arr的长度则继续循环
        while(index < arr.length){
            //5.1每次生成一个随机索引对应一个奖金
            int i = r.nextInt(arr.length);
            //5.2调用方法判断这个奖金是否被获取过
            boolean b = exist(brr, arr[i]);
            //5.3b是false代表这个奖金没有被抽取过
            if(!b) {
                //5.4把获取之后的奖金存放到brr中
                brr[index] = arr[i];
                //5.5index索引向后移动一次
                index++;
                //5.6打印本次被抽出的奖项
                System.out.println( arr[i] + "元的奖金被抽出");
            }
        }
    }
    //6.定义方法:判断brr数组中是否存在num这个数字
    public static boolean exist(int[] brr,int num){
        //6.1对数组进行遍历
        for (int i = 0; i < brr.length; i++) {
            //6.2判断如果找到数字,则返回true
            if(brr[i] == num){
                return true;
            }
        }
        //6.3如果最终没有找到则返回false
        return false;
    }
}

所学知识编程案例汇总

复习之前所学Java编程知识

案例一:买飞机票

需求:

机票价格按照淡季旺季、头等舱和经济舱收费、输入机票原价、月份和头等舱或经济舱。

按照如下规则计算机票价格:旺季(5-10月)头等舱9折,经济舱8.5折,淡季(11月到来年4月)头等舱7折,经济舱6.5折。

package com.cjcat.all;

import java.util.Scanner;

public class demo1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入机票原价:");
        double money = sc.nextDouble();
        System.out.println("输入月份:");
        int month = sc.nextInt();
        if (month <= 0 || month > 12) {
            System.out.println("输入月份有误");
            return;
        }
        System.out.println("头等舱请输入1,经济舱请输入2:");
        int chose = sc.nextInt();
        double end = play(money, month, chose);
        System.out.println("最终优惠价格为:" + end);
    }

    public static double play(double money, int month, int chose) {
        if (month >= 5 && month <= 10) {
            switch (chose) {
                case 1:
                    money *= 0.9;
                    break;
                case 2:
                    money *= 0.85;
                    break;
            }
        } else {
            switch (chose) {
                case 1:
                    money *= 0.7;
                    break;
                case 2:
                    money *= 0.65;
            }
        }
        return money;
    }
}

案例二:找素数

判断101-200之间有多少个素数,并输出所有素数

package com.cjcat.all;

public class demo2 {
    public static void main(String[] args) {
        for (int i = 101; i <= 200; i++) {

            //信号位
            boolean flag = true;

            for (int j = 2; j < i / 2; j++) {
                if (i % j == 0) {
                    flag = false;
                    break;
                }
            }


            if (flag) {
                System.out.println(i + "\t");
            }

        }
    }
}

案例三:开发验证码

需求:定义方法实现随机产生一个5位的验证码,每位可能是数字、大写字母、小写字母

package com.cjcat.all;

import java.util.Random;

public class demo3 {
    public static void main(String[] args) {
        String code = createCode(5);
        System.out.println("随机验证码是:" + code);
    }

    public static String createCode(int num) {
        String code = "";
        Random r = new Random();
        for (int i = 0; i < num; i++) {
            int type = r.nextInt(3);
            switch (type) {
                case 0:
                    char ch = (char) (r.nextInt(26) + 65);
                    code += ch;
                    break;
                case 1:
                    char ch1 = (char) (r.nextInt(26) + 97);
                    code += ch1;
                    break;
                case 2:
                    code += r.nextInt(10);
                    break;
            }
        }
        return code;
    }
}

案例四:数组元素的复制

需求:把一个数组中的元素复制到另一个新数组中去

package com.cjcat.all;

public class demo4 {
    public static void main(String[] args) {
        int[] arr = {11, 22, 33, 44, 55, 66};
        int[] brr = new int[6];
        copy(arr, brr);
        print(arr);
        print(brr);
    }

    public static void copy(int[] arr, int[] brr) {
        for (int i = 0; i < arr.length; i++) {
            brr[i] = arr[i];
        }
    }

    public static void print(int[] arr) {
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
            System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ",");
        }
        System.out.println("]");
    }
}

案例五:评委打分

需求:在唱歌比赛中,有6名评委给选手打分,分数范围是[0 - 100]之间的整数。选手的最后得分为:去掉最高分、最低分后的4个评委的平均分,请完成上述过程并计算出选手的得分。

package com.cjcat.all;

import java.util.Scanner;

public class demo5 {
    public static void main(String[] args) {
        int[] arr = new int[6];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入第" + (i + 1) + "名评委的分数");
            arr[i] = sc.nextInt();
        }
        int sum = 0;
        int max = arr[0];
        int min = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];
            }
            if (min > arr[i]) {
                min = arr[i];
            }
            sum += arr[i];
        }
        double scores = (sum - max - min) * 1.0 / (arr.length - 2);
        System.out.println("选手得分为:" + scores);

    }
}

案例六:数字加密

需求:某系统的数字密码,比如1983,采用加密方式进行传输,规则如下:先得到每位数,然后每位数都加上5 , 再对10求余,最后将所有数字反转,得到一串新数。

package com.cjcat.all;

import java.util.Scanner;

public class demo6 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("输入需要加密的数字个数:");
        int length = sc.nextInt();
        int[] arr = new int[length];
        for (int i = 0; i < arr.length; i++) {
            System.out.println("请输入第" + (i + 1) + "个数字:");
            arr[i] = sc.nextInt();
        }
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (arr[i] + 5) % 10;
        }
        int temp = 0;
        for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
    }
}

案例七:模拟双色球

题目

中奖号码由六个红球和一个蓝球组成(注意:6个红球要求不能重复)

可以定义方法用于返回一组中奖号码(7个数据),返回的形式是一个整型数组

package com.cjcat.all;

import java.util.Random;
import java.util.Scanner;

public class demo7 {
    public static void main(String[] args) {
        int[] createNumber = createNumber();
        int[] userNumbers = userNumber();
        judge(createNumber, userNumbers);
    }

    public static void judge(int[] createNumber, int[] userNumber) {
        int red = 0;
        int blue = 0;
        for (int i = 0; i < createNumber.length - 1; i++) {
            for (int i1 = 0; i1 < userNumber.length - 1; i1++) {
                if (createNumber[i] == userNumber[i1]) {
                    red++;
                    break;
                }
            }
        }
        if (createNumber[createNumber.length - 1] == userNumber[userNumber.length - 1]) {
            blue = 1;
        }
        System.out.println("中奖号码是:");
        printArray(createNumber);
        System.out.println("您投注的号码是:");
        printArray(userNumber);
        System.out.println("您命中的红球个数:" + red);
        System.out.println("您是否命中蓝球:" + (blue == 1 ? "是" : "否"));
        if (blue == 1 && red < 3) {
            System.out.println("恭喜您获得六等奖5元");
        } else if (red == 3 && blue == 1 || red == 4 && blue == 0) {
            System.out.println("恭喜您获得五等奖10元");
        } else if (red == 4 && blue == 1 || red == 5 && blue == 0) {
            System.out.println("恭喜您获得四等奖200元");
        } else if (red == 5 && blue == 1) {
            System.out.println("恭喜您获得三等奖3000元");
        } else if (red == 6 && blue == 0) {
            System.out.println("恭喜您获得二等奖500万元");
        } else if (red == 6 && blue == 1) {
            System.out.println("恭喜您获得一等奖1000万元");
        } else {
            System.out.println("很遗憾没有中奖");
        }

    }

    public static void printArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + "\t");
        }
        System.out.println();
    }

    public static int[] createNumber() {
        int[] arr = new int[7];
        Random r = new Random();
        for (int i = 0; i < arr.length - 1; i++) {
            while (true) {
                int num = r.nextInt(33) + 1;
                boolean flag = true;
                for (int j = 0; j < i; j++) {
                    if (arr[j] == num) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    arr[i] = num;
                    break;
                }
            }
        }
        arr[arr.length - 1] = r.nextInt(16) + 1;
        return arr;
    }

    public static int[] userNumber() {
        int[] arr = new int[7];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < arr.length - 1; i++) {
            System.out.println("请输入第" + (i + 1) + "个红球的号码:");
            arr[i] = sc.nextInt();
        }
        System.out.println("请输入蓝球的号码:");
        arr[arr.length - 1] = sc.nextInt();
        return arr;
    }
}

总结

多看多练多反思

  • 9
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我会尽力回答你的问题。针对USB接口程序设计,我们需要先了解一些基础知识: 1. USB是什么? USB(Universal Serial Bus)是一种用于连接计算机与外部设备的串行总线标准,可用于数据传输、充电和供电等多种用途。 2. USB接口类型 USB接口按照接口类型分为USB Type-A、USB Type-B、mini-USB、micro-USB等多种类型。 3. USB接口规范 USB接口规范包括USB1.0、USB2.0、USB3.0等多个版本,每个版本都有自己的传输速度和功能特性。 在了解了上述基础知识后,我们可以开始设计USB接口程序。具体步骤如下: 1. 定义一个接口类Usb,在该类中定义一个抽象方法,用于实现数据传输功能。 ```java public interface Usb { void transferData(); } ``` 2. 定义一个USB设备类,该类实现了Usb接口,并实现了接口中的抽象方法,用于实现具体的数据传输功能。 ```java public class UsbDevice implements Usb { @Override public void transferData() { System.out.println("USB设备传输数据"); } } ``` 3. 定义一个主机类,该类也实现了Usb接口,但是该类的transferData方法中不是实现传输数据的功能,而是调用USB设备类的transferData方法,从而实现数据传输。 ```java public class Host implements Usb { private Usb usbDevice; public Host(Usb usbDevice) { this.usbDevice = usbDevice; } @Override public void transferData() { usbDevice.transferData(); } } ``` 4. 在主函数中实例化USB设备类和主机类,并调用主机类的transferData方法,从而实现数据传输。 ```java public static void main(String[] args) { UsbDevice usbDevice = new UsbDevice(); Host host = new Host(usbDevice); host.transferData(); } ``` 这样,我们就完成了一个简单的USB接口程序设计。当然,实际情况中,USB接口程序可能会更加复杂,需要考虑更多的因素,例如不同类型的USB接口、不同版本的USB规范等等。同时,我们还需要注意USB接口程序的稳定性和可靠性,以确保程序的正常运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

猫猫全栈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值