循环打印

时间:2014-06-03 18:17:05

标签: java

我的while循环如何继续打印"请输入库存长度(mm):"

代码:

while (true) {
            // input stocks
            System.out.print("Please enter stock length(mm): ");
            Scanner sc = new Scanner(System.in);
            int stockLength = sc.nextInt();
            System.out.print("Please enter quantities of this stock length: ");
            int stockQuantity = sc.nextInt();
            Stocks stockObj = new Stocks(stockLength,stockQuantity);
            stockList.add(stockObj);
            System.out.print("Are you done? (y/n): ");
            String done = sc.nextLine();
            if (done == "y" || done == "yes") {
                break;
            }
        }

输出:

Please enter stock length(mm): 4
Please enter quantities of this stock length: 4
Are you done? (y/n): Please enter stock length(mm): 

1 个答案:

答案 0 :(得分:4)

使用sc.next()

代替sc.nextLine()
String done = sc.next();
if (done.equalsIgnoreCase("y") || done.equalsIgnoreCase("yes")) {
     System.out.println("breaking out of loop " + "Value of done: " + done)
     break;
}