[java]枚举类练习

2022/5/2 1:42:40

本文主要是介绍[java]枚举类练习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

emm...很

想法:

  1. 枚举类实现季节信息打印
  2. 一个简单的交互
  3. 一个很艹的剧情

上代码

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]);
    }
}
  1. Season[] seasons = Season.values();直接全装进去
  2. seasonList()增强for循环打印
  3. menu()中switch+Scanner简单交互
  4. findSeason()数组下标查询打印
  5. 生草的剧情

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]枚举类练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程