Java 我在创建一个if语句来检查两个变量时遇到了麻烦
首先,我将写下我想做的事情: 用户选择flight by ID,然后如果用户有促销代码,请输入它 这是飞行物体:Java 我在创建一个if语句来检查两个变量时遇到了麻烦,java,Java,首先,我将写下我想做的事情: 用户选择flight by ID,然后如果用户有促销代码,请输入它 这是飞行物体: Flight flight1 = new Flight(1, "Moscow", "Belgrade", 300, 600, 850, "gC49"); Flight flight2 = new Flight(2, "Paris", "Dortmund", 250, 2
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
是促销代码。如果用户输入该代码,则打印出他有折扣,如果该代码不正确,则他没有折扣
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!");
}
我试着这样做:
在代码的这一部分,用户根据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选择一个航班,该航班有一些促销代码,如果用户输入该促销代码,他们将获得折扣
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!");
}
示例:
用户选择ID号3,即此航班:
flight3=新航班(3,“Podgorica”、“Nis”、25、40、80、“sx33”)代码>
当一个程序要求用户提供促销代码时,他应该输入sx33
以获得折扣,否则消息是不为您提供折扣
我试着像这样循环:
for (int i = 0; i < flightList.size();) {
if (userPromoCode.contains(flightList.get(i).getPromoCode())) {
for(int i=0;i
但是它仍然是一样的。当您使用单独的for循环遍历列表时,总是从第一个“Flight”对象开始
将始终是第一个航班的促销代码。您需要做的是将用户输入的促销代码与已选择的航班进行比较
因此,不必使用两个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)) {
...
您无需再次迭代flightList,因为您已经存储了正确的promocode。将实例保存在tempUserChoiceFlight
变量/字段中的某个位置,以指示“这是用户选择的航班”。然后稍后使用它访问促销代码。您需要保存用户为航班输入选择的对象,在用户输入促销代码后,您需要比较以前存储的对象的促销代码。我是如何实现的?我明白您想说什么,但idk如何实现这一点,所以这都是因为两个for循环?啊,我现在就看吧,谢谢,因为当您使用单独的for循环遍历列表时,总是从第一个“Flight”对象开始
将始终是第一次航班的促销代码。您需要做的是将用户输入的促销代码与已选择的航班进行比较。是的,我在头脑中了解我需要做什么,但不知道如何实现。再次感谢您!