java - 应用 do while 循环

标签 java do-while

我的代码目前无法在程序的开头启动,即“请输入...”。我希望代码能够在每次执行选择 1 或 2 时返回到起始语句。 while 语句不满足这一点,因为我无法在声明“choice”之前使用 while 语句。我知道我应该使用 do while 循环,但每次我尝试实现它时 - 它都会给我一个带有大括号的错误。

以下是我的代码片段:

    System.out.println("Please type in 1 for a customer to view their portfolio, 2 to trade stocks or 3 to exit the application");
    int choice = Integer.parseInt(input.nextLine());
    {
while (!"3".equals(choice));
    { 
        if (choice == 1) {
            System.out.println(mycustomers[menuchoice].toString());
            return;
                         }
        if (choice == 2) {
            String StockSelect = "Please select a stock";
            for (int i = 0; i < mystocks.length; i++) {
                // [i] is the element we are accessing
                StockSelect += " " + i + " " + (mystocks[i].getSymbol());
            }

            System.out.println(StockSelect);

            int stockchoice = Integer.parseInt(input.nextLine());

            System.out.println("Select 1 to buy or 2 to sell?");
            int choice2 = Integer.parseInt(input.nextLine());

            if (choice2 == 1) {
                System.out.println("How many stocks would you like to buy ");
                int volumebought = Integer.parseInt(input.nextLine());
                for (int i = 0; i < numofstocks; i++) {
                    mycustomers[menuchoice].setBalance(
                            mycustomers[menuchoice].getBalance() - (volumebought * mystocks[i].getprice()));
                    mycustomers[menuchoice].setPortfolio();
                }
                System.out.println(mycustomers[menuchoice].toString());
                return;
            }
            if (choice2 == 2) {
                System.out.println("How much stocks would you like to sell ");
                int volumesold = Integer.parseInt(input.nextLine());
                for (int i = 0; i < numofstocks; i++) {
                    mycustomers[menuchoice].setBalance(
                            mycustomers[menuchoice].getBalance() + (volumesold * mystocks[i].getprice()));
                            mycustomers[menuchoice].setPortfolio();
                }
                System.out.println(mycustomers[menuchoice].toString());
                return;
            }

        }
        if (choice == 3) // how to exit application
        {
            System.out.println("Thank you for using the application");
            System.out.println("The current state of all customers are:");
            for (int i = 0; i < mycustomers.length; i++) {
                System.out.println(mycustomers[i].toString());
            }
            System.out.println("The current state of all stocks are:");
            for (int i = 0; i < mystocks.length; i++) {
                System.out.println(mystocks[i].toString());
            }
            System.exit(0);
        }

    }

}}}

如何实现 do-while 循环,以便每次执行代码后都返回到初始语句 - 只要输入不是 3?

最佳答案

在 while 循环内请求输入,如下所示:

choice = Integer.parseInt(input.nextLine());

关于java - 应用 do while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36960884/

相关文章:

java - 无法反序列化 `org.json.JSONObject` 的实例

java - Java中的不可变数组

c - 结束这个 while 循环我做错了什么

java - 程序加 1、2 或 3,直到达到 21

javascript - 计算循环执行了多少次

java - 哪种方法更适合将微服务应用程序迁移到云端? Kubernetes、AWS Lambda等,如何取舍?

用于生成和使用生成的决策树的 Java 库

javascript - 尝试编写一个 do-while 循环语句来打印我的语句,但当前没有打印它

c - 使用字符输入执行 while 循环

java - Libgdx 如何设置视口(viewport)位置?