java输出变成一行输出怎么变,Java - 如何在输入后打印()输出到上一行

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

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

Turn Guess Bulls Cows

----------------------------

1 1234

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

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?

解决方案

You should look into Ansi Escape Codes.

Something like:

public void update() {

final String guessString = keyboard.next();

System.out.println("\033[1A\033[" + guessString.length() + 'C'

+ "\t{next col}");

}

will result in:

1234 {next col}

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

"\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).

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).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值