用户输入在同一行?

时间:2015-01-18 15:31:05

标签: java

我是Java的新手,请原谅我。我正在写一个小小的猜猜游戏程序。

import java.util.Scanner;

公共课GuessingGame {

static Scanner userInput = new Scanner(System.in);

public static void main(String[] args) {
    int menuOption; 
    // Main Screen //
    System.out.println("Guessing Game");
    System.out.println("---------------------");
    System.out.println("1.) Play Game");
    System.out.println("2). Exit");
    System.out.println();           

    while (true) {
        System.out.print("Please select a menu option: ");                  
        menuOption = userInput.nextInt();

        if (menuOption == 2) {
            System.out.println("Exiting the game. Thanks for playing!");
            break; 

        } else if (menuOption == 1) {
            System.out.println("Let's start the game!");

            getRandomRange();

        } else {
                System.out.println("Sorry, that's not a valid option. Please try again.");
                continue;
        }
    }

}
/**
 * Determine the range of numbers to work with
 */
public static void getRandomRange() {
    int min;
    int max;

    System.out.print("Choose a minimum number: ");
    min = userInput.nextInt();

    System.out.print("Choose a maximum value: ");
    max = userInput.nextInt();

    getRandomNumber(min, max);
}

public static void getRandomNumber(int min, int max) {
    int randomNumber = (int) (Math.random() * (max - min + 1)) + min;

    getAGuess(min, max, randomNumber);
}

public static void getAGuess(int min, int max, int randomNumber) {
    int guess; 

    while (true) {
        System.out.print("Guess a number between " + min + " and " + (max) + ": ");
        guess = userInput.nextInt();

        if (guess == randomNumber) {
            System.out.println("Correct! The random number is: " + randomNumber);
            break;
        }
    }   
  }
}

当我提示用户猜测一个数字并且数字不正确时,我希望能够立即刷新输入流并在同一行输入另一个猜测。

例如: 猜一个0到5之间的数字:我只在这一行输入我的猜测。

我可以将打印输出循环但在这种情况下,如果我输入一个inccorect数字,光标会跳转到下一行。

希望这是有道理的。谢谢!

2 个答案:

答案 0 :(得分:1)

如果你的目标只是清除控制台,那么你可以这样做:

Runtime.getRuntime().exec("cls");

或者如果您使用的是Linux或OS X:

Runtime.getRuntime().exec("clear");

现在,如果你添加了你想要保留的上一行,你需要保留一个数组并在每次清除后重新打印。

这在IDE中无效。


如果您想要一种更独立于系统的方式,那么有一个名为JLineGitHub)的库,它旨在更好地使用控制台。它实际上包含一个方法clearScreen


你也可以这样做:

System.out.print("\033[H\033[2J");

这将清除屏幕并将光标返回到第一行。

答案 1 :(得分:0)

快速而肮脏的解决方案......

public class Main {


    public static final void main(String[] data) {

        Scanner userInput = new Scanner(System.in);
        int min = 0;
        int max = 10;
        int randomNumber = (int) ((Math.random() * max) + (min + 1));

        while (true) {
            System.out.print("Guess a number between " + min + " and " + max + ": ");
            int guess = userInput.nextInt();
            if (guess == randomNumber) {
                System.out.println("Correct! The random number is: " + randomNumber);
                break;
            } else {
                for (int i = 0 ; i < 25; i++) {
                    System.out.println();
                }
            }
        } 
    }
}
相关问题