简体   繁体   English

如何通过询问控制台中的用户是否要重复计算来执行一段代码

[英]How can I execute a section of code by asking the user in the console if they want to repeat the calculation

I have written code that performs calculations by entering the console and now I want to ask the user if they want to perform this again by entering "Y" or "N" in the console.我已经编写了通过进入控制台执行计算的代码,现在我想询问用户是否要通过在控制台中输入“Y”或“N”再次执行此操作。

I solved this part with a simple if statement, but now I want to execute the whole code above again in case the user enters "Y".我用一个简单的 if 语句解决了这部分,但现在我想再次执行上面的整个代码,以防用户输入“Y”。 Does anyone have a suggestion for this problem?有人对这个问题有建议吗?

    System.out.println("Wollen Sie die Rechnung nocheinmal ausführen? Y / N");

    String yesorno2 = StdIn.readString();
    if (yesorno2.equals("Y")) {
        //Should repeat the code above
    } else {
        System.out.println("Auf Wiedersehn");
        //Should say the text above and end the code
    }
}

There are a few ways you could repeat the code above, depending on what you're trying to accomplish.有几种方法可以重复上面的代码,具体取决于您要完成的任务。 One way would be to use a while loop that continues to execute the code as long as the user enters "Y".一种方法是使用 while 循环,只要用户输入“Y”就继续执行代码。 Here's an example:这是一个例子:

while (yesorno2.equals("Y")) {
    // code to repeat goes here
    System.out.println("Wollen Sie die Rechnung nocheinmal ausführen? Y / N");
    yesorno2 = StdIn.readString();
}
System.out.println("Auf Wiedersehn");

Another way would be to use a do-while loop, like this:另一种方法是使用 do-while 循环,如下所示:

do {
    // code to repeat goes here
    System.out.println("Wollen Sie die Rechnung nocheinmal ausführen? Y / N");
    yesorno2 = StdIn.readString();
} while (yesorno2.equals("Y"));
System.out.println("Auf Wiedersehn");

It's a good idea to wrap up the code that needs to be repeated in a function, so it could be easily called multiple times.最好将需要重复的代码封装在一个function中,这样可以方便地多次调用。

You also could use recursion, which is a technique that involves a function calling itself.您还可以使用递归,这是一种涉及 function 调用自身的技术。 This would also let you wrap up the code that needs to be repeated in a single function, but it can be more tricky to implement and can lead to stack overflow if not implemented correctly.这也可以让您将需要重复的代码包装在一个 function 中,但实现起来可能更棘手,如果实现不正确,可能会导致堆栈溢出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在向用户询问他们想要多少问题之后,如何让我的代码显示该数量的随机问题? - After asking the user for how many questions they want, how can I get my code to show that number of random problems? 如何重复代码; 如果用户输入否。 我希望重复所有三个循环,直到用户输入是 - How to repeat code; If user inputs no. I want all three loops to repeat, until user inputs Yes 我如何包含一个问题,询问用户是否想再次玩游戏? - How do I include a question asking the user if they want to play again? 我可以重复一段代码直到我得到想要的答案吗 - Can I repeat a section of code until I get a desired answer 如何让用户输入在 java 中重复我的代码? - How can I let the user input repeat my code in java? 如何让计算器询问用户是否想在 Java 中执行另一个计算? - How can I make a calculator ask the user if they want to perform another calculation in Java? 如何减少 Java 8 中的重复代码? - How can i reduce this repeat code in Java 8? 如何实现询问用户是否要再次运行我的 Java 程序? - How do I implement asking the user if they want to run my java program again? 在询问用户是否要继续后,如何一次打印出一个 HashMap KeyValue? - How do I print out one HashMap KeyValue at one time after asking the user if they want to continue? 当不要求输入时,键入的文本会显示在控制台中,我该如何阻止它? - Typed text displays in console when not asking for input, how can I stop this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM