java 要求是/否返回循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6964880/
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
Ask for yes/no to go back to the loop
提问by apathetic012
How do i go back to the tryloop if the user answers Y/y? I know the code below doesn't ask for the user's Do you want to try again (Y/N)?. I can only use bufferedReader at the moment.
如果用户回答 Y/y,我如何返回到try循环?我知道下面的代码不会询问用户是否要再试一次(是/否)?. 我目前只能使用 bufferedReader。
import java.io.*;
public class Num10 {
public static void main(String[] args){
String in="";
int start=0, end=0, step=0;
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.print("Input START value = ");
in=input.readLine();
start=Integer.parseInt(in);
System.out.print("Input END value = ");
in=input.readLine();
end=Integer.parseInt(in);
System.out.print("Input STEP value = ");
in=input.readLine();
step=Integer.parseInt(in);
}catch(IOException e){
System.out.println("Error!");
}
if(start>=end){
System.out.println("The starting number should be lesser than the ending number");
System.exit(0);
}else
if(step<=0){
System.out.println("The step number should always be greater than zero.");
System.exit(0);
}
for(start=start;start<=end;start=start+step){
System.out.println(start);
}
System.out.println("\nDo you want to try again (Y/N)?");
}
}
回答by Chris Eberle
A do ... while
loop would be appropriate for this.
一个do ... while
循环将适用于此。
boolean again = false;
do
{
// your code here
again = askQuestion();
} while(again);
or you could just use a regular while
loop:
或者您可以只使用常规while
循环:
boolean again = true;
while(again)
{
// your code here
again = askQuestion();
}
And for both of these, askQuestion
is another method that asks the user a question and returns true
or false
.
对于这两种情况,askQuestion
是另一种向用户提问并返回true
or 的方法false
。
回答by Mohammad Faisal
use do{..... }while();
利用 do{..... }while();
like:
喜欢:
boolean again=true;
do{
try{
//your code
}
//your code
}while(again);
How do i go back to the try loop if the user answers Y/y?
You can not go back to try{.....}
. You must use
如果用户回答 Y/y,我如何返回到 try 循环?
你不能回去try{.....}
。你必须使用
public static void main(String... args){
//your code
}
instead of public static void main(String[] args){}
and to go back use main();
at your desired location of code.
而不是public static void main(String[] args){}
并返回main();
在您想要的代码位置使用。
回答by Karl Knechtel
How do i go back to the try loop if the user answers Y/y?
如果用户回答 Y/y,我如何返回到 try 循环?
try
doesn't create a loop. That's the problem.
try
不会创建循环。那就是问题所在。
Loops "go back" automatically until something happens to end the loop. That's what makes them loops.
循环会自动“返回”,直到发生某些事情结束循环。这就是使它们循环的原因。
So you put an actual loop around the try block, and ask the question at the end of the loop, and break out of the loop if it is answered with N/n (or whatever; you can decide for yourself how you want to handle it if the input is some other garbage). The do
- while
loop is appropriate because it checks for its exit condition at the end, rather than the beginning (like while
and for
).
因此,您在 try 块周围放置了一个实际循环,并在循环结束时提出问题,如果回答为 N/n(或其他;您可以自己决定如何处理),则退出循环如果输入是其他一些垃圾的话)。本do
-while
循环是合适的,因为它会检查在最后的退出条件,而不是开始(如while
和for
)。
回答by Alexis King
Java has a lot of tools for something like this. These are known as "loops". The loops available in Java are the while
, do ... while
, for
, and for ... each
. The while loop is the simplest. It looks like this:
Java 有很多这样的工具。这些被称为“循环”。在Java中可用的循环是while
,do ... while
,for
,和for ... each
。while 循环是最简单的。它看起来像这样:
while (condition) {
// Some code
}
Basically, where I put "condition" you put any boolean value. A boolean value is simply a value that can be either true
or false
, nothing else. In Java, you can create variables of the boolean
type, but often times, you won't explicitly create a boolean.
基本上,在我放置“条件”的地方,您可以放置任何布尔值。布尔值只是一个可以是true
or的值false
,没有别的。在 Java 中,您可以创建该boolean
类型的变量,但通常情况下,您不会显式创建布尔值。
Instead, you'll use conditional statements. For example x < y
, x == y
, and x >= y
are all "conditional statements". That means, in the end, they evaluate into a boolean value. So, in this code:
相反,您将使用条件语句。例如x < y
,,x == y
和x >= y
都是“条件语句”。这意味着,最后,它们评估为一个布尔值。所以,在这段代码中:
int x = 1;
int y = 5;
if (x < y) {
// Code
}
Where it says x < y
, it's actually evaluating it into true
behind the scenes. Boolean values are important, and are used in many places in Java.
它所说的x < y
,实际上是true
在幕后对其进行评估。布尔值很重要,并且在 Java 中的许多地方都使用。
Anyway, back on the topic of loops, let's get to the next loop, do ... while
. It looks like this:
不管怎样,回到循环的话题,让我们进入下一个循环,do ... while
。它看起来像这样:
do {
// Code
} while (condition);
do ... while
works the same as while
, but it always runs at least once, even if the condition is false
.
do ... while
工作原理相同while
,但它总是至少运行一次,即使条件是false
.
for
and for ... each
are slightly more complicated. They are really just shortcuts. Consider the following code:
for
并且for ... each
稍微复杂一些。它们实际上只是捷径。考虑以下代码:
int i = 0;
while (i < 10) {
System.out.println("I is: " + i);
i++;
}
Here, the loop will run 10 times, and i
will keep track of how many loops. The for
loop can do the same thing, but in a more concise way:
在这里,循环将运行 10 次,并记录i
循环次数。该for
循环可以做同样的事情,但在一个更简洁的方式:
for (int i = 0; i < 10; i++) {
System.out.println("I is: " + i);
}
That code will do the same thing the first one did, but it uses a for
loop. As you can see, the for
loop has three parts. In the first, you can declare a variable, in the second, you put a condition, and in the third, you put something that's run every iteration of the loop. for
loops are often used when iterating over arrays, but you may not have learned that yet, so don't fret.
该代码将执行与第一个相同的操作,但它使用了一个for
循环。如您所见,for
循环分为三个部分。在第一个中,您可以声明一个变量,在第二个中,您放置一个条件,在第三个中,您放置一个在循环的每次迭代中运行的东西。for
在迭代数组时经常使用循环,但您可能还没有学到,所以不要担心。
The for ... each
loop is more complicated than all the others, so if you don't understand it just yet, don't worry. Basically, it's a fast way to get all the elements of a list, or anything that's in the Collections API. For example, say you have a list of Strings, you could use this to get all the Strings in the list:
这个for ... each
循环比其他所有循环都要复杂,所以如果你还不理解它,不要担心。基本上,这是获取列表的所有元素或 Collections API 中任何内容的快速方法。例如,假设您有一个字符串列表,您可以使用它来获取列表中的所有字符串:
for (String string : aList) {
System.out.println(string);
}
That would go through every String in the list and print it out.
这将遍历列表中的每个字符串并将其打印出来。
Anyway, back to the problem at hand. Here, we need to chose a loop to use, and it seems that the best one for the situation would be a simple while
loop. Just do something like this:
无论如何,回到手头的问题。在这里,我们需要选择一个循环来使用,似乎最适合这种情况的while
循环是一个简单的循环。只是做这样的事情:
boolean condition = true;
while (condition) {
// Your code
try {
while (true) { //Creates an infinite loop
in = input.readLine();
if (in.equalsIgnoreCase("y")) {
condition = true;
break;
} else if (in.equalsIgnoreCase("n")) {
condition = false;
break;
} else {
System.out.println("Please enter either Y or N.");
}
}
} catch (IOException e) {
System.out.println("ERROR!");
}
}
Here, we use two while
loops. The first wraps the entire code, checking for a boolean called condition
. We've initialized it to be true
, so the loops will always run at least once. Second, I made another while
loop. Here, I did while (true)
, which will run forever. However, you can "break" out of a loop by using the break
keyword. The reason I used this, is the user has a possibility of entering something other than "y" or "n". In that case, it should just ask them again. If it gets "Y", it sets condition
to true
and breaks out of the loop (which is technically unnecessary, since condition is already set to true
). If it gets "N" it sets it to false
and breaks out of the loop. Since it is now false
, the loop will be exited and the program will end.
在这里,我们使用两个while
循环。第一个包装整个代码,检查一个名为condition
. 我们已将其初始化为true
,因此循环将始终至少运行一次。其次,我又做了一个while
循环。在这里,我做到了while (true)
,它将永远运行。但是,您可以使用break
关键字“中断”循环。我使用它的原因是用户有可能输入“y”或“n”以外的其他内容。在这种情况下,它应该再问他们一次。如果它得到“Y”,它设置condition
为true
并退出循环(这在技术上是不必要的,因为条件已经设置为true
)。如果它得到“N”,则将其设置为false
并跳出循环。既然是现在false
,