完善重复方法

时间:2019-03-19 15:19:18

标签: java javafx

我有在多个组合框中重复的方法,但我不太确定是否有办法将其概括化以最大程度地减少重复?方法如下:

public void numberTicket1(){
        switch (no1.getValue().toString()){
            case "One":
                numberOfTickets = 1;
                break;
            case "Two":
                numberOfTickets = 2;
                break;
            case "Three":
                numberOfTickets = 3;
                break;
            case "Four":
                numberOfTickets = 4;
                break;
            case "Five":
                numberOfTickets = 5;
                break;
            case "Six":
                numberOfTickets = 6;
                break;
            default:
                numberOfTickets = 0;
        }
    }

1 个答案:

答案 0 :(得分:3)

numberOfTickets = Arrays.asList (
    "One", "Two", "Three", "Four", "Five", "Six").
    indexOf (no1.getValue().toString()) + 1;