Java基础流程控制习题练习示例含代码

1、【嵌套循环】使用双层for循环,在控制台打印出一个四行五列的长方形,效果如下:

@@@@@
@@@@@
@@@@@
@@@@@

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

2、【嵌套循环】使用双层for循环,在控制台打印出一个如下三角形,效果如下:

@
@@
@@@
@@@@
@@@@@

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

3、【跳转关键字】输出0-9之间的数,但是不包括5。

public class Demo3 {
   
    public static void main(String[] args) {
   
        for (int i = 0; i <= 9; i++) {
   
            if (i==5){
   
                continue;
            }
            System.out.print(i+" ");
        }
    }
}

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

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

5、【三元运算符】李雷想买一个价值7988元的新手机,她的旧手机在二手市场能卖1500元,而手机专卖店推出以旧换新的优惠,把她的旧手机交给店家,新手机就能够打8折优惠。为了更省钱,李雷要不要以旧换新?

public class Demo5 {
   
    public static void main(String[] args) {
   
        int newPhone = 7988;
        int oldPhone = 1500;
        
        String want=(newPhone-newPhone*0.8)>1500?"可以":"不可以";
        System.out.println("李雷" + want + "以旧换新");
    }
}

6、【三元运算符】让用户依次录入三个整数,求出三个数中的最小值,并打印到控制台。

public class Demo6 {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        System.out.println("请录入三个数字");
        int one=sc.nextInt();
        int two=sc.nextInt();
        int three=sc.nextInt();
        //求出最小值
        int min=(one<two?one:two)<three?(one<two?one:two):three;
        System.out.println("最小值为: " + min);
    }
}

7、【switch】某银行推出了整存整取定期储蓄业务,其存期分为一年、两年、三年、四年,到期凭存单支取本息。

存款年利率表如下:

​ 存期 年利率(%)

​ 一年 2.25

​ 两年 2.7

​ 三年 3.25

​ 四年 3.6

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

提示:

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

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

public class Demo7 {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        int year;
        double money;
        while (true){
   
            System.out.println("请存入money: ");
            money=sc.nextInt();
            if (money < 1000) {
   
                System.out.println("本金不足一千元,请重新输入: ");
            }else {
   
                break;
            }
        }
        while (true) {
   
            System.out.println("请输入存储年限: ");
            year=sc.nextInt();
            if (year>4){
   
                System.out.println("存储年限超过上限, 请重新输入");
            }else if (year<1){
   
                System.out.println("存储年限超过下限, 请重新输入");
            }else {
   
                break;
            }
        }
        switch (year){
   
            case 1:
                money= (money+money*2.25*0.01*1);
                System.out.println("得到的本息金额为: "+money);
                break;
            case 2:
                money= (money+money*2.7*0.01*2);
                System.out.println("得到的本息金额为: "+money);
                break;
            case 3:
                money= (money+money*3.25

你可能感兴趣的:(#,Java基础进阶,java,开发语言,intellij-idea,算法)