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;
}
}
}
}
3 个解决方案
#1
11
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)
{}
}
#2
3
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
你可以这样做。
while(in.hasNext()){
}
#3
2
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.....
#1
11
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)
{}
}
#2
3
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
你可以这样做。
while(in.hasNext()){
}
#3
2
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.....