如何让“按任意键继续”在我的 Java 代码中工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19870467/
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
How do I get "Press any key to continue" to work in my Java code?
提问by user2840682
I want the user to enter information again in the first while loop after pressing any key on the keyboard. How do I achieve that? Am I doing something wrong with the while loops. SHould I just have one while loop?
我希望用户在按下键盘上的任意键后在第一个 while 循环中再次输入信息。我如何做到这一点?我在 while 循环中做错了什么吗?我应该只有一个 while 循环吗?
import java.util.Scanner;
public class TestMagicSquare
{
public static void main(String[] args)
{
boolean run1 = true;
boolean run2 = true;
Square magic = new Square();
Scanner in = new Scanner(System.in);
while(run1 = true)
{
System.out.print("Enter an integer(x to exit): ");
if(!in.hasNextInt())
{
if(in.next().equals("x"))
{
break;
}
else
{
System.out.println("*** Invalid data entry ***");
}
}
else
{
magic.add(in.nextInt());
}
}
while(run2 = true)
{
System.out.println();
if(!magic.isSquare())
{
System.out.println("Step 1. Numbers do not make a square");
break;
}
else
{
System.out.println("Step 1. Numbers make a square");
}
System.out.println();
if(!magic.isUnique())
{
System.out.println("Step 2. Numbers are not unique");
break;
}
else
{
System.out.println("Step 2. Numbers are unique");
}
System.out.println();
magic.create2DArray();
if(!magic.isMagic())
{
System.out.println("Step 3. But it is NOT a magic square!");
break;
}
else
{
System.out.println("Step 3. Yes, it is a MAGIC SQUARE!");
}
System.out.println();
System.out.print("Press any key to continue...");// Here I want the simulation
in.next();
if(in.next().equals("x"))
{
break;
}
else
{
run1 = true;
}
}
}
}
回答by Prabhakaran Ramaswamy
1) See while(run1 = true)
and while(run2 = true)
1) 看while(run1 = true)
和 while(run2 = true)
= is assignment operator in java. use == operator to compare primitives
= 是 java 中的赋值运算符。使用 == 运算符比较原语
2) You can do like this
2)你可以这样做
while(in.hasNext()){
}
回答by upcrob
Before getting into implementation details, I think you need to step back and re-examine your algorithm a bit. From what I gather, you want get a list of integers from the user and determine if they form a magic square. You can do the first step in a single while loop. Something like this pseudo-code:
在进入实现细节之前,我认为您需要退后一步并重新检查一下您的算法。根据我收集的信息,您希望从用户那里获得一个整数列表,并确定它们是否形成一个幻方。您可以在单个 while 循环中执行第一步。像这样的伪代码:
while (true)
print "Enter an integer (x to stop): "
input = text from stdin
if input is 'x'
break
else if input is not an integer
print "non integer value entered, aborting..."
return
else
add input to magic object
After that, you can output details about the numbers:
之后,您可以输出有关数字的详细信息:
if magic is a magic square
print "this is a magic square"
else
print "this is not a magic square"
// etc, etc.....
回答by E235
You can create this function (good only for enter key) and use it where ever you want in your code:
您可以创建此函数(仅适用于 enter key)并在代码中的任何位置使用它:
private void pressAnyKeyToContinue()
{
System.out.println("Press Enter key to continue...");
try
{
System.in.read();
}
catch(Exception e)
{}
}
回答by Jay Laughlin
My answer still only works with the Enter keybut it does so without leaving stuff on the input stream that could get in the way later (System.in.read() can potentially leave things on the stream that get read in on the next input). So I read the whole line (here I use Scanner but I guess that isn't actually necessary, you just need something to get rid of the entire line in the input stream):
我的答案仍然只适用于Enter 键,但这样做不会在输入流上留下可能妨碍以后的东西(System.in.read() 可能会在下一个输入中读取的东西留在流上)。所以我阅读了整行(这里我使用 Scanner 但我想这实际上不是必需的,你只需要一些东西来摆脱输入流中的整行):
public void pressEnterKeyToContinue()
{
System.out.println("Press Enter key to continue...");
Scanner s = new Scanner(System.in);
s.nextLine();
}