JavaSE基础——分支、循环语句练习题

1、判断水仙花数(请输入任意一个三位数,判断它是否是水仙花数? 说明:水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身)(特别经典的题!)

        Scanner sc = new Scanner(System.in);System.out.println("请输入一个任意三位数:");int num = sc.nextInt();//个位int ge = num % 10;//十位int shi = num / 10 %10;//百位int bai = num / 100;//第一种方法
//        String c = ge*ge*ge + shi*shi*shi + bai*bai*bai == num ? "这个数是是水仙花数" : "这个数不是水仙花数";//        System.out.println(c);//第二种方法,使用if语句if (Math.pow(ge,3)+Math.pow(shi,3)+Math.pow(bai,3) == num){System.out.println("这个数是水仙花数。");}else{System.out.println("这个数不是水仙花数");}

2、请输入三个数,使用if语句找出最大的数(if)

        System.out.println("输入第一个数");int num1 = sc.nextInt();System.out.println("输入第二个数");int num2 = sc.nextInt();System.out.println("输入第三个数");int num3 = sc.nextInt();int max = 0;if (num1>num2){max = num1;}else{max = num2;}if (num3>max){max = num3;}System.out.println(max);

3、请输入任意一个年份,判断该年份是否是闰年?闰年的判定标准: 第一能被4整除,但是不能被100整除的是闰年; 第二能被400整除的也是闰年。(if)

        System.out.println("输入任意年份:");int year = sc.nextInt();if (year % 4 == 0 && year % 100 !=0 ||year % 400 == 0){System.out.println(year + "是闰年");}else{System.out.println(year + "不是闰年");}

4、输入今天是星期几,打印出当天下班后的安排(switch语句)

        /*请用户输入今天是星期几(1-7),打印出来晚上的安排周一:开例会周二健身周三沟通客户周四项目加班周五项目加班周六健身周天带孩子回父母家看看*/Scanner sc = new Scanner(System.in);System.out.println("输入今天是星期几(1-7):");int weekday = sc.nextInt();switch (weekday){case 1:System.out.println("开例会");break;case 2 :case 6:System.out.println("健身");break;case 3 :System.out.println("沟通客户");break;case 4:case 5:System.out.println("项目加班");break;case 7 :System.out.println("周天带孩子回父母家看看");break;//case 后要将default:当没有匹配的值将执行default。default:System.out.println("====default");}

5、输入5个人的成绩,打印出总分、平均分。(for循环)

        int sum = 0;Scanner sc = new Scanner(System.in);System.out.println("输入班级人数");int n = sc.nextInt();for (int i = 0; i 

6、输入一个5位数,判断这个数是不是回文数(回文数:比如 12321,34543就是回文数,万位=个位 千位=十位)(if)

        Scanner sc = new Scanner(System.in);System.out.println("输入一个5位数:");int num = sc.nextInt();//个位数int ge = num % 10;//十位数int shi = num / 10 % 10;//千位数int qian = num / 1000 % 10;//万位数int wan = num / 10000;if (ge == wan && shi == qian ){System.out.println(num + "这个数是回文数");}else{System.out.println("这个数不是回文数");

7、输入三角形的三条边长,判断是否能组成一个三角形(提示:三角形任意两条边之和大于第三边。)(if)
扩展:判断这个三角形是直角、钝角还是锐角。

public class HomeWork2 {/*输入三角形的三条边长,判断是否能组成一个三角形;提示:三角形任意两条边之和大于第三边。*/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();
//
//                if (a + b > c && a + c > b && b + c > a){
//                    System.out.println("是一个三角形");
//                }else{
//                    System.out.println("不是一个三角形");
//                }/*输入三角形判断三角形是直角钝角 还是锐角*/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();if (a + b > c && a + c > b && b + c > a){System.out.println("是一个三角形");int max = a > b ? a > c ? a : c : b > c ? b : c;int min = a < b ? a < c ? a  :c : b < c ? b : c;int middle = 0;if (max != a && min != a){middle = a;}if(max != b && min != b){middle = b;}if(max != c && min != c){middle = c;}if (min*min + middle*middle > max*max){System.out.println("这是一个锐角三角形。");}if (min*min + middle*middle == max*max){System.out.println("这是一个直角三角形。");}if (min*min + middle*middle < max*max){System.out.println("这是一个钝角三角形。");}}else{System.out.println("不是一个三角形");}}

8、使用for语句循环计算1-100所有偶数的和(for)

public static void main(String[] args) {int sum = 0;for (int i = 1; i <= 100; i++) {if (i % 2 == 0){sum += i;}}System.out.println("1-100中所有偶数的和:" + sum);}

9、任意输入一个9位以内的整型数字,打印出它是几位数(while)

        Scanner sc = new Scanner(System.in);System.out.println("输入一个九位以内的数字");int input = sc.nextInt();//12345int num = input;int result = 0;while (true){num /= 10 ;result++;if (num == 0 ){break;}}System.out.println( input + "是一个" + result + "位的数。");

10、打印0-50以内前5个能被7整除的数

扩展:打印0-50以内前所有能被7整除的数

        // 打印0-50以内前5个能被7整除的数//打印次数int count = 0 ;for (int i = 1; i <= 50; i++) {if (i % 7 == 0 ){System.out.println(i);count++;}//当打印到第五次时退出循环if (count >= 5 ){break;}}// 打印0-50以内前所有能被7整除的数for (int i = 1; i <= 50; i++) {if (i % 7 == 0 ) {System.out.println(i);}}

11、打印出100以内所有跟3有关的数字,包括包含3的和能被3整除的。

        for (int i = 0; i < 100; i++) {if ((i % 3 == 0)||(i % 10 == 3 )||(i / 10 == 3)){System.out.println(i);}}

12、猜数字游戏,在程序中产生一个50-99之间的随机数 然后让用户猜测:用户输入数字与随机数进行比较, 分别有“猜大了”“猜小了”“猜对了”三种提示 用户一共有五次机会,五次机会用完都没有猜对,游戏结束。 如果猜对了就提前结束游戏。

        //1、生成一个随机数Random r = new Random();int num = r.nextInt(49) + 50;System.out.println(num);//3、判断猜测,并输出猜测结果。。。。for (int i = 1; i <=5 ; i++) {//        2、用户输入猜测的数Scanner sc = new Scanner(System.in);System.out.println("请输入你猜测的第"+ i+ "的数字(50-99之间)");int data = sc.nextInt();if (data<50||data>99){i--;continue;}if (data > num){System.out.println("猜大了");} else if (data < num){System.out.println("猜小了");}else{System.out.println("猜对了");break;}}

13、有一对老耗子,每个月都生一对小耗子。 小耗子长3个月,第四个月开始变成老耗子开始生 假如所有耗子都不死,那么请问24个月后有多少只耗子?

        /*有一对老耗子,每个月都生一对小耗子。小耗子长3个月,第四个月开始变成老耗子开始生假如所有耗子都不死,那么请问24个月后有多少只耗子?1     2       3         4         5        6      7        8老耗子     2     2      2       2         4         6        8       12      18小耗子1岁  0     2      2       2         4         6        8       12      182岁  0     0      2       2         2         4        6       8       123岁  0     0      0       2         2         2        4       6       8下个月的老耗子=上个月的老耗子+三岁的小耗子下个月1岁耗子 = 当月老耗子的个数下个月2岁的耗子个数 = 上个月的1岁耗子个数下个月3岁的耗子个数 = 上个月的2岁耗子个数*/int child1 = 0;int child2 = 0;int child3 = 0;int old = 2;
//        i代表月份:24个月for (int i = 0; i < 24; i++) {old += child3;child3 = child2;child2 = child1;child1 = old;}// 第一种方法int sum = child1 + child2 + child3 +old;System.out.println(old);System.out.println(child1);System.out.println(child2);System.out.println(child3);System.out.println(sum);//第二种方法System.out.println(old + child1 + child2 + child3);

14、输入一个20以内的正整数,转换成二进制输出(分别使用while和do-while)

//      使用whileScanner sc = new Scanner(System.in);System.out.println("输入一个20以内的正整数:");int num = sc.nextInt();String result = "" ;
//      不知道要循环多少次,使用while循环。while (num != 0 ){//数字对二取余int num1 = num % 2;//二进制要从下往上写。result = num1 + result;//每次数字要除2num /= 2;}System.out.println(result);//使用do-whileScanner sc = new Scanner(System.in);System.out.println("输入一个20以内的正整数:");int num = sc.nextInt();String result = "" ;
//      不知道要循环多少次,使用while循环。do{//数字对二取余int num1 = num % 2;//二进制要从下往上写。result = num1 + result;//每次数字要除2num /= 2;}while (num != 0 );System.out.println(result);

15、计算出0-45中,偶数的个数,奇数的个数 偶数的和,奇数的和。

        //偶数和奇数的个数int num1 = 0;int num2 = 0;//和int sum1 = 0;int sum2 = 0;for (int i = 0; i <= 45; i++) {//偶数if (i%2 == 0){num1++;sum1 += i;}//奇数if (i%2 != 0){num2++;sum2 += i;}}System.out.println("偶数的个数"+num1);System.out.println("奇数的个数"+num2);System.out.println("偶数的和"+sum1);System.out.println("奇数的和"+sum2);

16、求0-100中 5的倍数的个数

        int num =0;for (int i = 0; i <= 100; i++) {if (i % 5 == 0){num++;}}System.out.println("0-100中,5的倍数的个数" + num);

17、有一个分数序列,2/1,3/2,5/3,8/5,13/8,21/13.... 求这个序列的前10项的和。规律: 后一个数的分母 等于是前一个的 分子 后一个数的分子 等于前一个数的 分子+分母。

        //分母double a = 1;//分子double b = 2;//和double sum = 0;for (int i = 0; i < 10; i++) {sum += b/a;double temp = a;a = b;b += temp;}System.out.println("前十项和是" + sum);

18、输入3个小组,每组5人的同学成绩,统计每个小组的总分,平均分和全班的总分与平均分。

    public static void main(String[] args) {Scanner sc = new Scanner(System.in);int classSum = 0;for (int i = 0; i < 3; i++) {int groupSum = 0;for (int j = 0; j < 5; j++) {System.out.println("请输入第" + (i + 1) + "组的第" + (j + 1) + "位同学的成绩:");int score = sc.nextInt();classSum += score;groupSum += score;}System.out.println("第" + (i + 1) + "组的总分是:" + groupSum);System.out.println("第" + (i + 1) + "组的平均分是:" + groupSum/5);}System.out.println("全班总分是:" + classSum);System.out.println("全班平均分是:" + classSum/15);}

19、打星星(更容易理解嵌套循环哦)

public static void main(String[] args) {
//        //外部循环控制行
//        for (int i = 0; i < 5; i++) {
//            //控制列
//            //打星
//            for (int j = 0; j < 8; j++) {
//                System.out.print("*");
//            }
//            //打回车。换行
//            System.out.println();
//        }/*** ** * ** * * ** * * * ** * * * * ** * * * * * ** * * * * * * **///8行
//        for (int i = 0; i < 8; i++) {
//            //星
//            for (int j = 0; j < i+1; j++) {
//                System.out.print("*");
//            }
//            //回车
//            System.out.println();
//        }/***********************///6行
//        for (int i = 0; i < 6; i++) {
//            //空格
//            for (int j = 0; j < 6-i; j++) {
//                System.out.print(" ");
//            }
//            //星星
//            for (int k = 0; k < i+1; k++) {
//                System.out.print("*");
//            }
//            //回车
//            System.out.println();
//        }/*          i 空格  星星******     0   0    6*****     1   1    5****     2   2    4***     3   3    3**     4   4    2*     5   5    1*///        for (int i = 0; i < 6; i++) {
//            //空格
//            for (int j = 0; j < i; j++) {
//                System.out.print(" ");
//            }
//            //星星
//            for (int j = 0; j < 6-i; j++) {
//                System.out.print("*");
//            }
//            //回车
//            System.out.println();
//        }/*             i 空格  星星*         0   5    1***        1   4    3*****       2   3    5*******      3   2    7*********     4   1    9***********    5   0    11*/
//        for (int i = 0; i < 6; i++) {
//            //空格
//            for (int j = 0; j < 5-i; j++) {
//                System.out.print(" ");
//            }
//            //星星
//            for (int j = 0; j < 2 * i + 1; j++) {
//                System.out.print("*");
//            }
//            //回车
//            System.out.println();
//        }/*** **     **       **         **       **     ** ***/for (int i = 0; i < 6; i++) {//空格for (int j = 0; j < 5-i; j++) {System.out.print(" ");}//System.out.print("*");//空格
//            i   空
//            0   0
//            1   1
//            2   3
//            3   5
//            4   7
//            5   9for (int k = 0 ; k < 2*i - 1; k++){System.out.print(" ");}if (i>=1){System.out.print("*");}System.out.println("");}for (int i = 4; i >= 0; i--) {//空格//1  2 3 4 5for (int j = 0; j < 5-i; j++) {System.out.print(" ");}System.out.print("*");//空格//7 5 3 1 0for (int k = 0; k < 2*i-1; k++) {System.out.print(" ");}if (i>0){System.out.print("*");}System.out.println("");}}

20、从1加到100.(使用while实现)

        int i = 1;int sum = 0;while (i <= 100){sum += i;i++;}System.out.println(sum);

21、输入一个数字,0-10,输入正确则退出,输入错误则继续输入,三次机会。(使用while循环)。

        int j = 0;Scanner sc = new Scanner(System.in);while (j <3){System.out.println("请输入一个数字(0-10):");int num = sc.nextInt();if (num>=0&&num<=10){System.out.println("输入正确");break;}else{System.out.println("输入错误,你还有"+(2-j) + "次机会。");}j++;}

22、输入一个任意位数的正整数,打印出它是几位数。(使用while和do-while循环)

        Scanner sc = new Scanner(System.in);System.out.println("输入任意位数的正整数:");int num = sc.nextInt();int result = 0;while (true){num /= 10;result += 1;if (num == 0 ){break;}}使用do while 循环do{num /= 10;result += 1;if (num == 0 ){break;}}while (true);System.out.println("这是一个"+ result + "位数。");

23、使用while循环,实现猜数字游戏。

        Scanner sc = new Scanner(System.in);Random r = new Random();int num = r.nextInt(100);int i = 0;while (i<10){System.out.println("输入第"+(i+1) + "次猜测");int num1 = sc.nextInt();if (num>num1){System.out.println("猜小了。");}else if (num


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章