为什么开关机箱部分和运行时多态性不起作用?



我在(开关大小写)部分有问题,我试图在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.... ");
}
}

你从来没有填充过你的农民1列表,它总是空的,因此isFruitAvailable总是假的。此外,您的 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;
}
}

根据我的理解,您想将新农民与 alredy 填充的农民名单进行比较,即farmers,如果匹配任何匹配,请将其添加到farmers1列表中。

因此,在switch的情况下更改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;
}
}

最新更新