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

java基础之八大题目练习

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

java基础之八大题目练习

目录

一.通过简单范例理解continue、break、return

  1.判断一个整数是否是水仙花数,所谓水仙花数是指一个3位数,其各个位上数字立方和等于其本身 例如:153=1*1*1+5*5*5+3*3*3

3. 判断一个年份是否为闰年(依据:1.能被400整除的是闰年 2.能被4整除且不能被100整除的是闰年)

4.某人有100000元,每经过一次路口,需要交费,规则如下: 1) 当现金>50000时,每次交5% 2)当现金<=50000时,每次交1000 编程计算该人可以经过多少次路口(要求:使用while、 break方式完成)

5.输出1-100之间的不能被5整除的数,每5个一行

6.求出 1 - 1/2 + 1/3 - 1/4+ .....+1/99-1/100的和

7. 求 1 +(1+2)+(1+2+3)+(1+2+3+4)+.....+(1+2+3+4+5+...+100)的和

8.通过for语句来输出小写的a-z以及大写的Z-A


一.通过简单范例理解continue、break、return

continue 语句跳出本次循环,而break跳出整个循环。

简单continue范例 、for 范例 和 return范例比较:

public class test1 {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            if (i == 2) continue;   //当i等于2时,结束本次循环,不打印2,进入下一轮循环
            System.out.println(i);
        }

    }
}



程序执行结果为:
0
1
3
4
public class test2 {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            if (i == 2) break; //当i等于2时,break跳出最靠近的那个循环,跳出后,程序继续往下执 
                                       行,若无执行语句,则程序结束。本程序跳出for循环后,程序已 
                                         无语句可执行,程序结束
            System.out.println(i);
        }

    }
}



程序执行结果为:
0
1
public class test3 {


    public static void main(String[] args) {

        for (int i = 1; i <= 5; i++) {

            if (i == 3) {
                System.out.println("时光成梦" + i);
                return; //当return用在方法时,表示跳出方法,如果使用在main,表示跳出程序
            }
            System.out.println("hello world!");
        }
        System.out.println("加油!!");


    }


输出结果为
hello world!
hello world!
时光成梦3

  1.判断一个整数是否是水仙花数,所谓水仙花数是指一个3位数,其各个位上数字立方和等于其本身 例如:153=1*1*1+5*5*5+3*3*3
public class test4 {
    public static void main(String[] args) {
        while (true) {
            System.out.println("请输入一个整数:");
            Scanner sc = new Scanner(System.in);
             int num = sc.nextInt();
            int n1 = num/100;
            int n2 = num/10%10;
            int n3 =num%10;
            if(n1*n1*n1+n2*n2*n2+n3*n3*n3==num){
                System.out.println(num+"为水仙花数");
            }
        }

    }
}



执行结果:

请输入一个整数:
153
153为水仙花数
请输入一个整数:
163
163不是水仙花数

 

3. 判断一个年份是否为闰年(依据:1.能被400整除的是闰年 2.能被4整除且不能被100整除的是闰年)
public class test4 {

    public static void main(String[] args) {
        while (true) {
            System.out.println("请输入一个年份:");
            Scanner sc = new Scanner(System.in);
            int year = sc.nextInt();
            if (year <= 0) {
                System.out.println("您输入的年份必须为正整数,请重新输入");
                continue;
            }
            ;
            if (year % 400 == 0) {
                System.out.println("这" + year + "年份是闰年");
            } else if (year % 4 == 0 && year % 100 != 0) {
                System.out.println("这" + year + "年份是闰年");
            } else {
                System.out.println("这" + year + "年份不是闰年");
            }
        }
    }
}

 执行结果:

请输入一个年份:
20183
这20183年份不是闰年
请输入一个年份:
2018
这2018年份不是闰年
请输入一个年份:
2019
这2019年份不是闰年
请输入一个年份:
2020
这2020年份是闰年

4.某人有100000元,每经过一次路口,需要交费,规则如下: 1) 当现金>50000时,每次交5% 2)当现金<=50000时,每次交1000 编程计算该人可以经过多少次路口(要求:使用while、 break方式完成)
public class test1 {

    public static void main(String[] args) {
        double flag = 100000.0; //
        double total = 100000.0;
        int count = 0;


        while (total > 50000) {
            total = total - total * 0.05;
            count++;
        }
        while (total <= 50000) {
            flag = total;
            total -= 1000;
            if (total < 0) break;
            count++;

        }

        System.out.println("经过路口" + count + "次," + "还剩" + flag + "元");
        
    }
}

执行结果:

经过路口62次,还剩767.4979115529859元

5.输出1-100之间的不能被5整除的数,每5个一行
public class test5 {
    public static void main(String[] args) {

        int count = 0;
        for (int i = 1; i <= 100; i++) {
            if (i % 5 != 0) {
                System.out.print(i + " ");
                count++;
                if (count % 5 == 0) {
                    System.out.println();
                }


            }
        }


    }
}

执行结果:

1 2 3 4 6 
7 8 9 11 12 
13 14 16 17 18 
19 21 22 23 24 
26 27 28 29 31 
32 33 34 36 37 
38 39 41 42 43 
44 46 47 48 49 
51 52 53 54 56 
57 58 59 61 62 
63 64 66 67 68 
69 71 72 73 74 
76 77 78 79 81 
82 83 84 86 87 
88 89 91 92 93 
94 96 97 98 99 

6.求出 1 - 1/2 + 1/3 - 1/4+ .....+1/99-1/100的和
public class test6 {
    public static void main(String[] args) {
        double sum = 0;
        for(double i = 1; i < 100 ; i +=2){sum += (1/i - 1/(i+1));}
        System.out.println(sum);


    }
}

执行结果:

0.6881721793101949

7. 求 1 +(1+2)+(1+2+3)+(1+2+3+4)+.....+(1+2+3+4+5+...+100)的和
public class test7 {
    public static void main(String[] args) {
        int sum1 = 0;
        int sum = 0;
        for(int i = 1; i <= 100 ; i++){
             sum1 += i;
             sum += sum1;

        }
        System.out.println(sum);


    }
}

执行结果:

171700 

8.通过for语句来输出小写的a-z以及大写的Z-A
public class test8 {
    public static void main(String[] args) {

       
        for (char c1 = 'a'; c1 <= 'z'; c1++) {  //打印出a-z
            System.out.println(c1 + " ");
        }
        System.out.println("=================");

        
        for (char c1 = 'Z'; c1 >= 'A'; c1--) {  //打印出A-Z
            System.out.println(c1 + " ");
        }

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/986872.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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