Java 基本语法 switch-case 例题练习
2021/5/20 20:56:48
本文主要是介绍Java 基本语法 switch-case 例题练习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录
- 练习一
- 练习二
- 练习三
- 加练习
练习一
class Test { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("请输入一个小写字母:"); String str = scan.next(); char c = str.charAt(0); switch (c) { case 'a': System.out.print("A"); break; case 'b': System.out.print("B"); break; case 'c': System.out.print("C"); break; case 'd': System.out.print("D"); break; case 'e': System.out.print("E"); break; default: System.out.print("other"); break; } } }
结果
练习二
这样如此肯定不合理
package hh; import java.util.Scanner; public class test02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("请输入你的成绩:"); int score = scan.nextInt(); if(score > 60){ System.out.println("合格"); }else System.out.println("不合格"); } }
结果
程序
package hh; import java.util.Scanner; public class test02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("请输入你的成绩:"); int score = scan.nextInt(); int scoredemo = score/10; switch(scoredemo){ case 0: case 1: case 2: case 3: case 4: case 5: System.out.println("不合格"); break; case 6: case 7: case 8: case 9: case 10: System.out.println("合格"); break; } } }
结果
练习三
package hh; import java.util.Scanner; public class test02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入2019年月份"); int month = scan.nextInt(); System.out.println("请输入2019年日期"); int day = scan.nextInt(); int sumDays = 0; switch (month) { case 12: sumDays += 30; case 11: sumDays += 31; case 10: sumDays += 30; case 9: sumDays += 31; case 8: sumDays += 31; case 7: sumDays += 30; case 6: sumDays += 31; case 5: sumDays += 30; case 4: sumDays += 31; case 3: sumDays += 28; case 2: sumDays += 31; case 1: sumDays += day; } System.out.println(sumDays+"天"); } }
结果
倒着从12月份开始加天数,这样子更简单。
加练习
package hh; import java.util.Scanner; public class test02 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入年份"); int year = scan.nextInt(); System.out.println("请输入月份"); int month = scan.nextInt(); System.out.println("请输入几号"); int day = scan.nextInt(); int sumDays = 0; switch (month) { case 12: sumDays += 30; case 11: sumDays += 31; case 10: sumDays += 30; case 9: sumDays += 31; case 8: sumDays += 31; case 7: sumDays += 30; case 6: sumDays += 31; case 5: sumDays += 30; case 4: sumDays += 31; case 3: //判断是否是闰年 if((year % 4 == 0 && year % 100 != 0)|| year % 400 == 0){ sumDays += 29; }else sumDays += 28; case 2: sumDays += 31; case 1: sumDays += day; } System.out.println(year + "年" + month + "月" + day + "日是当年的第" + sumDays+ "天" ); } }
结果
这篇关于Java 基本语法 switch-case 例题练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程
- 2024-11-26Springboot单体架构搭建资料:新手入门教程
- 2024-11-26Springboot单体架构搭建资料详解与实战教程
- 2024-11-26Springboot框架资料:新手入门教程
- 2024-11-26Springboot企业级开发资料入门教程
- 2024-11-26SpringBoot企业级开发资料详解与实战教程
- 2024-11-26Springboot微服务资料:新手入门全攻略