gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:32:07 25 4
gpt4 key购买 nike

我遇到了(switch-case)部分的问题,我尝试在 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.... ");
}
}

最佳答案

您从未填充过您的 farmer1 列表,它始终为空,因此 isFruitAvailable 始终为 false。另外,您的 while 循环永远不会终止,它只是不断旋转(您拥有的“break”语句位于 switch 内部,与“while”无关)

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

关于java - 为什么 switch-case 部分和运行时多态性不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55878926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com