#java
Вопрос:
Я спрашиваю пользователя, есть ли у него промо-код, если есть, я даю ему возможность ввести его, я хочу, чтобы у него было 3 попытки для правильного ввода.
Вот как я пытался:
public void chooseClassAndPromoCode(ArrayList<Flight> flightList) {
System.out.println("Pick which class do you want? (1 - economic, 2 - business, 3 - first class)");
int userFlightClass = scanner.nextInt();
scanner.nextLine();
System.out.println("Do you have promo code? (yes / no )");
String yesNoPromoCode = scanner.nextLine();
int attempts = 3;
if (yesNoPromoCode.equalsIgnoreCase("yes")) {
System.out.println("Enter promo code");
String userPromoCode = scanner.nextLine();
while (attempts-- > 0) {
if (userPromoCode.equals(selectedFlight.getPromoCode()) amp;amp; userFlightClass == 1) {
System.out.println("Price for economic class is: " selectedFlight.getPriceForEconomicClass());
// System.out.println("Promo code is valid. You have discount of: 20%, new price is: " discountEconomyClass());
System.out.println("Balance of: " selectedPassenger.getFirstName() " is: " selectedPassenger.getBalance());
// payingEconomicClassWithPromoCode(flightList);
} else {
System.out.println("Incorrent. Number of atempts: " attempts);
}
}
}
}
}
После того, как я его запущу, программа распечатает мне это:
Incorrent. Number of atempts: 2
Incorrent. Number of atempts: 1
Incorrent. Number of atempts: 0
Комментарии:
1. В чем ваш фактический вопрос/проблема? что работает не так, как вы хотите, чтобы это работало? какова разница(и) желаемых и фактических результатов?
2. Введите свой код для ввода промо-кода внутри цикла while. Вам нужно будет добавить перерыв, чтобы выйти из цикла, когда есть хороший код.
3. Вы получаете ввод от пользователя только один раз в начале. Когда цикл выполняется, он проверяет один и тот же ввод снова и снова, не давая пользователю возможности повторить попытку.
Ответ №1:
Просто исправьте расположение userPromoCode
сканера, он должен быть внутри while
.
public void chooseClassAndPromoCode(ArrayList<Flight> flightList) {
System.out.println("Pick which class do you want? (1 - economic, 2 - business, 3 - first class)");
int userFlightClass = scanner.nextInt();
scanner.nextLine();
System.out.println("Do you have promo code? (yes / no )");
String yesNoPromoCode = scanner.nextLine();
int attempts = 3;
String userPromoCode = ""
if (yesNoPromoCode.equalsIgnoreCase("yes")) {
while (attempts-- > 0) {
System.out.println("Enter promo code");
userPromoCode = scanner.nextLine();
if (userPromoCode.equals(selectedFlight.getPromoCode()) amp;amp; userFlightClass == 1) {
System.out.println("Price for economic class is: " selectedFlight.getPriceForEconomicClass());
// System.out.println("Promo code is valid. You have discount of: 20%, new price is: " discountEconomyClass());
System.out.println("Balance of: " selectedPassenger.getFirstName() " is: " selectedPassenger.getBalance());
// payingEconomicClassWithPromoCode(flightList);
break;
} else {
System.out.println("Incorrent. Number of atempts: " attempts);
}
}
}
}