01: 使用if选择结构判断一个整数,属于哪个范围:大于0;小于0;等于0
- import java.util.Scanner;
- /**
- * @Author: lly
- * @Description: 使用if选择结构判断一个整数,属于哪个范围:大于0;小于0;等于0
- * @DateTime: 2022/7/18 15:35
- **/
- public class Work1 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入一个整数:");
- int num = sc.nextInt();
- if(num>0){
- System.out.println(num + "大于零");
- }else if(num == 0){
- System.out.println(num + "等于零");
- }else {
- System.out.println(num +"小于零");
- }
- }
- }
02:使用if选择结构判断一个整数是偶数还是奇数。
- import java.util.Scanner;
-
- /**
- * @Author: lly
- * @Description: 使用if选择结构判断一个整数是偶数还是奇数。
- * @DateTime: 2022/7/18 15:40
- **/
- public class Work2 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入一个整数:");
- int num = sc.nextInt();
- if (num % 2 ==0){
- System.out.println(num+"是偶数");
- }else {
- System.out.println(num +"是奇数");
- }
- }
- }
03:使用if选择结构对三个整数进行排序,输出时按照从小到大的顺序输出。
- import java.util.Scanner;
-
- /**
- * @Author: lly
- * @Description: 使用if选择结构对三个整数进行排序,输出时按照从小到大的顺序输出。
- * @DateTime: 2022/7/18 15:42
- **/
- public class Work3 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入三个整数:");
- int num1= sc.nextInt();
- int num2= sc.nextInt();
- int num3= sc.nextInt();
- int max = 0;
- int min = 0;
- int middle = 0;
- if (num1>num2){
- if (num1>num3){
- max = num1;
- if(num3 > num2){
- middle = num3;
- min=num2;
- }else {
- middle = num2;
- min= num3;
- }
- }else {
- max = num3;
- middle=num1;
- min=num2;
- }
- }else{
- if (num2>num3){
- max = num2;
- if (num3>num1){
- middle = num3;
- min=num1;
- }else {
-
- max = num3;
- middle=num2;
- min=num1;
- }
- }
- }
- System.out.println("这三个数从小到大排列为:" + min +"\t"+ middle +"\t"+max);
- }
- }
04:使用if选择结构判断一个三位的整数是否是水仙花数。
水仙花数的条件:三位数为abc,则满足:a3+b3+c3=abc
- package HomeWork;
-
- import java.util.Scanner;
-
- /**
- * @Author: lly
- * @Description: 使用if选择结构判断一个三位的整数是否是水仙花数。水仙花数的条件:三位数为abc,则满足:a3+b3+c3=abc
- * @DateTime: 2022/7/18 16:05
- **/
- public class Work4 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入一个三位整数:");
- int code = sc.nextInt();
- int gw = code%10;
- int sw = code/10%10;
- int bw = code/100%10;
-
- if (gw*gw*gw+sw*sw*sw+bw*bw*bw ==code){
- System.out.println(code +"是水仙花数");
- }else {
- System.out.println(code+ "不是水仙花数");
- }
- }
- }
-
05:使用if选择结构判断某一年份是否是闰年。
闰年的条件:
普通闰年:能被4整除但不能被100整除的年份为普通闰年。(如2004年就是闰年);
世纪闰年:能被400整除的为世纪闰年。(如2000年是世纪闰年);
- import java.util.Scanner;
-
- /**
- * @Author: lly
- * @Description: 使用if选择结构判断某一年份是否是闰年。
- * 闰年的条件:
- * 普通闰年:能被4整除但不能被100整除的年份为普通闰年。(如2004年就是闰年);
- * 世纪闰年:能被400整除的为世纪闰年。(如2000年是世纪闰年);
- * @DateTime: 2022/7/18 16:10
- **/
- public class Work5 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入年份");
- int year = sc.nextInt();
- int flag = 0;
- if (year%4==0){
- if (year%100==0){
- flag = 0;
- }else {
- flag=1;
- }
- }
- if (year %400 == 0){
- flag =2;
- }
-
- if (flag==0){
- System.out.println(year+"不是闰年");
- }else if (flag == 1){
- System.out.println(year+"是普通闰年");
- }else {
- System.out.println(year + "是世纪闰年");
- }
-
- }
- }
06:使用if选择结构判断一个4位整数,统计出此整数里面包含多少个偶数,多少个奇数
- import java.util.Scanner;
- /**
- * @Author: lly
- * @Description: 使用if选择结构判断一个4位整数,统计出此整数里面包含多少个偶数,多少个奇数
- * @DateTime: 2022/7/18 16:22
- **/
- public class Work6 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入一个四位整数:");
- int code = sc.nextInt();
- int gw = code%10;
- int sw = code/10%10;
- int bw = code/100%10;
- int qw = code/1000%10;
- int even=0;
- int odd = 0;
- if (gw%2==0){
- even+=1;
- }else {
- odd+=1;
- }
-
- if (sw%2==0){
- even+=1;
- }else {
- odd+=1;
- }
-
- if (bw%2==0){
- even+=1;
- }else {
- odd+=1;
- }
-
- if (qw%2==0){
- even+=1;
- }else {
- odd+=1;
- }
-
- System.out.println("偶数有" +even + "个");
- System.out.println("奇数有" +odd + "个");
-
- }
- }
07:开发一个程序,根据公式(身高-108)*2=体重,可以有10斤左右的浮动。来观察测试者体重是否合适。
- import java.util.Scanner;
- /**
- * @Author: lly
- * @Description: 根据公式(身高-108)*2=体重,可以有10斤左右的浮动。来观察测试者体重是否合适。
- * @DateTime: 2022/7/18 16:30
- **/
- public class Work7 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入身高:");
- double height = sc.nextDouble();
- System.out.print("请输入体重:");
- double weight = sc.nextDouble();
-
- if (((height - 108)*2>=(weight-10)) && (((height - 108 )*2 <=(weight+10)))){
- System.out.println("体重合适");
- }
- else {
- System.out.println("体重不合适");
- }
- }
- }
08: 判断此考试成绩为什么等级。
90-100之间为优秀
80-89之间为优良
70-79之间为良好,
60-69之间为及格
60分以下为不及格。
- import java.util.Scanner;
-
- /**
- * @Author: lly
- * @Description: 判断此考试成绩为什么等级。
- * 90-100之间为优秀
- * 80-89之间为优良
- * 70-79之间为良好,
- * 60-69之间为及格
- * 60分以下为不及格。
- * @DateTime: 2022/7/18 16:37
- **/
- public class Work8 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入考试成绩:");
- int score = sc.nextInt();
-
- if (score>=90){
- System.out.println("优秀");
- }else if (score>=80){
- System.out.println("优良");
- }else if (score>=70){
- System.out.println("良好");
- }else if ( score>=60){
- System.out.println("及格");
- }else {
- System.out.println("不及格");
- }
- }
- }
09: 写一个程序,输出类似09:03:12的时间格式,需要判断输入的数字是否符合实际情况,比如小时数就不能大于24,如果大于24就提示输入错误;分钟数和秒数不能大于60,如果大于60就提示输入错误。
- import java.util.Scanner;
-
- /**
- * @Author: lly
- * @Description: 写一个程序,输出类似09:03:12的时间格式,需要判断输入的数字是否符合实际情况,
- * 比如小时数就不能大于24,如果大于24就提示输入错误;分钟数和秒数不能大于60,
- * 如果大于60就提示输入错误。
- * @DateTime: 2022/7/18 16:51
- **/
- public class Work9 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- while (true) {
- // 只要进入程序,就必须输入正确,不然别想退出程序
- System.out.print("请输入小时:");
- int hour = sc.nextInt();
- System.out.print("请输入分钟:");
- int minute = sc.nextInt();
- System.out.print("请输入秒数:");
- int second = sc.nextInt();
- if (hour>=0 && hour<=24 ){
- if (minute<60 && minute >0){
- if (second >0 && second <=60){
- System.out.println(hour +":" +minute +":" +second);
- }else {
- System.out.println("对不起,秒数输入错误!!!请重新输入!");
- }
- }else {
- System.out.println("对不起,分钟输入错误!!请重新输入!");
- }
- }else {
- System.out.println("对不起,小时输入错误!!!请重新输入!");
- }
- }
- }
- }
10: 有3个整数,给出提示信息:
能否创建三角形;
如果能构建三角形,提示是直角三角形还是等边三角形等腰三角形还是普通三角形;
最后输出三角形面积;
- import java.util.Scanner;
-
- /**
- * @Author: lly
- * @Description: 有3个整数,给出提示信息:
- * 能否创建三角形;
- * 如果能构建三角形,提示是直角三角形还是等边三角形等腰三角形还是普通三角形;
- * 最后输出三角形面积;
- * @DateTime: 2022/7/18 17:11
- **/
- public class Work10 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入三角形的三条边");
- int a = sc.nextInt();
- int b = sc.nextInt();
- int c = sc.nextInt();
- float s =(a+b+c)/2f;
- float S = (float) Math.sqrt(s*(s-a)*(s-b)*(s-c));
- if ((a+b)>c && (a+c)>b && (c+b)>a ){
- if (a==b && a==c){
- System.out.println("是等边三角形");
- System.out.println(S);
- }else if (a==b || a==c || b==c){
- System.out.println("是等腰三角形");
- System.out.println(S);
- }else if( ((a*a+b*b)==c*c )|| ((a*a+c*c)==b*b ) || ((c*c+b*b)==a*a ) ){
- System.out.println("是直角三角形");
- System.out.println(S);
- }else {
- System.out.println("普通三角形");
- System.out.println(S);
- }
- }else {
- System.out.println("不能构成三角形");
- }
- }
- }
11:随机输入一个字母,如果是大写字母就转化为小写字母输出;如果是小写字母就转化为大写字母输出。
- import java.util.Scanner;
-
- /**
- * @Author: lly
- * @Description: 随机输入一个字母,如果是大写字母就转化为小写字母输出;如果是小写字母就转化为大写字母输出。
- * @DateTime: 2022/7/18 17:38
- **/
- public class Work11 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String ch = sc.next();
- char ch1 = ch.charAt(0);
- // System.out.println( (int)ch1);
- char ch2;
- if (ch1 >= 'A' && ch1 <= 'Z') {
- ch2 = (char) (ch1 + 32);
- System.out.println(ch2);
- } else if (ch1 >= 'a' && ch1 <= 'z') {
- ch2 = (char) (ch1 - 32);
- System.out.println(ch2);
- }
-
- }
- }
12:使用if结构实现学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
- import java.util.Scanner;
- /**
- * @Author: lly
- * @Description: 使用if结构实现学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
- * @DateTime: 2022/7/18 17:58
- **/
- public class Work12 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入成绩:");
- int score = sc.nextInt();
- if (score>=90){
- System.out.println("您的成绩为A");
- } else if (score>=60 ) {
- System.out.println("您的成绩为B");
-
- }else {
- System.out.println("您的成绩为C");
- }
- }
- }
13:使用条件结构实现:岳灵珊同学参加到Java的学习,他父亲岳不群和母亲宁中则承诺:
如果岳灵珊的考试成绩==1OO分,父亲给她买辆车
如果岳灵珊的考试成绩>=90分,母亲给她买台笔记本电脑
如果岳灵珊的考试成绩>=60分,母亲给她买部手机
如果岳灵珊的考试成绩<60分,没有礼物
- import java.util.Scanner;
-
- /**
- * @Author: lly
- * @Description: 使用条件结构实现:岳灵珊同学参加到Java的学习,他父亲岳不群和母亲宁中则承诺:
- * 如果岳灵珊的考试成绩==1OO分,父亲给她买辆车
- * 如果岳灵珊的考试成绩>=90分,母亲给她买台笔记本电脑
- * 如果岳灵珊的考试成绩>=60分,母亲给她买部手机
- * 如果岳灵珊的考试成绩<60分,没有礼物
- * @DateTime: 2022/7/18 18:02
- **/
- public class Work13 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入岳灵珊的Java成绩");
- int score = sc.nextInt();
- if (score == 100){
- System.out.println("你爸给你买辆车");
- } else if (score>=90) {
- System.out.println("你妈给你买个笔记本电脑");
- } else if (score>=60) {
- System.out.println("你妈给你买个手机");
- }else {
- System.out.println("都没及格还想要礼物????");
- }
- }
- }
-
14:使用条件结构实现,如果用户名等于字符‘青’,密码等于数字‘123’,就输出“欢迎你,青”,否则就输出“对不起,你不是青”。
- import java.util.Scanner;
-
- /**
- * @Author: lly
- * @Description: 使用条件结构实现,如果用户名等于字符‘青’,密码等于数字‘123’,就输出“欢迎你,青”,否则就输出“对不起,你不是青”。
- * @DateTime: 2022/7/18 18:12
- **/
- public class Work14 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入用户名:");
- String userName = sc.next();
- System.out.print("请输入密码:");
- int password = sc.nextInt();
- if (userName.equals("青") && password == 123){
- System.out.println("欢迎你,青");
- }else {
- System.out.println("对不起,你不是青");
- }
- }
- }
15:求ax2+bx+c=0方程的根。
a,b,c分别为函数的参数,
如果:b2-4ac>0,则有两个解;b2-4ac=0,则有一个解;b2-4ac<0,则无解;
已知:
x1=(-b+sqrt(b2-4ac))/2a
x2=(-b-sqrt(b2-4ac))/2a
- import java.util.Scanner;
-
- import static java.lang.Math.sqrt;
-
- /**
- * @Author: lly
- * @Description: 求ax2+bx+c=0方程的根。
- * a,b,c分别为函数的参数,
- * 如果:b2-4ac>0,则有两个解;b2-4ac=0,则有一个解;b2-4ac<0,则无解;
- * 已知:
- * x1=(-b+sqrt(b2-4ac))/2a
- * x2=(-b-sqrt(b2-4ac))/2a
- * @DateTime: 2022/7/18 18:18
- **/
- public class Work15 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入二次项系数a:");
- double a = sc.nextDouble();
- System.out.println("请输入一次项系数b:");
- double b = sc.nextDouble();
- System.out.println("请输入常数项c:");
- double c = sc.nextDouble();
- double x1=0;
- double x2=0;
- double v = b * b - 4 * a * c;
- if ( v >0 ){
- x1=(-b+sqrt(v)/2*a);
- x2=(-b-sqrt(v)/2*a);
- System.out.println("方程的两个根分别为:" + x1 + "\t"+x2);
- }else if (v==0){
- x1=-b/2*a;
- System.out.println("此方程有二重根为:" + x1);
- }else {
- System.out.println("此方程无根");
- }
- }
- }
16:使用switch选择结构实现判断某年某月某日是这一年的第几天?
- import java.util.Scanner;
-
- public class Work16 {
- public static void main(String[] args) {
-
- /**/
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入年份");
- int year = sc.nextInt();
- System.out.println("请输入月份");
- int month = sc.nextInt();
- System.out.println("请输入第几日");
- int day = sc.nextInt();
- int sum = 0;//记录总天数
- switch(month - 1){
- case 11: sum += 30;
- case 10: sum += 31;
- case 9: sum += 30;
- case 8: sum += 31;
- case 7: sum += 31;
- case 6: sum += 30;
- case 5: sum += 31;
- case 4: sum += 30;
- case 3: sum += 31;
- case 2: sum += 28;
- case 1: sum += 31;
- case 0: sum += day;
- }
- if(month > 2){
- if(year % 400 == 0 || year % 4 == 0 && year % 100 !=0){
- sum++;
- }
- System.out.println("第 " + sum+" 天");
-
- }
- }
- }
17:使用switch选择结构判断一个月份属于一年中的那个季节。(春夏秋冬)
- import java.util.Scanner;
-
- /**
- * @Author: lly
- * @Description: 使用switch选择结构判断一个月份属于一年中的那个季节。(春夏秋冬)
- * @DateTime: 2022/7/18 18:36
- **/
- public class Work17 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入月份:");
- int month = sc.nextInt();
- switch (month){
- case 3: case 4: case 5:
- System.out.println("现在是春天");
- break;
- case 6: case 7: case 8:
- System.out.println("现在是夏天");
- break;
- case 9: case 10: case 11:
- System.out.println("现在是秋天");
- break;
- default:
- System.out.println("现在是冬天");
- }
- }
- }
-