Java - 如何在输入后打印()输出到上一行 [英] Java - How to print() output to previous line after input

查看:847
本文介绍了Java - 如何在输入后打印()输出到上一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,我仍然试图抓住它,所以我很抱歉这是一个愚蠢的问题,但我想知道如何在输入的同一行打印输出。例如,我正在编写一个简单的游戏,用户试图猜测一个4位数字(就像Mastermind一样)。我已经找到了所有的机制,但我很难在控制台上显示它。在一开始它可能看起来像:(a)

I'm kind of new to Java and I'm still trying to get the hang of it, so I apologize if this is a stupid question, but I was wondering how to print the output on the same line as the input. For example, I'm programming a simple game where the user tries to guess a 4-digit number (rather like Mastermind). I've got the mechanics all figured out, but I'm having a hard time displaying it on the console. In the beginning it might look something like: (a)

Turn    Guess   Bulls   Cows
----------------------------
1

然后,用户会输入他们的猜测:(b)

Then, the user would input their guess: (b)

Turn    Guess   Bulls   Cows
----------------------------
1       1234

一旦用户点击进入,程序应该检查他们对密码的猜测并输出公牛的数量(他们的猜测中的数字与秘密号码匹配)确切地说)和奶牛(他们的猜测中的数字是数字的一部分,但位置错误),然后开始一个新的行并再次等待用户输入。因此,如果秘密号码是4321 ......(c)

And as soon as the user hits enter, the program should check their guess against the secret number and output the number of "bulls" (digits in their guess that match the secret number exactly) and "cows" (digits in their guess that are part of the number but in the wrong position), and then start a new line and again await user input. So if the secret number were, say, 4321... (c)

Turn    Guess   Bulls   Cows
----------------------------
1       1234    0       4
2

麻烦的是,我不能为我的生活弄清楚如何让输出显示在与输入相同的行上。这是我到目前为止的一小部分(因为完整的代码更加丑陋而被剥离):

Trouble is, I can't for the life of me figure out how to get the output to display on the same line as the input. Here's a snippet of what I have so far (stripped-down because the full code is much uglier):

number = "4321";
String guess;
int attempts = 1;
do
{
    System.out.print(attempts + "\t\t");
    guess = keyboard.next();
    attempts++;
    int bulls = checkBulls(guess, number);
    int cows = checkCows(guess, number);
    System.out.print("\t\t" + bulls + "\t\t" + cows + "\n");
}
while (!guess.equals(number));

这使我达到(b),但是当我点击输入时,会发生这种情况:

Which gets me as far as (b), but then when I hit enter, this happens:

Turn    Guess   Bulls   Cows
----------------------------
1       1234                 // so far so good
    0       4                // ack! These should've been on the previous line!
2

我知道这对游戏来说并不重要,可能只是我使事情变得比必要的复杂,但它驱使我坚果。我想发生的事情是,当你输入猜测后点击输入时,程序会启动一个新行,然后打印公牛和奶牛。有什么方法可以解决这个问题吗?

I know this isn't really essential to the game and is probably just me making things more complicated than necessary, but it's driving me nuts. I suppose what's happening is that when you hit enter after typing in your guess, the program starts a new line and then prints the bulls and cows. Is there any way to get around this?

推荐答案

您应该查看 Ansi Escape Codes

类似:

public void update() {
    final String guessString = keyboard.next();

    System.out.println("\033[1A\033[" + guessString.length() + 'C'
        + "\t{next col}");
}

将导致:

1234   {next col}

(其中1234,用户输入后跟{RETURN};在适当支持的控制台中输入。

(where 1234, followed by {RETURN} was entered by the user; in an appropriately supporting console.

\ 033 [1A将光标向上移动1行; \ 033 [xC将光标 x 移动到右侧(上面我们计算 x 作为猜测的长度)。

"\033[1A" moves the cursor up 1 row; and "\033[xC" moves the cursor x to the right (where above we calculate x as the length of their guess).

这种方法的另一种选择是清除控制台(\ 033 [2J),这样你就可以完全重新绘制表格(这可能更容易保持)。

Another option that you have with this approach is to clear the console ("\033[2J") so that you can just completely re-draw the table (which is probably easier to maintain).

这篇关于Java - 如何在输入后打印()输出到上一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆