JavaSE自学小练习(判断与循环)

2022/8/2 1:23:55

本文主要是介绍JavaSE自学小练习(判断与循环),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

题目来源:https://space.bilibili.com/37974444?spm_id_from=333.337.0.0

一、if判断语句

题目1

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

public class demo1 {
    public static void main(String[] args) {
        int money1 = 7988 - 1500;
        double money2 = 7988 * 0.8;
        //判断
        if (money1 > money2) {
            System.out.println("要以旧换新");
        } else {
            System.out.println("不以旧换新");
        }
    }
}

运行结果

 

 

题目2

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

import java.util.Scanner;
​
public class Demo3 {
    public static void main(String[] args) {
        //输入金额与年限
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入存入金额:");
        double money = sc.nextInt();
        System.out.println("请输入存入年限:");
        int year = sc.nextInt();
​
        //进行判断与计算
        double sum = 0;
        if (year == 1) {
            sum = money + money * 0.0225 * 1;
        } else if (year == 2) {
            sum = money + money * 0.0270 * 2;
        } else if (year == 3) {
            sum = money + money * 0.0325 * 3;
        } else if (year == 5) {
            sum = money + money * 0.0360 * 5;
        }
​
        //输出
        System.out.println("本息总和为" + sum);
​
    }
}
​

运行结果

 

 

 

 

题目3

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

存期      年利率(%)
​
一年      2.25
​
两年      2.7
​
三年      3.25
​
五年      3.6

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

提示:

存入金额和存入年限均由键盘录入
​
本息计算方式:本金+本金×年利率×年限

import java.util.Scanner;
​
public class Demo3 {
    public static void main(String[] args) {
        //输入金额与年限
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入存入金额:");
        double money = sc.nextInt();
        System.out.println("请输入存入年限:");
        int year = sc.nextInt();
​
        //进行判断与计算
        double sum = 0;
        if (year == 1) {
            sum = money + money * 0.0225 * 1;
        } else if (year == 2) {
            sum = money + money * 0.0270 * 2;
        } else if (year == 3) {
            sum = money + money * 0.0325 * 3;
        } else if (year == 5) {
            sum = money + money * 0.0360 * 5;
        }
​
        //输出
        System.out.println("本息总和为" + sum);
​
    }
}
​

运行结果

 

 

 

 

题目4

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

普通顾客购不满100元不打折,满100元打9折;
​
会员购物不满200元打8折,满200元打7.5折;
​
不同打折规则不累加计算。

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

package SelectionAndLooping;
​
import java.util.Scanner;
​
public class Demo4 {
    public static void main(String[] args) {
        //输入顾客类别与消费金额
        System.out.println("请输入顾客类别(0表示普通客户,1表示会员):");
        Scanner sc = new Scanner(System.in);
        double customer = sc.nextInt();
        System.out.println("请输入消费金额:");
        double money = sc.nextInt();
​
        //判断并进行计算
        if (customer == 0) {
            if (money >= 100) {
                money = money * 0.9;
            }
        } else if (customer == 1) {
            if (money >= 200)
                money = money * 0.75;
            else if (money < 200 && money > 0) {
                money = money * 0.8;
            }
        }
​
        //输出
        System.out.println(("您应支付的金额为" + money));
​
​
    }
}
​

运行结果

 

 

题目5

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

0 ~ 3000元的部分,交税3%           
​
~ 12000元
的部分,交税10%
​
~ 25000的部分 , 交税20%        
​
~ 35000的部分,交税25%
​
~ 55000的部分,交税30%      
​
~ 80000的部分,交税35%
​
超过80000的部分,交税45%

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

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

package SelectionAndLooping;
​
import java.util.Scanner;
​
public class Demo5 {
    public static void main(String[] args) {
        //输入税前工资
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您的税前工资:");
        int money = sc.nextInt();
        double before = money - (money * 0.1) - 5000;
​
        //定义税
        double shui = 0;
​
        //进行梯度计算
        if (before > 0 && before <= 3000) {
            shui = before * 0.03;
        } else if (before > 3000 && before <= 12000) {
            shui = 3000 * 0.03 + (before - 3000) * 0.1;
        } else if (before > 12000 && before <= 25000) {
            shui = 3000 * 0.03 + 9000 * 0.1 + (before - 12000) * 0.2;
        } else if (before > 25000 && before <= 35000) {
            shui = 3000 * 0.03 + 9000 * 0.1 + 13000 * 0.2 + (before - 25000) * 0.25;
        } else if (before > 35000 && before <= 55000) {
            shui = 3000 * 0.03 + 9000 * 0.1 + 13000 * 0.2 + 10000 * 0.25 + (before - 35000) * 0.3;
        } else if (before > 55000 && before <= 80000) {
            shui = 3000 * 0.03 + 9000 * 0.1 + 13000 * 0.2 + 10000 * 0.25 + 20000 * 0.3 + (before - 55000) * 0.35;
        } else if (before > 80000) {
            shui = 3000 * 0.03 + 9000 * 0.1 + 13000 * 0.2 + 10000 * 0.25 + 20000 * 0.3 + 25000 * 0.35 + (before - 80000) * 0.45;
        }
​
        //计算并输出
        double after = money - (money * 0.1) - shui;
        System.out.println("您的个人所得税为" + shui + "元,您的税后工资为" + after + "元");
​
    }
}
​

运行结果

 

 

 

二、switch选择语句

题目1

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

要求:

键盘录入三个整数,其中前两个整数代表参加运算的数据,第三个整数为要进行的运算(1:表示加法运算,2:表示减法运算,3:表示乘法运算,4:表示除法运算),演示效果如下:
​
    请输入第一个整数: 30
​
    请输入第二个整数: 40
​
    请输入您要进行的运算(1:表示加法,2:表示减法,3:表示乘法,4:表示除法): 1
​
    控制台输出:30+40=70

package SelectionAndLooping;
​
import java.util.Scanner;
​
public class Demo6 {
    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 symbol = sc.nextInt();
​
        //定义结果
        double result;
​
        //进行判断与运算,并输出
        switch (symbol) {
            case 1:
                result = num1 + num2;
                System.out.println(num1 + "+" + num2 + "=" + result);
                break;
            case 2:
                result = num1 - num2;
                System.out.println(num1 + "-" + num2 + "=" + result);
                break;
            case 3:
                result = num1 * num2;
                System.out.println(num1 + "*" + num2 + "=" + result);
                break;
            case 4:
                result = (double) num1 / (double) num2;
                System.out.println(num1 + "/" + num2 + "=" + result);
                break;
        }
    }
}
​

运行结果

 

 

三、循环语句

题目1

键盘录入两个整数number1和number2表示一个范围,求这个范围之内的整数和。

package SelectionAndLooping;
​
import java.util.Scanner;
​
public class Demo7 {
    public static void main(String[] args) {
        //输入number1与number2
        Scanner sc = new Scanner(System.in);
        System.out.println("请以此输入两个数字");
        int number1 = sc.nextInt();
        int number2 = sc.nextInt();
​
        //定义中间量
        int temp = number2;
​
        //判断大小
        if (number1 > number2) {
            number2 = number1;
            number1 = temp;
        }
​
        //定义和
        int sum = 0;
​
        //求范围内的数并进行计算与输出
        for (int i = number1; i <= number2; i++) {
            sum += i;
        }
        System.out.println("范围内数字和为" + sum);
​
​
    }
}
​

运行结果

 

 

 

题目2

需求:键盘录入两个数字,表示一个范围。统计这个范围中,既能被3整除,又能被5整除数字有多少个?

package SelectionAndLooping;
​
import java.util.Scanner;
​
public class Demo8 {
    public static void main(String[] args) {
        //输入number1与number2
        Scanner sc = new Scanner(System.in);
        System.out.println("请以此输入两个数字");
        int number1 = sc.nextInt();
        int number2 = sc.nextInt();
​
        //定义个数
        int count = 0;
​
        //进行计算
        for(int i = number1;i <= number2;i++){
            if(i % 3 == 0 && i % 5 == 0){
                count++;
            }
        }
​
        //输出个数
        System.out.println("既能被3整除,又能被5整除数字有"+count+"个");
​
    }
}
​

运行结果

 

 

 

题目3

需求:世界最高山峰是珠穆朗玛峰(8844.43米=8844430毫米),假如我有一张足够大的纸,它的厚度是0.1毫米。请问,我折叠多少次,可以折成珠穆朗玛峰的高度?

package SelectionAndLooping;
​
import java.sql.SQLOutput;
import java.util.concurrent.CountDownLatch;
​
public class Demo9 {
    public static void main(String[] args) {
        //定义初始厚度
        double x = 0.1;
​
        //定义次数
        int count = 0;
​
        //循环判断次数
        while (x < 8844430) {
            x *= 2;
            count++;
        }
​
        //输出
        System.out.println("折叠" + count + "次就可以可以折成珠穆朗玛峰的高度");
​
    }
}

运行结果

 

 

 

题目4

需求:给你一个整数 x 。

       如果 x 是一个回文整数,打印 true ,否则,返回 false 。

解释:回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。

例如,121 是回文,而 123 不是。

package SelectionAndLooping;
​
import java.util.Scanner;
​
public class Demo10 {
    public static void main(String[] args) {
        //输入需要判断的整数
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
​
        //定义一个中间值
        int temp = num;
​
        //定义回文以后的数
        int num0 = 0;
​
        //开始判断
        while (num != 0) {
            int ge = num % 10;  //求当前个位数
            num /= 10;
            num0 = num0 * 10 + ge; //回文以后的数的计算
        }
​
        //进行判断
        if (num0 == temp) {
            System.out.println(temp + "是回文数");
        } else {
            System.out.println(temp + "不是回文数");
        }
​
​
    }
​
}
​

运行结果

 

 

 

 

题目5

需求:

   给定两个整数,被除数dividend和除数divisor(都是正数,且不超过int的范围) 。
​
  将两数相除,要求不使用乘法、除法和 % 运算符。
​
  得到商和余数。

package SelectionAndLooping;
​
import java.util.Scanner;
​
public class Demo11 {
    public static void main(String[] args) {
        //依次输入被除数与除数
        Scanner sc = new Scanner(System.in);
        System.out.println("请依次输入被除数与除数");
        int dividend = sc.nextInt();
        int divisor = sc.nextInt();
​
        //定义中间量
        int temp = divisor;
​
        //定义商与余数
        int shang = 0;
        int yushu = 0;
​
        //通过减法进行一个商的确定
        while (dividend > divisor) {
            dividend -= divisor;
            shang++;
        }
​
        //最后确定余数
        yushu = dividend;
​
        //输出
        System.out.println("商为" + shang + "余数为" + yushu);
​
​
    }
}
​

运行结果

 

 

 

题目6

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

package SelectionAndLooping;
​
public class Demo12 {
    public static void main(String[] args) {
        for (int i = 1949; i <= 2019; i++) {
            if ((2019 - i) % 12 == 0) {
                System.out.println(i + "年是猪年");
            }
​
​
        }
    }
}

运行结果

题目7

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

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

运行结果

 

 



这篇关于JavaSE自学小练习(判断与循环)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程