Java 如何通过用户输入结束while循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18975208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:01:02  来源:igfitidea点击:

How to end a while Loop via user input

javaif-statementwhile-loop

提问by Rick123

package cst150zzhw4_worst;

import java.util.Scanner;

public class CST150zzHW4_worst {

    public static void main(String[] args) {
    //Initialize Variables
    double length; // length of room
    double width; // Width of room
    double price_per_sqyd; // Total carpet needed price
    double price_for_padding; // Price for padding
    double price_for_installation; // Price for installation
        String input; // User's input to stop or reset program
    double final_price; // The actual final price
        boolean repeat = true;

    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

        while (repeat)
        {   
        //User Input

    System.out.println("\n" +"What is the length of the room?: ");
    length = keyboard.nextInt();

    System.out.println("What is the width of the room?: ");
    width = keyboard.nextInt();

    System.out.println("What is the price of the carpet per square yard?: ");
    price_per_sqyd = keyboard.nextDouble();

        System.out.println("What is the price for the padding?: ");
        price_for_padding = keyboard.nextDouble();

        System.out.println("What is the price of the installation?: ");
        price_for_installation = keyboard.nextDouble();

        final_price = (price_for_padding + price_for_installation + price_per_sqyd)*((width*length)/9);

        keyboard.nextLine(); //Skip the newline

        System.out.println("The possible total price to install the carpet will be $" + final_price + "\n" + "Type 'yes' or 'no' if this is correct: ");
        input = keyboard.nextLine();

        } 
    }
}

How would I make it so when the user says yes the program stop and if the user says no then the program just repeats? I don't know why I'm having so much trouble. I've searched for well over 4 hours. I am only supposed to use a while loop, I think.

当用户说“是”时程序停止,如果用户说“否”则程序只是重复,我将如何做到这一点?我不知道为什么我有这么多麻烦。我已经搜索了4个多小时。我认为我只应该使用 while 循环。

回答by Jean Logeart

You have to assign repeatin your while-loop so it becomes falseif the user says yes:

您必须repeat在您的while-loop 中进行分配,以便false用户说yes

repeat = !input.equalsIgnoreCase("yes"); 

回答by GoodSp33d

You just need to set repeatto true or false based on user input. So in the end, compare inputwith yes or no. Something like this would work for you :

您只需要repeat根据用户输入设置为 true 或 false。所以最后,比较input是或否。这样的事情对你有用:

if ("yes".equals(input)) 
 repeat = true; // This would continue the loop
else 
 repeat = false; // This would break the infinite while loop 

回答by Jonas H?gh

You can use a breakstatement to exit a while loop.

您可以使用break语句退出 while 循环。

while (...) {

   input = ...;
   if (input.equals("Y")) {
     break;
   }
}

回答by Prabhakaran Ramaswamy

    boolean repeat = true;

   // Create a Scanner object for keyboard input.
     Scanner keyboard = new Scanner(System.in);

    while (repeat)
    {   
       -----------------------
       -------------------------
       System.out.println("Do you want to continue:");
       repeat = keyboard.nextBoolean();
    }

回答by khalid jarrah

you also if you want your code to be more systematic , go and search about the interrupt , specially thread interrupt , these answers above is correct , find the more organic code and implement it

如果你想让你的代码更系统,去搜索中断,特别是线程中断,上面这些答案是正确的,找到更有机的代码并实现它