[java]枚举类练习
2022/5/2 1:42:40
本文主要是介绍[java]枚举类练习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
emm...很
想法:
- 枚举类实现季节信息打印
- 一个简单的交互
- 一个很艹的剧情
上代码
enum Season
package com.wenjun.www; public enum Season { SPRING("温暖","三月"),SUMMER("炎热","六月"),AUTUMN("凉爽","八月"),WINTER("寒冷","十二月"); @SuppressWarnings("all") private String intro = ""; @SuppressWarnings("all") private String month = ""; Test(String intro, String mouth) { this.intro = intro; this.mouth = month; } @Override public String toString() { return this.name().toLowerCase()+ "在"+month+"最"+intro; } }
对吧,直接重写toString()
打印完事。不要在意@SuppressWarnings("all")
[滑稽]
class mainMenu
package com.wenjun.www; import com.wenjun.www.Season; import java.util.Scanner; public class mainMenu { private boolean appear = true; private Scanner choice = new Scanner(System.in); private String seasonName = ""; private Season[] seasons = Season.values(); private void seasonList(){ for (Season value : seasons) { System.out.println(value); } } public void menu(){ do { System.out.println("燕子,你喜欢哪个季节?"); seasonList(); System.out.println("选一个,不行让我滚!"); season_name = choice.next(); switch (seasonName){ case "spring": findSeason(0); break; case "summer": findSeason(1); break; case "autumn": findSeason(2); break; case "winter": findSeason(3); break; case "滚": System.out.println("燕子!你别走啊燕子!"); System.out.println("司机:“确定要加速吗?”(y/n):"); seasonName = choice.next(); if (seasonName.toUpperCase().equals("Y")){ appear = false; } break; default: System.out.println("燕子!你说句话啊燕子!"); break; } }while (appear); } private void findSeason(int id){ System.out.println(seasons[id]); } }
Season[] seasons = Season.values();
直接全装进去seasonList()
增强for循环打印menu()
中switch+Scanner简单交互findSeason()
数组下标查询打印- 生草的剧情
RUN!
package com.wenjun.www; import com.wenjun.www.mainMenu; public class App { public static void main(String[] args) { new mainMenu().menu(); System.out.println("燕子!!!"); } }
完事!
后记
乱写+1
exp+3
这篇关于[java]枚举类练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 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微服务资料:新手入门全攻略