我在创建if语句来检查两个变量时遇到了麻烦



首先我要写我想做的事情:

用户通过ID选择航班,之后如果用户有促销代码,请输入。

这是飞行对象:

Flight flight1 = new Flight(1, "Moscow", "Belgrade", 300, 600, 850, "gC49");
Flight flight2 = new Flight(2, "Paris", "Dortmund", 250, 290, 400, "soe4");
Flight flight3 = new Flight(3, "Podgorica", "Nis", 25, 40, 80, "sx33");
Flight flight4 = new Flight(4, "London", "Miami", 600, 1500, 2500, "zcl3");

第一个对象末尾的gC49是促销代码。如果用户输入该代码,则打印出他有折扣,如果该代码不正确,则他没有折扣。

我试过这样做:

在代码的这一部分,用户通过ID:选择一个航班

System.out.println("Choose one flight by ID: ");
int userChoiceFlight = scanner.nextInt();
scanner.nextLine();
for (Flight tempUserChoiceFlight : flightList) {
if (userChoiceFlight == tempUserChoiceFlight.getId()) {
System.out.println("You flight from: " + tempUserChoiceFlight.getFrom()
+ " to: " + tempUserChoiceFlight.getTo());
}
}

在代码的这一部分,用户询问他是否有促销代码,如果有,他输入,如果该代码与他选择的飞行对象的代码匹配,他应该写一条消息,说他有折扣。

System.out.println("Do you have promo code? (yes / no) ");
String yesNo = scanner.nextLine();
if (yesNo.contains("yes")) {
System.out.println("Enter promo code");
String userPromoCode = scanner.nextLine();
for (Flight tempFlights : flightList) {
if (userPromoCode.contains(tempFlights.getPromoCode())) {
System.out.println("Cool you have discount!");
break;
} else {
System.out.println("Promo code is not valid!");
break;
}
}
} else if (yesNo.contains("no")) {
System.out.println("Find one, you have no discount!");
} else {
System.out.println("Wrong input!");
}

这是主代码:

if (userPromoCode.contains(tempFlights.getPromoCode())) {

错误为:

第一个对象的促销代码对每个对象都有效,因此,如果用户输入第一个物体的促销代码,他在每次航班上都有折扣。

我需要什么:

用户通过ID选择一个航班,该航班有一些促销代码,如果用户输入该促销代码,他们将获得折扣。

示例:

用户选择ID号3,即此航班:Flight flight3 = new Flight(3, "Podgorica", "Nis", 25, 40, 80, "sx33");当程序要求用户提供促销代码时,他应该输入折扣的sx33,否则消息为no discount for you

我试着像这样循环:

for (int i = 0; i < flightList.size();) {
if (userPromoCode.contains(flightList.get(i).getPromoCode())) {

但还是一样。

当您使用单独的for循环来遍历列表时,您总是从第一个"飞行;对象因此,根据您的代码,tempFlights.getPromoCode()将始终是第一次飞行的促销代码。你需要做的是将用户输入的促销代码与已经选择的航班进行比较。

因此,您可以这样做,而不是使用两个for循环。

System.out.println("Choose one flight by ID: ");
int userChoiceFlight = scanner.nextInt();
scanner.nextLine();
for (Flight tempUserChoiceFlight : flightList) {
if (userChoiceFlight == tempUserChoiceFlight.getId()) {
System.out.println("You flight from: " + tempUserChoiceFlight.getFrom()
+ " to: " + tempUserChoiceFlight.getTo());
System.out.println("Do you have promo code? (yes / no) ");
String yesNo = scanner.nextLine();
if (yesNo.contains("yes")) {
System.out.println("Enter promo code");
String userPromoCode = scanner.nextLine();
if (userPromoCode.contains(tempUserChoiceFlight.getPromoCode())) {
System.out.println("Cool you have discount!");
break;
} else {
System.out.println("Promo code is not valid!");
break;
}

} else if (yesNo.contains("no")) {
System.out.println("Find one, you have no discount!");
} else {
System.out.println("Wrong input!");
}
}
}

迭代flightList时,您将从列表中获得id目的地。如果你能获得该航班的促销代码,它会对你有所帮助。

String promo = ""; // declaring a global variable outside the for loop so you can use it later
for (Flight tempUserChoiceFlight : flightList) {
if (userChoiceFlight == tempUserChoiceFlight.getId()) {
System.out.println("You flight from: " + tempUserChoiceFlight.getFrom()
+ " to: " + tempUserChoiceFlight.getTo());
promo += tempUserChoiceFlight.getPromo(); // getting the correct promocode 
}
}

由于您现在有了用户想要的航班的正确促销代码,您可以向用户询问他的促销代码,即他拥有的促销代码,并将两者进行比较。像这样:

if (yesNo.contains("yes")) {
...
String userPromoCode = scanner.nextLine();
if(userPromoCode.equals(promo)) { 
...

您不需要再次迭代飞行列表,因为您已经存储了正确的促销代码。

最新更新