如何“按任意键继续”以使用我的Java代码?

时间:2013-11-08 23:41:25

标签: java

我希望用户在按下键盘上的任意键后在第一个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;
        }
      }
    }

   }

4 个答案:

答案 0 :(得分:14)

您可以创建此功能(仅适用于输入键)并在代码中的任何位置使用它:

 private void pressAnyKeyToContinue()
 { 
        System.out.println("Press Enter key to continue...");
        try
        {
            System.in.read();
        }  
        catch(Exception e)
        {}  
 }

答案 1 :(得分:3)

1)请参阅while(run1 = true)while(run2 = true)

=是java中的赋值运算符。使用==运算符来比较基元

2)你可以这样做

while(in.hasNext()){

}

答案 2 :(得分:1)

在进入实施细节之前,我认为您需要退一步并重新检查一下您的算法。根据我收集的内容,您需要从用户处获取整数列表,并确定它们是否形成魔术方块。您可以在单个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

之后,您可以输出有关数字的详细信息:

if magic is a magic square
    print "this is a magic square"
else
    print "this is not a magic square"

// etc, etc.....

答案 3 :(得分:0)

我的答案仍然仅适用于 Enter键,但这样做不会在输入流中留下任何东西,以后可能会造成麻烦(System.in.read()可能会将东西留在在下一个输入中读取的流)。因此,我读了整行(这里我使用Scanner,但我想实际上并没有必要,您只需要一些东西来摆脱输入流中的整行):

public void pressEnterKeyToContinue()
{ 
        System.out.println("Press Enter key to continue...");
        Scanner s = new Scanner(System.in);
        s.nextLine();
}