有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么switchcase部分和运行时多态性不起作用?

我的(开关盒)部分有问题,我试着在main中测试一些运行时多态性。它不起作用。在开关箱中;它总是说:

Your choice is not available.

最后,它并没有退出这个项目。这是一个连续的循环。 我该如何解决这个问题

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String tempName;
        int tempQueue;
        double tempKilo, tempCalibre;
        String tempAgri;
        ArrayList<Farmer> farmers = new ArrayList<>();
        final int capacity = 2;

        for(int i=0; i<capacity; i++)            {
            System.out.println(" Please enter the farmer's name : ");
            tempName = sc.nextLine();
            System.out.println(" Please enter the queue number : ");
            tempQueue = Integer.parseInt(sc.nextLine());
            System.out.println(" Please enter the calibre of the cherry : ");
            tempCalibre = Double.parseDouble(sc.nextLine());
            System.out.println(" Please enter the kilo of the cherry : ");
            tempKilo = Double.parseDouble(sc.nextLine());
            Cherry cherry1 = new Cherry(tempName, tempQueue, tempCalibre, tempKilo);
            farmers.add(cherry1);
            cherry1.displayMenu();
            cherry1.Price();
        }
        for(int i=0; i<capacity; i++) {
            System.out.println(" Please enter the farmer's name : ");
            tempName = sc.nextLine();
            System.out.println(" Please enter the queue number : ");
            tempQueue = Integer.parseInt(sc.nextLine());
            System.out.println(" Please enter the production type of Apple : ");
            tempAgri = sc.nextLine();
            System.out.println(" Please enter the kilo of the apple : ");
            tempKilo = Double.parseDouble(sc.nextLine());
            Apple apple1 = new Apple(tempName, tempQueue, tempAgri, tempKilo);
            farmers.add(apple1);
            apple1.displayMenu();
            apple1.Price();
        }
        ArrayList<Farmer>farmers1=new ArrayList<>();
        int checkpoint;
        boolean isPFruitAvailable;
        double totalPrice;
        while(true) {
            display();
            checkpoint=Integer.parseInt(sc.nextLine());
            switch(checkpoint) {
                case 1:
                    System.out.println(" Please enter the name of the farmer that you want... ");
                    tempName=sc.nextLine();
                    isPFruitAvailable=false;
                    for(Farmer fruit : farmers1) {
                        if(fruit.getName().equals(tempName)) {
                            farmers1.add(fruit);
                            System.out.println("Your choice is successfully added.");
                            isPFruitAvailable=true;
                            break;
                        }
                    }
                    if(!isPFruitAvailable) {
                            System.out.println(" Your choice is not available");
                    }
                    break;
                case 2:
                    totalPrice=0.0;
                    System.out.println(" Please enter the name in the cart . ");
                    System.out.println(" Checking out...");
                    for(Farmer fruit1 : farmers1) {
                        fruit1.displayMenu();
                        totalPrice+=fruit1.Price();
                    }
                    System.out.println(" Price is "+totalPrice);
                    break;
                default:
                    System.out.println(" Thanks for your informations...");
                    System.out.println(" The program quits within a second... ");
                    break;
            }
        }
    }

    public static void display() {
       System.out.println(" Welcome to fruit transfer system... ");
       System.out.println(" Press 1 to add fruit into the system... ");
       System.out.println(" Press 2 to check fruit you want... ");
       System.out.println(" Press any key to quit the program.... ");
    }
}

共 (0) 个答案