Цикл Java не закрывается при получении определенного ввода - PullRequest
0 голосов
/ 23 октября 2019

У меня есть цикл while приличного размера, который должен заканчиваться, когда на ответ на следующий запрос ответили «N», если нет. Добавление Break; после последней строки кода приводит к тому, что моя программа выдает коды ошибок после достижения этой подсказки и ввода символа. Эта проблема может возникнуть при форматировании, поскольку я получаю сообщения об ошибках типа: " Enter 'Y' to add another laptop to your purchase or 'N' to exit: y Exception in thread "main" java.util.IllegalFormatConversionException: f != java.lang.String

Вот цикл while:

while (Character.toUpperCase(cont) == 'Y') {

             dateTime = Calendar.getInstance();//Obtains current year

            System.out.printf("%nTOP LAPTOPS OF %tY"
                            + "%n%n1.  %-23s %7s $%,9.2f"
                            + "%n2.  %-23s %8s %,9.2f"
                            + "%n3.  %-23s %8s %,9.2f"
                            + "%n4.  %-23s %8s %,9.2f"
                            + "%n5.  %-23s %8s %,9.2f"
                            + "%n%nEnter your choice:  ",
                    dateTime,
                    "HP Envy 13", " ", 799.99,
                    "Asus Zen Book 13 UX33FA", " ", 849.99,
                    "Dell XPS 13", " ", 989.99,
                    "Alienware Area 51-m", " ", 1999.99,
                    "Razer Blade Stealth", " ", 1299.00); //Prompt 1



            choice = input.nextInt(); //Input is assigned to choice.

            if (choice == 5) {
                laptop = "Razer Blade Stealth";
                price = 1299.00;
            } else {
                if (choice == 4) {
                    laptop = "Alienware Area 51 -m";
                    price = 1999.99;
                } else {
                    if (choice == 3) {
                        laptop = "Dell XPS 131";
                        price = 989.99;
                    } else {
                        if (choice == 2) {
                            laptop = "Asus zenbook 13 UX33FA";
                            price = 849.99;
                        } else {
                            if (choice == 1) {
                                laptop = "HP Envy 13";
                                price = 799.99;
                            } else {
                                System.out.printf("%nInvalid choice! Try again");


                            }
                        }
                    }

            }
        }

        if (choice > 0) {
            if (choice < 6) {
                System.out.printf("Enter the quantity for %s: ", laptop);
                qty = input.nextInt();

                lineItem = qty * price;
                subtotal = subtotal + tax;

                if (trigger == 1) {
                    orderSummary += String.format("%n%,-9d %-30s %9s %,17.2f", qty, laptop, " ", lineItem);

                    trigger = 0;
                }//END if for $ sign.
                else {
                    orderSummary += String.format("%n%,-9d %-30s %9s %,17.2f", qty, laptop, " ", lineItem);
                }

            }
        }
        //Empty call to nextLine()
        input.nextLine();

        System.out.printf("%nEnter 'Y' to add another laptop to your purchase or 'N' to exit: ");
        char y = input.nextLine().charAt(0);

        }//END while

1 Ответ

1 голос
/ 23 октября 2019

Попробуйте, так как вы не показывали блок while полностью,

char y = 'Y';

while(y == 'Y' || y == 'y')
{
    // do whatever you want...
    // ..................

    System.out.println("Enter Y to add another laptop to your purchase or any other char to exit: ");
    y = input.next().charAt(0);

}
...