• java题目 ----选择结构


    01: 使用if选择结构判断一个整数,属于哪个范围:大于0;小于0;等于0

    1. import java.util.Scanner;
    2. /**
    3. * @Author: lly
    4. * @Description: 使用if选择结构判断一个整数,属于哪个范围:大于0;小于0;等于0
    5. * @DateTime: 2022/7/18 15:35
    6. **/
    7. public class Work1 {
    8. public static void main(String[] args) {
    9. Scanner sc = new Scanner(System.in);
    10. System.out.print("请输入一个整数:");
    11. int num = sc.nextInt();
    12. if(num>0){
    13. System.out.println(num + "大于零");
    14. }else if(num == 0){
    15. System.out.println(num + "等于零");
    16. }else {
    17. System.out.println(num +"小于零");
    18. }
    19. }
    20. }

    02:使用if选择结构判断一个整数是偶数还是奇数。

    1. import java.util.Scanner;
    2. /**
    3. * @Author: lly
    4. * @Description: 使用if选择结构判断一个整数是偶数还是奇数。
    5. * @DateTime: 2022/7/18 15:40
    6. **/
    7. public class Work2 {
    8. public static void main(String[] args) {
    9. Scanner sc = new Scanner(System.in);
    10. System.out.print("请输入一个整数:");
    11. int num = sc.nextInt();
    12. if (num % 2 ==0){
    13. System.out.println(num+"是偶数");
    14. }else {
    15. System.out.println(num +"是奇数");
    16. }
    17. }
    18. }

    03:使用if选择结构对三个整数进行排序,输出时按照从小到大的顺序输出。

    1. import java.util.Scanner;
    2. /**
    3. * @Author: lly
    4. * @Description: 使用if选择结构对三个整数进行排序,输出时按照从小到大的顺序输出。
    5. * @DateTime: 2022/7/18 15:42
    6. **/
    7. public class Work3 {
    8. public static void main(String[] args) {
    9. Scanner sc = new Scanner(System.in);
    10. System.out.print("请输入三个整数:");
    11. int num1= sc.nextInt();
    12. int num2= sc.nextInt();
    13. int num3= sc.nextInt();
    14. int max = 0;
    15. int min = 0;
    16. int middle = 0;
    17. if (num1>num2){
    18. if (num1>num3){
    19. max = num1;
    20. if(num3 > num2){
    21. middle = num3;
    22. min=num2;
    23. }else {
    24. middle = num2;
    25. min= num3;
    26. }
    27. }else {
    28. max = num3;
    29. middle=num1;
    30. min=num2;
    31. }
    32. }else{
    33. if (num2>num3){
    34. max = num2;
    35. if (num3>num1){
    36. middle = num3;
    37. min=num1;
    38. }else {
    39. max = num3;
    40. middle=num2;
    41. min=num1;
    42. }
    43. }
    44. }
    45. System.out.println("这三个数从小到大排列为:" + min +"\t"+ middle +"\t"+max);
    46. }
    47. }

    04:使用if选择结构判断一个三位的整数是否是水仙花数。

    水仙花数的条件:三位数为abc,则满足:a3+b3+c3=abc

    1. package HomeWork;
    2. import java.util.Scanner;
    3. /**
    4. * @Author: lly
    5. * @Description: 使用if选择结构判断一个三位的整数是否是水仙花数。水仙花数的条件:三位数为abc,则满足:a3+b3+c3=abc
    6. * @DateTime: 2022/7/18 16:05
    7. **/
    8. public class Work4 {
    9. public static void main(String[] args) {
    10. Scanner sc = new Scanner(System.in);
    11. System.out.print("请输入一个三位整数:");
    12. int code = sc.nextInt();
    13. int gw = code%10;
    14. int sw = code/10%10;
    15. int bw = code/100%10;
    16. if (gw*gw*gw+sw*sw*sw+bw*bw*bw ==code){
    17. System.out.println(code +"是水仙花数");
    18. }else {
    19. System.out.println(code+ "不是水仙花数");
    20. }
    21. }
    22. }

    05:使用if选择结构判断某一年份是否是闰年。

    闰年的条件:

    普通闰年:能被4整除但不能被100整除的年份为普通闰年。(如2004年就是闰年);

    世纪闰年:能被400整除的为世纪闰年。(如2000年是世纪闰年);

    1. import java.util.Scanner;
    2. /**
    3. * @Author: lly
    4. * @Description: 使用if选择结构判断某一年份是否是闰年。
    5. * 闰年的条件:
    6. * 普通闰年:能被4整除但不能被100整除的年份为普通闰年。(如2004年就是闰年);
    7. * 世纪闰年:能被400整除的为世纪闰年。(如2000年是世纪闰年);
    8. * @DateTime: 2022/7/18 16:10
    9. **/
    10. public class Work5 {
    11. public static void main(String[] args) {
    12. Scanner sc = new Scanner(System.in);
    13. System.out.print("请输入年份");
    14. int year = sc.nextInt();
    15. int flag = 0;
    16. if (year%4==0){
    17. if (year%100==0){
    18. flag = 0;
    19. }else {
    20. flag=1;
    21. }
    22. }
    23. if (year %400 == 0){
    24. flag =2;
    25. }
    26. if (flag==0){
    27. System.out.println(year+"不是闰年");
    28. }else if (flag == 1){
    29. System.out.println(year+"是普通闰年");
    30. }else {
    31. System.out.println(year + "是世纪闰年");
    32. }
    33. }
    34. }

    06:使用if选择结构判断一个4位整数,统计出此整数里面包含多少个偶数,多少个奇数

    1. import java.util.Scanner;
    2. /**
    3. * @Author: lly
    4. * @Description: 使用if选择结构判断一个4位整数,统计出此整数里面包含多少个偶数,多少个奇数
    5. * @DateTime: 2022/7/18 16:22
    6. **/
    7. public class Work6 {
    8. public static void main(String[] args) {
    9. Scanner sc = new Scanner(System.in);
    10. System.out.print("请输入一个四位整数:");
    11. int code = sc.nextInt();
    12. int gw = code%10;
    13. int sw = code/10%10;
    14. int bw = code/100%10;
    15. int qw = code/1000%10;
    16. int even=0;
    17. int odd = 0;
    18. if (gw%2==0){
    19. even+=1;
    20. }else {
    21. odd+=1;
    22. }
    23. if (sw%2==0){
    24. even+=1;
    25. }else {
    26. odd+=1;
    27. }
    28. if (bw%2==0){
    29. even+=1;
    30. }else {
    31. odd+=1;
    32. }
    33. if (qw%2==0){
    34. even+=1;
    35. }else {
    36. odd+=1;
    37. }
    38. System.out.println("偶数有" +even + "个");
    39. System.out.println("奇数有" +odd + "个");
    40. }
    41. }

    07:开发一个程序,根据公式(身高-108)*2=体重,可以有10斤左右的浮动。来观察测试者体重是否合适。

    1. import java.util.Scanner;
    2. /**
    3. * @Author: lly
    4. * @Description: 根据公式(身高-108)*2=体重,可以有10斤左右的浮动。来观察测试者体重是否合适。
    5. * @DateTime: 2022/7/18 16:30
    6. **/
    7. public class Work7 {
    8. public static void main(String[] args) {
    9. Scanner sc = new Scanner(System.in);
    10. System.out.print("请输入身高:");
    11. double height = sc.nextDouble();
    12. System.out.print("请输入体重:");
    13. double weight = sc.nextDouble();
    14. if (((height - 108)*2>=(weight-10)) && (((height - 108 )*2 <=(weight+10)))){
    15. System.out.println("体重合适");
    16. }
    17. else {
    18. System.out.println("体重不合适");
    19. }
    20. }
    21. }

    08: 判断此考试成绩为什么等级。

    90-100之间为优秀

    80-89之间为优良

    70-79之间为良好,

    60-69之间为及格

    60分以下为不及格。

    1. import java.util.Scanner;
    2. /**
    3. * @Author: lly
    4. * @Description: 判断此考试成绩为什么等级。
    5. * 90-100之间为优秀
    6. * 80-89之间为优良
    7. * 70-79之间为良好,
    8. * 60-69之间为及格
    9. * 60分以下为不及格。
    10. * @DateTime: 2022/7/18 16:37
    11. **/
    12. public class Work8 {
    13. public static void main(String[] args) {
    14. Scanner sc = new Scanner(System.in);
    15. System.out.print("请输入考试成绩:");
    16. int score = sc.nextInt();
    17. if (score>=90){
    18. System.out.println("优秀");
    19. }else if (score>=80){
    20. System.out.println("优良");
    21. }else if (score>=70){
    22. System.out.println("良好");
    23. }else if ( score>=60){
    24. System.out.println("及格");
    25. }else {
    26. System.out.println("不及格");
    27. }
    28. }
    29. }

    09: 写一个程序,输出类似09:03:12的时间格式,需要判断输入的数字是否符合实际情况,比如小时数就不能大于24,如果大于24就提示输入错误;分钟数和秒数不能大于60,如果大于60就提示输入错误。

    1. import java.util.Scanner;
    2. /**
    3. * @Author: lly
    4. * @Description: 写一个程序,输出类似09:03:12的时间格式,需要判断输入的数字是否符合实际情况,
    5. * 比如小时数就不能大于24,如果大于24就提示输入错误;分钟数和秒数不能大于60,
    6. * 如果大于60就提示输入错误。
    7. * @DateTime: 2022/7/18 16:51
    8. **/
    9. public class Work9 {
    10. public static void main(String[] args) {
    11. Scanner sc = new Scanner(System.in);
    12. while (true) {
    13. // 只要进入程序,就必须输入正确,不然别想退出程序
    14. System.out.print("请输入小时:");
    15. int hour = sc.nextInt();
    16. System.out.print("请输入分钟:");
    17. int minute = sc.nextInt();
    18. System.out.print("请输入秒数:");
    19. int second = sc.nextInt();
    20. if (hour>=0 && hour<=24 ){
    21. if (minute<60 && minute >0){
    22. if (second >0 && second <=60){
    23. System.out.println(hour +":" +minute +":" +second);
    24. }else {
    25. System.out.println("对不起,秒数输入错误!!!请重新输入!");
    26. }
    27. }else {
    28. System.out.println("对不起,分钟输入错误!!请重新输入!");
    29. }
    30. }else {
    31. System.out.println("对不起,小时输入错误!!!请重新输入!");
    32. }
    33. }
    34. }
    35. }

    10: 有3个整数,给出提示信息:

    能否创建三角形;

    如果能构建三角形,提示是直角三角形还是等边三角形等腰三角形还是普通三角形;

    最后输出三角形面积;

    1. import java.util.Scanner;
    2. /**
    3. * @Author: lly
    4. * @Description: 有3个整数,给出提示信息:
    5. * 能否创建三角形;
    6. * 如果能构建三角形,提示是直角三角形还是等边三角形等腰三角形还是普通三角形;
    7. * 最后输出三角形面积;
    8. * @DateTime: 2022/7/18 17:11
    9. **/
    10. public class Work10 {
    11. public static void main(String[] args) {
    12. Scanner sc = new Scanner(System.in);
    13. System.out.println("请输入三角形的三条边");
    14. int a = sc.nextInt();
    15. int b = sc.nextInt();
    16. int c = sc.nextInt();
    17. float s =(a+b+c)/2f;
    18. float S = (float) Math.sqrt(s*(s-a)*(s-b)*(s-c));
    19. if ((a+b)>c && (a+c)>b && (c+b)>a ){
    20. if (a==b && a==c){
    21. System.out.println("是等边三角形");
    22. System.out.println(S);
    23. }else if (a==b || a==c || b==c){
    24. System.out.println("是等腰三角形");
    25. System.out.println(S);
    26. }else if( ((a*a+b*b)==c*c )|| ((a*a+c*c)==b*b ) || ((c*c+b*b)==a*a ) ){
    27. System.out.println("是直角三角形");
    28. System.out.println(S);
    29. }else {
    30. System.out.println("普通三角形");
    31. System.out.println(S);
    32. }
    33. }else {
    34. System.out.println("不能构成三角形");
    35. }
    36. }
    37. }

    11:随机输入一个字母,如果是大写字母就转化为小写字母输出;如果是小写字母就转化为大写字母输出。


     

    1. import java.util.Scanner;
    2. /**
    3. * @Author: lly
    4. * @Description: 随机输入一个字母,如果是大写字母就转化为小写字母输出;如果是小写字母就转化为大写字母输出。
    5. * @DateTime: 2022/7/18 17:38
    6. **/
    7. public class Work11 {
    8. public static void main(String[] args) {
    9. Scanner sc = new Scanner(System.in);
    10. String ch = sc.next();
    11. char ch1 = ch.charAt(0);
    12. // System.out.println( (int)ch1);
    13. char ch2;
    14. if (ch1 >= 'A' && ch1 <= 'Z') {
    15. ch2 = (char) (ch1 + 32);
    16. System.out.println(ch2);
    17. } else if (ch1 >= 'a' && ch1 <= 'z') {
    18. ch2 = (char) (ch1 - 32);
    19. System.out.println(ch2);
    20. }
    21. }
    22. }

    12:使用if结构实现学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

    1. import java.util.Scanner;
    2. /**
    3. * @Author: lly
    4. * @Description: 使用if结构实现学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。
    5. * @DateTime: 2022/7/18 17:58
    6. **/
    7. public class Work12 {
    8. public static void main(String[] args) {
    9. Scanner sc = new Scanner(System.in);
    10. System.out.print("请输入成绩:");
    11. int score = sc.nextInt();
    12. if (score>=90){
    13. System.out.println("您的成绩为A");
    14. } else if (score>=60 ) {
    15. System.out.println("您的成绩为B");
    16. }else {
    17. System.out.println("您的成绩为C");
    18. }
    19. }
    20. }

    13:使用条件结构实现:岳灵珊同学参加到Java的学习,他父亲岳不群和母亲宁中则承诺:

    如果岳灵珊的考试成绩==1OO分,父亲给她买辆车

    如果岳灵珊的考试成绩>=90分,母亲给她买台笔记本电脑

    如果岳灵珊的考试成绩>=60分,母亲给她买部手机

    如果岳灵珊的考试成绩<60分,没有礼物

    1. import java.util.Scanner;
    2. /**
    3. * @Author: lly
    4. * @Description: 使用条件结构实现:岳灵珊同学参加到Java的学习,他父亲岳不群和母亲宁中则承诺:
    5. * 如果岳灵珊的考试成绩==1OO分,父亲给她买辆车
    6. * 如果岳灵珊的考试成绩>=90分,母亲给她买台笔记本电脑
    7. * 如果岳灵珊的考试成绩>=60分,母亲给她买部手机
    8. * 如果岳灵珊的考试成绩<60分,没有礼物
    9. * @DateTime: 2022/7/18 18:02
    10. **/
    11. public class Work13 {
    12. public static void main(String[] args) {
    13. Scanner sc = new Scanner(System.in);
    14. System.out.print("请输入岳灵珊的Java成绩");
    15. int score = sc.nextInt();
    16. if (score == 100){
    17. System.out.println("你爸给你买辆车");
    18. } else if (score>=90) {
    19. System.out.println("你妈给你买个笔记本电脑");
    20. } else if (score>=60) {
    21. System.out.println("你妈给你买个手机");
    22. }else {
    23. System.out.println("都没及格还想要礼物????");
    24. }
    25. }
    26. }

    14:使用条件结构实现,如果用户名等于字符‘青’,密码等于数字‘123’,就输出“欢迎你,青”,否则就输出“对不起,你不是青”。

    1. import java.util.Scanner;
    2. /**
    3. * @Author: lly
    4. * @Description: 使用条件结构实现,如果用户名等于字符‘青’,密码等于数字‘123’,就输出“欢迎你,青”,否则就输出“对不起,你不是青”。
    5. * @DateTime: 2022/7/18 18:12
    6. **/
    7. public class Work14 {
    8. public static void main(String[] args) {
    9. Scanner sc = new Scanner(System.in);
    10. System.out.print("请输入用户名:");
    11. String userName = sc.next();
    12. System.out.print("请输入密码:");
    13. int password = sc.nextInt();
    14. if (userName.equals("青") && password == 123){
    15. System.out.println("欢迎你,青");
    16. }else {
    17. System.out.println("对不起,你不是青");
    18. }
    19. }
    20. }

    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

    1. import java.util.Scanner;
    2. import static java.lang.Math.sqrt;
    3. /**
    4. * @Author: lly
    5. * @Description: 求ax2+bx+c=0方程的根。
    6. * a,b,c分别为函数的参数,
    7. * 如果:b2-4ac>0,则有两个解;b2-4ac=0,则有一个解;b2-4ac<0,则无解;
    8. * 已知:
    9. * x1=(-b+sqrt(b2-4ac))/2a
    10. * x2=(-b-sqrt(b2-4ac))/2a
    11. * @DateTime: 2022/7/18 18:18
    12. **/
    13. public class Work15 {
    14. public static void main(String[] args) {
    15. Scanner sc = new Scanner(System.in);
    16. System.out.println("请输入二次项系数a:");
    17. double a = sc.nextDouble();
    18. System.out.println("请输入一次项系数b:");
    19. double b = sc.nextDouble();
    20. System.out.println("请输入常数项c:");
    21. double c = sc.nextDouble();
    22. double x1=0;
    23. double x2=0;
    24. double v = b * b - 4 * a * c;
    25. if ( v >0 ){
    26. x1=(-b+sqrt(v)/2*a);
    27. x2=(-b-sqrt(v)/2*a);
    28. System.out.println("方程的两个根分别为:" + x1 + "\t"+x2);
    29. }else if (v==0){
    30. x1=-b/2*a;
    31. System.out.println("此方程有二重根为:" + x1);
    32. }else {
    33. System.out.println("此方程无根");
    34. }
    35. }
    36. }

    16:使用switch选择结构实现判断某年某月某日是这一年的第几天?

    1. import java.util.Scanner;
    2. public class Work16 {
    3. public static void main(String[] args) {
    4. /**/
    5. Scanner sc = new Scanner(System.in);
    6. System.out.println("请输入年份");
    7. int year = sc.nextInt();
    8. System.out.println("请输入月份");
    9. int month = sc.nextInt();
    10. System.out.println("请输入第几日");
    11. int day = sc.nextInt();
    12. int sum = 0;//记录总天数
    13. switch(month - 1){
    14. case 11: sum += 30;
    15. case 10: sum += 31;
    16. case 9: sum += 30;
    17. case 8: sum += 31;
    18. case 7: sum += 31;
    19. case 6: sum += 30;
    20. case 5: sum += 31;
    21. case 4: sum += 30;
    22. case 3: sum += 31;
    23. case 2: sum += 28;
    24. case 1: sum += 31;
    25. case 0: sum += day;
    26. }
    27. if(month > 2){
    28. if(year % 400 == 0 || year % 4 == 0 && year % 100 !=0){
    29. sum++;
    30. }
    31. System.out.println("第 " + sum+" 天");
    32. }
    33. }
    34. }

    17:使用switch选择结构判断一个月份属于一年中的那个季节。(春夏秋冬)

    1. import java.util.Scanner;
    2. /**
    3. * @Author: lly
    4. * @Description: 使用switch选择结构判断一个月份属于一年中的那个季节。(春夏秋冬)
    5. * @DateTime: 2022/7/18 18:36
    6. **/
    7. public class Work17 {
    8. public static void main(String[] args) {
    9. Scanner sc = new Scanner(System.in);
    10. System.out.print("请输入月份:");
    11. int month = sc.nextInt();
    12. switch (month){
    13. case 3: case 4: case 5:
    14. System.out.println("现在是春天");
    15. break;
    16. case 6: case 7: case 8:
    17. System.out.println("现在是夏天");
    18. break;
    19. case 9: case 10: case 11:
    20. System.out.println("现在是秋天");
    21. break;
    22. default:
    23. System.out.println("现在是冬天");
    24. }
    25. }
    26. }

  • 相关阅读:
    计算机网络复习总结2
    C++ 34 之 单例模式
    面向对象设计模式
    一文详解数据链路相关技术
    高级前端手写面试题
    设计模式(五)设计原则part2
    红队专题-从零开始VC++C/S远程控制软件RAT-MFC-超级终端
    rapidjson之内存分配器
    408每日一练:第一天
    how2heap2.31学习(1)
  • 原文地址:https://blog.csdn.net/weixin_53342789/article/details/126263048