Java基础-day03

mac2026-01-16  5

练习一 判断3位水仙花数

public class Demo_07_Loop_Daffodil { //打印3位水仙花数 //3位水仙花数:三位数,每一位的三次方相加的和等于这个三位数 public static void main(String[] args) { /* //for循环实现 for (int i = 100; i < 1000; i++) { int a = i % 10; int b = i / 10 % 10; int c = i / 100; if ( a*a*a + b*b*b + c*c*c == i) { System.out.println(i); } } */ //while循环实现 int i = 100; while (i < 1000) { int a = i % 10; //获取个位数 int b = i / 10 % 10; //获取十位数 int c = i / 100; //获取百位数 if ( a*a*a + b*b*b + c*c*c == i) { System.out.println(i); } i++; } } }

练习二 统计3位水仙花数的个数

public class Demo_08_Loop_CountDaffodil { //统计3位水仙花数的个数 //3位水仙花数:三位数,每一位的三次方相加的和等于这个三位数 public static void main(String[] args) { int count = 0; /* //for循环实现 for (int i = 100; i < 1000; i++) { int a = i % 10; int b = i / 10 % 10; int c = i / 100; if ( a*a*a + b*b*b + c*c*c == i) { count ++; } } System.out.println("共有" + count + "个水仙花数"); */ //while循环实现 int i = 100; while (i < 1000) { int a = i % 10; //获取个位数 int b = i / 10 % 10; //获取十位数 int c = i / 100; //获取百位数 if (a * a * a + b * b * b + c * c * c == i) { count ++; } i++; } System.out.println("共有" + count + "个水仙花数"); } }

练习四 continue练习(逢7过)

public class Demo_13_Loop_Continue { //逢7过 public static void main(String[] args) { for (int i = 1; i <= 21; i++) { if ((i % 7 == 0) || (i % 10 == 7)){ //含7或7的倍数就跳过 System.out.println("pass"); continue; } System.out.println(i); } } }

练习五 输出指定范围内的随机数

import java.util.Random; public class RandomInRound { //生成指定范围内(50-80)的随机数 public static void main(String[] args) { int num; Random r = new Random(); num = r.nextInt(31) + 50; //r.nextInt(31)是生成从0到30共31个随机数 /* * 公式: int x = r.nextInt(上限 - 下限 + 1) + 下限; */ System.out.println(num); } }

练习六 猜数字游戏

import java.util.Random; import java.util.Scanner; public class NumberGame { public static void main(String[] args) { int num,guess; Random rd = new Random(); Scanner sc = new Scanner(System.in); num = rd.nextInt(10) + 1; while (true) { System.out.println("请输入你要猜的数字(1~10):"); guess = sc.nextInt(); if (guess >= 1 && guess <= 10) { //判断输入内容是否合法 if (guess > num) { System.out.printf("您猜的数字%d大了,再猜一次吧。\n",guess); } else if (guess < num) { System.out.printf("您猜的数字%d小了,再猜一次吧。\n", guess); } else { System.out.println("恭喜你猜对了!"); break; } } else { System.out.println("输入有误,请输入1~10之间的数字"); } } } }

作业一 按需求打印1~100内不包含九的数字

public class homework_03 { /* 分析以下需求并实现 1.打印1到100之内的整数,但数字中包含9的要跳过 2.每行输出5个满足条件的数,之间用空格分隔 3.如:1 2 3 4 5 */ public static void main(String[] args) { int count = 0; for (int i = 1; i <= 100; i++) { if (i % 10 == 9 || i / 10 == 9) { //个位或者十位上有9的都跳过 continue; } System.out.print(i + " "); //每个数字后跟一个空格 count++; if (count % 5 == 0){ System.out.println(""); //每5个数字输出一个换行,实现每行输出五个数 } } } }

作业二 打印输出四位数中满足要求的所有数和总个数

public class homework_04 { /* 分析以下需求,并用代码实现: 1.按照从大到小的顺序输出四位数中的个位+百位=十位+千位 的数字及个数 2.每行输出5个满足条件的数,之间用空格分隔 3.如:9999 9988 9977 9966 9955 */ public static void main(String[] args) { int count = 0; for (int i = 1000; i < 10000; i++) { int ge = i % 10; //得到个位数 int shi = i % 100 / 10; //得到十位数 int bai = i % 1000 / 100; //得到百位数 int qian = i / 1000; //得到千位数 if ((ge + bai) == (shi + qian)) { //如果满足位+百位=十位+千位,执行以下代码 count++; System.out.print(i + " "); //每个数字后跟一个空格 if (count % 5 == 0) { System.out.println(""); //每打印五个数字进行一次换行 } } } System.out.printf("一共有%d个数满足个位+百位=十位+千位\n",count); } }

作业三 打印四行五列的星号

public class homework_05 { /* 打印四行五列的星星 ***** ***** ***** ***** */ 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(""); //每打印五个*后进行换行 } } }

tips:相较于while循环和for循环,do…while循环的判断语句会少执行一次

最新回复(0)