我有在多个组合框中重复的方法,但我不太确定是否有一种方法可以将其概括化以减少重复?方法如下:

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;
        }
    }

最佳答案

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

09-26 11:24