我在(switch-case)部分遇到问题,并尝试在main中测试一些运行时多态性。它不起作用。在开关情况下;它总是说:


  您的选择不可用。


最后,它不会退出程序。它连续循环。
我应该怎么做才能摆脱这个问题?

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.... ");
    }
}

最佳答案

您从未填充过Farmers1列表,该列表始终为空,因此isFruitAvailable始终为false。同样,您的while循环永远不会终止,只会不断旋转(您在switch内且与“ while”无关的“ break”语句)

for(Farmer fruit : farmers1) {
    if(fruit.getName().equals(tempName)) {
        farmers1.add(fruit); //the only place where you add something, but it never gets executed
        break;
    }
}

08-05 13:08