简体   繁体   English

用户输入在同一行?

[英]User input on same line?

I'm new to Java so forgive me. 我是Java新手,请原谅我。 I'm writing a small little Guessing Game program. 我正在编写一个小的Guessing Game程序。

import java.util.Scanner; 导入java.util.Scanner;

public class GuessingGame { 公共课程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;
        }
    }   
  }
}

When I prompt the user to guess a number and the number is incorrect, I want to be able to flush immediately flush the input stream and enter another guess on the same line. 当我提示用户猜测一个数字并且该数字不正确时,我希望能够立即刷新输入流并在同一行上输入另一个猜测。

For example: Guess a number between 0 and 5: I enter my guesses here on this one line only. 例如:猜一个0到5之间的数字:我仅在这一行输入我的猜想。

I could take the print out of the loop but in that case, if I enter an inccorect number, the cursor jumps to the next line. 我可以将打印内容从循环中取出,但是在这种情况下,如果输入错误的数字,则光标会跳到下一行。

Hope this makes sense. 希望这是有道理的。 Thanks! 谢谢!

If your goal is only to clear the console, then you could do: 如果您的目标只是清除控制台,则可以执行以下操作:

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

Or if you are on Linux or OS X: 或者,如果您使用的是Linux或OS X:

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

Now if you add previous line that you wanted to keep, you will need to keep then in an Array and reprint after each clear. 现在,如果您添加要保留的上一行,则需要将其保留在数组中,并在每次清除后重新打印。

This won't work in IDE's. 这在IDE中不起作用。


If you want a more system-independent way, there is a library named JLine ( GitHub ), which is designed for a better console usage. 如果您想要一种与系统无关的方法,可以使用一个名为JLineGitHub )的库,该库旨在更好地使用控制台。 It actually contains a method clearScreen . 它实际上包含一个方法clearScreen


You could also do: 您也可以这样做:

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

This will clear the screen and return the cursor to the first row. 这将清除屏幕,并将光标返回到第一行。

A quick and dirty solution... 快速而肮脏的解决方案...

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();
                }
            }
        } 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM