【问题标题】:Java while loop not closing when getting specific input获取特定输入时Java while循环未关闭
【发布时间】:2019-10-23 20:30:33
【问题描述】:

我有一个大小合适的 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

【问题讨论】:

  • 请编辑您的问题而不是在评论中添加它,并且至少包括循环条件。
  • 你能用循环条件显示(编辑你的问题)'while'行吗?
  • 添加break; 使代码无法编译这一事实向我暗示,您实际上可能并未将该关键字放入循环的代码块中。您可能在代码中存在一些不明显的格式问题
  • 尝试添加最少的代码

标签: java while-loop char


【解决方案1】:

试试这个,因为你没有完整显示 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);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 2019-08-24
    • 2015-09-04
    • 2017-04-27
    • 2018-08-14
    • 1970-01-01
    相关资源
    最近更新 更多