Java简单猜测游戏

时间:2015-02-16 00:02:56

标签: java debugging while-loop

两名球员的示例程序。玩家1输入密码(范围0-100)。玩家2猜这个号码。如果该猜测更高或更低,则输出消息。如果玩家2猜对了,输出祝贺消息和猜测次数。这是我到目前为止所得到的。

import java.util.Scanner;
public class GuessingGame 
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int number;

    System.out.println("Player 1 enter secret number (range 0 - 100): ");
    number = input.nextInt();

    int guess = 0;
    int tries = 0;

    while(guess != number)
    {
        tries = tries + 1;

        System.out.print("Player 2 enter your guess (range 0 - 100): ");
        guess = input.nextInt();
        if (guess > number)
        {
            System.out.printf("Your guess is higher than the correct number\n");
        }
        else if (guess < number)
        {
            System.out.printf("Your guess is lower than the correct number\n");
        }
        else if (guess == number)
        {
            System.out.printf("Congratulations! You guess the correct number\n");
            System.out.printf("Number of guesses: %d\n", tries);
            break;
        }
    }
}

}

我的问题是:如何隐藏玩家#1输入的密码,让玩家#2看不到? 如何将密码的范围限制在0到100.我在播放器#1输入后立即使用下面的代码。但是,在此之后,它仍然会显示玩家#2的输入。

if (number < 0)
System.out.print("Please enter number greater than 0");
if (number > 100)
System.out.print("Please enter number smaller than 100");

任何人都请帮助我!我正在使用NetBeans。

2 个答案:

答案 0 :(得分:1)

您可以使用 JOptionPane(对话框),以便玩家1输入密码。

For example:

    String input=JOptionPane.showInputDialog(null,"Player 1 enter secret number (range 0 - 100): ");
    int number=Integer.parseInt(input);

请在此处查看有关Dialogs http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html

的更多信息

答案 1 :(得分:1)

您可以在第一个玩家使用以下方法选择值后立即清除控制台:

public final static void clearConsole()
{
    try
    {
        final String os = System.getProperty("os.name");

        if (os.contains("Windows"))
        {
            Runtime.getRuntime().exec("cls");
        }
        else
        {
            Runtime.getRuntime().exec("clear");
        }
    }
    catch (final Exception e)
    {
        //  Handle any exceptions.
    }
}

现在您可以将代码更改为:

import java.util.Scanner;
public class GuessingGame 
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int number;

    do {
    System.out.println("Player 1 enter secret number (range 0 - 100): ");
    number = input.nextInt();
    }while (number < 0 || number > 100);
    clearConsole();

    int guess = 0;
    int tries = 0;

    while(guess != number)
    {
        tries = tries + 1;

        System.out.print("Player 2 enter your guess (range 0 - 100): ");
        guess = input.nextInt();
        if (guess > number)
        {
            System.out.printf("Your guess is higher than the correct number\n");
        }
        else if (guess < number)
        {
            System.out.printf("Your guess is lower than the correct number\n");
        }
        else if (guess == number)
        {
            System.out.printf("Congratulations! You guess the correct number\n");
            System.out.printf("Number of guesses: %d\n", tries);
            break;
        }
    }
}

编辑包括号码范围检查。