Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么交换机案例部分和运行时多态性不起作用?_Java_Polymorphism - Fatal编程技术网

Java 为什么交换机案例部分和运行时多态性不起作用?

Java 为什么交换机案例部分和运行时多态性不起作用?,java,polymorphism,Java,Polymorphism,我在(切换情况)部分遇到了问题,我尝试在main中测试一些运行时多态性。它不起作用。在开关箱中;它总是说: 您的选择不可用 最后,它不会退出该计划。它是一个连续的循环。 我该如何解决这个问题 public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String tempName; int tempQue

我在(切换情况)部分遇到了问题,我尝试在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.... ");
    }
}
公共类测试{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
字符串tempName;
int-tempQueue;
双tempkillo,tempCalibre;
弦tempAgri;
ArrayList farmers=新ArrayList();
最终int容量=2;

对于(int i=0;i您从未填充您的farmers1列表,它始终为空,因此isFruitAvailable始终为false。此外,您的while循环从未终止,它只是不断旋转(“break”语句,您的语句位于开关内部,与“while”无关)


您从未填充farmers1列表,它始终为空,因此isFruitAvailable始终为false。此外,您的while循环从未终止,它只是不断旋转(“break”语句,您的语句位于交换机内部,与“while”无关)


根据我的理解,您希望将新农民与已填充的农民列表进行比较,即
农民
,如果匹配,则将其添加到
农民1
列表中

因此,在
开关
案例中,更改
for
循环,如下所示:

// Compare the farmer tempName with the list of farmers already populated
for(Farmer fruit : farmers) {
    if(fruit.getName().equals(tempName)) {
        farmers1.add(fruit);
        System.out.println("Your choice is successfully added.");
        isPFruitAvailable=true;
        break;
    }
}

根据我的理解,您希望将新农民与已填充的农民列表进行比较,即
农民
,如果匹配,则将其添加到
农民1
列表中

因此,在
开关
案例中,更改
for
循环,如下所示:

// Compare the farmer tempName with the list of farmers already populated
for(Farmer fruit : farmers) {
    if(fruit.getName().equals(tempName)) {
        farmers1.add(fruit);
        System.out.println("Your choice is successfully added.");
        isPFruitAvailable=true;
        break;
    }
}

正如您提到的,您无法获得o/p
您的选择。
这意味着农民的名字与列表中的任何农民名字都不匹配我已经创建了一个ArrayList来引用农民的名字。如果我键入匹配的名字,但它再次显示相同的响应:(正如您提到的,您无法获得o/p
您的选择。
这意味着农民的姓名与列表中的任何农民姓名都不匹配。我已经创建了一个ArrayList来引用农民的姓名。如果我键入匹配的姓名,但再次显示相同的响应:(循环可能会运行在
farmers
上,而不是
farmers1
(后者也可以获得更有意义的变量名,可能是
basket
shoppingCart
)@Thilo是的,indeedYep,你是对的。我已经调试了这个问题。现在是处理其他问题的时候了:D这个循环应该运行在
农民
上,而不是
农民1
(后者也可以得到一个更有意义的变量名,可能
篮子
购物车
).@Thilo是的,indeedYep,你是对的。我已经调试了这个问题。现在是处理其他问题的时候了:D