0
点赞
收藏
分享

微信扫一扫

20220324 java基础代码题(一)

你的益达233 2022-03-24 阅读 47

1)划分成绩等级 将一、二年级学生的学生成绩划分等级,等级划分(用if  else)

public class ScoreLevel {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入您的分数:");
		int score = sc.nextInt();
		if (score >= 90 && score <= 100) {
			System.out.println("您的分数" + score + "对应的等级为“优秀”");
		} else if (score >= 80 && score < 90) {
			System.out.println("您的分数" + score + "对应的等级为“良好”");
		} else if (score >= 60 && score < 80) {
			System.out.println("您的分数" + score + "对应的等级为“合格”");
		} else if (score < 60 && score >= 0) {
			System.out.println("您的分数" + score + "对应的等级为“不合格”");
		} else {
			System.out.println("无效成绩");
		}
		sc.close();
	}
}

2)判断月份对应的季节 使用switch多分支语句判断某个月份属于哪个季节 

public class Seasons {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.print("请输入某个月份(1~12):");
		int month = sc.nextInt();
		if (month < 1 || month > 12) {
			System.out.println("警告:您在非法操作……");
		} else {
			switch (month) {
			case 3:
			case 4:
			case 5:
				System.out.println(month + "月正值春季");
				break;
			case 6:
			case 7:
			case 8:
				System.out.println(month + "月正值夏季");
				break;
			case 9:
			case 10:
			case 11:
				System.out.println(month + "月正值秋季");
				break;
			case 12:
			case 1:
			case 2:
				System.out.println(month + "月正值冬季");
				break;
			}
		}
		sc.close();
	}
}

3)细胞分裂实验 生物实验室做单细胞细菌繁殖实验,每一代细菌数量都会成倍数增长,一代菌落中只有一个细菌,二代菌落中分裂成两个细菌,三代菌落中分裂成4个细菌,以此类推,请计算第十代菌落中的细菌数量(用while)

public class SingleCellReproduction {
	public static void main(String[] args) {
		int cellNum =1,time = 1 ;
		while(time < 10){
			cellNum *= 2;
			time++;
		}
		System.out.println("第" + time + "代菌落中的细菌数量:" + cellNum + "个。");
	}
}

4)斐波那切数列 1、1、2、3、5、8、13、21、34、…是一组典型的斐波那契数列,前两个数相加等于第三个数。那么请问这组数中的第n个数的值是多少?

public class Arithmetic {
	public static void main(String[] args) {
		int num1 = 1, num2 = 1;// 前两个数字
		Scanner sc = new Scanner(System.in);// 创建扫描器对象,用于输入
		System.out.print("请输入要查看第n个数中的n值:");
		int n = sc.nextInt();// 记录用户输入的数字
		for (int i = 2; i < n; i++) {// 从2开始
			num1 = num1 + num2;// 后一个数字是前两个数字之和
			// 交换num1和num2的值
			int temp = num1;
			num1 = num2;
			num2 = temp;
		}
		System.out.println("第" + n + "个数为" + num2);// 输出指定位数上的数据
		sc.close();
	}
}

5)蜗牛爬井 有一口井,深10米,一只蜗牛从井底向井口爬,白天向上爬2米,晚上向下滑1米,问多少天可以爬到井口

public class Snail {
	public static void main(String[] args) {
		int distance = 0;
		int day = 1;
		while (true) {
			distance += 2;
			if (distance >= 10) {
				break;
			}
			distance -= 1;
			day++;
		}
		System.out.println("蜗牛第" + day + "天爬到了井口。");
	}
}

6)剧院售票 某剧院发售演出门票,演播厅观众席有4行,每行有10个座位。为了不影响观众视角,在发售门票时,屏蔽掉最左一列和最右一列的座位。请编写程序,结合本节知识点模拟整个售票过程

public class SellSeats {
	public static void main(String[] args) {
		System.out.println("剧院可售座位信息:");
		for (int i = 1; i <= 4; i++) {
			for (int j = 1; j <= 10; j++) {
				if (j == 1 || j == 10) {
					continue;
				} else {
					System.out.println("第" + i + "排,第 " + j + "列可售");
				}
			}
		}
	}
}

7)输出菱形 编写Java程序,使用for循环打印菱形

public class Diamond {
	public static void main(String args[]) {
		int lineCount = 17;// 输出的菱形有多少行,请赋值成奇数

		int maxLineNum = (lineCount + 1) / 2;// 菱形最多一行
		for (int i = 1; i <= maxLineNum; i++) {// 循环菱形数量越来越多的几行
			for (int space = 1; space <= maxLineNum - i; space++) {// 输出空格,数量=最后一行-当前行数
				System.out.print("  ");
			}
			for (int star = 1; star <= (i * 2) - 1; star++) {// 输出星号,数量=行数*2-1
				System.out.print("* ");
			}
			System.out.println();// 换行
		}

		int declineCount = lineCount - maxLineNum;// 计算剩下的几行,这几行星号的数量是递的
		for (int i = 1; i <= declineCount; i++) {// 循环菱形数量越来越少的行数
			for (int space = 1; space <= i; space++) {// 输出空格,数量等于当前的行数
				System.out.print("  ");
			}
			for (int star = 1; star <= (declineCount - i + 1) * 2 - 1; star++) {// 输出星号,数量等于(总数-当前行数)*2-1
				System.out.print("* ");
			}
			System.out.println();
		}

	}
}

8)无重复组合 使用for循环,在控制台输出由4、5、6、7能组成多少个互不相同且无重复数字的三位数?都是多少

public class RepeatNumber {
	public static void main(String[] args) {
		int bai = 0; // 百位上的数字
		int shi = 0; // 十位上的数字
		int ge = 0; // 个位上的数字
		int number = 0; // 数字的个数
		for (bai = 4; bai <= 7; bai++) {
			for (shi = 4; shi <= 7; shi++) {
				for (ge = 4; ge <= 7; ge++) {
					if (bai != shi && shi != ge && bai != ge) {
						number += 1;
						System.out.println(bai * 100 + shi * 10 + ge);
					}
				}
			}
		}
		System.out.println("总共能够组成" + number + "个数字!");
	}
}

9) 查找素数 使用for循环,判断1~100有多少个素数,并在控制台上输出所有素数。

public class SingleNum {
	public static void main(String[] args) {
		int count = 0;
		for (int i = 1; i < 101; i++) {
			// 默认是素数
			boolean flag = true;
			for (int j = 2; j <= Math.sqrt(i); j++) {
				if (i % j == 0) {
					// 能整除
					flag = false;
				}
			}
			if (flag) {
				count += 1;
				System.out.print(i + ",");
			}
		}
		System.out.println("\n共计" + count + "个素数");
	}
}

10)判断生肖 使用switch多分支语句判断某一年对应的中国生肖

public class Year {
	public static void main(String[] args) {
		int year = 2021;//年份
		switch (year % 12) {
		case 0:
			System.out.println(year + "年的生肖是猴");
			break;
		case 1:
			System.out.println(year + "年的生肖是鸡");
			break;
		case 2:
			System.out.println(year + "年的生肖是狗");
			break;
		case 3:
			System.out.println(year + "年的生肖是猪");
			break;
		case 4:
			System.out.println(year + "年的生肖是鼠");
			break;
		case 5:
			System.out.println(year + "年的生肖是牛");
			break;
		case 6:
			System.out.println(year + "年的生肖是虎");
			break;
		case 7:
			System.out.println(year + "年的生肖是兔");
			break;
		case 8:
			System.out.println(year + "年的生肖是龙");
			break;
		case 9:
			System.out.println(year + "年的生肖是蛇");
			break;
		case 10:
			System.out.println(year + "年的生肖是马");
			break;
		case 11:
			System.out.println(year + "年的生肖是羊");
			break;
		}
	}
}

10)摄氏度转华氏度 使用do…while循环,在控制台输出摄氏温度与华氏温度的对照表。对照表从摄氏温度-30℃到50℃,每行间隔10℃

public class Temperature {
	public static void main(String[] args) {
		int centigrade = -40; // 摄氏度的初始值为-40
		double fahrenheit; // 华氏度
		do {
			centigrade += 10; // 每行间隔10度
			fahrenheit = centigrade * 9 / 5.0 + 32; // 由摄氏度转为华氏度的公式
			System.out.println("摄氏温度:" + centigrade + "℃\t华氏温度:" + fahrenheit + "℉");
		} while (centigrade < 50);
	}
}

11) 百钱买百鸡 5文钱可以买一只公鸡,3文钱可以买1只母鸡,1文钱可以买3只雏鸡,现在用100文钱买100只鸡,那么公鸡、母鸡、雏鸡各有多少只?

public class BuyChicken {// 百钱买百鸡
	public static void main(String[] args) {
		int cock, hen, chick;// 公鸡、母鸡、小鸡
		for (cock = 0; cock <= 20; cock++) {// 最多买20只公鸡
			for (hen = 0; hen <= 33; hen++) {// 最多买33只母鸡
				for (chick = 3; chick <= 99; chick += 3) {// 最多买99只小鸡(有“百鸡”的限制)
					if (5 * cock + 3 * hen + chick / 3 == 100) {// 百钱
						if (cock + hen + chick == 100) {// 百鸡
							System.out.println("公鸡:" + cock + "\t母鸡:" + hen + "\t小鸡:" + chick);
						}
					}
				}
			}
		}
	}
}
举报

相关推荐

0 条评论