[英]Text doesn't print properly in console
I have a simple program consisting of two methods.我有一个由两种方法组成的简单程序。 The first is a menu that asks if the player wants to make a suggestion or exit the game.
第一个是询问玩家是否想提出建议或退出游戏的菜单。 If they choose to make a suggestion, the second method is called.
如果他们选择提出建议,则调用第二种方法。 The second method is suppose to print a form for the user to fill out.
第二种方法是假设打印一个表单供用户填写。 So it's suppose to look like this:
所以它看起来像这样:
Person:
Weapon:
Room: Library
Library is already filled out for the user.库已为用户填写。 The rest can be filled out like this:
其余的可以这样填写:
Person: Professor Plum
Weapon: Revolver
Room: Library
The problem is that what's printed out doesn't look like what I want.问题是打印出来的东西看起来不像我想要的。 What's printed out looks like this:
打印出来的内容是这样的:
Person: Weapon:
Person and Weapon are printed on the same line and the user is only able to fill out Weapons.人物和武器打印在同一行,用户只能填写武器。 Moreover, Room doesn't printout until Weapons is filled.
此外,在武器填满之前,房间不会打印出来。 Is there anyway I can make it look the way I laid out in the previous example?
无论如何,我可以让它看起来像我在上一个例子中的布局吗?
import java.util.Scanner;
public class Main {
private String[] suggestions = new String[3];
private Scanner sc = new Scanner(System.in);
private String room = "Library";
public void menuSelection() {
System.out.println("Please make a selection...\n");
System.out.println("1. Make a suggestion");
System.out.println("2. Exit game");
int selection = sc.nextInt();
if (selection == 1)
makeSuggestion();
else
System.exit(0);
}
public void makeSuggestion() {
System.out.print("Person: ");
suggestions[0] = sc.nextLine();
System.out.print("Weapon: ");
suggestions[1] = sc.nextLine();
System.out.println("Room: " + room);
suggestions[2] = room;
}
public static void main(String[] args) {
Main main = new Main();
main.menuSelection();
}
}
You are not printing the values.您没有打印值。 Use
System.out.println()
to print those values.使用
System.out.println()
打印这些值。
System.out.print("Person: ");
suggestions[0] = sc.nextLine();
System.out.println(suggestions[0]);
System.out.print("Weapon: ");
suggestions[1] = sc.nextLine();
System.out.println(suggestions[1]);
System.out.print("Room: " + room);
suggestions[2] = room;
System.out.println(suggestions[2]);
Output:输出:
Person: Professor Plum
Weapon: Revolver
Room: Library
Use sc.nextLine();
使用
sc.nextLine();
after int selection = sc.nextInt();
在
int selection = sc.nextInt();
This will give desire output这将给出欲望输出
System.out.println will print on a new line where System.out.print will print and the same line. System.out.println 将在 System.out.print 将在同一行上打印的新行上打印。 you should use System.out.println() instead of System.out.print()
你应该使用 System.out.println() 而不是 System.out.print()
this is what's causing the issue:这就是导致问题的原因:
int selection = sc.nextInt();
replace it with sc.nextLine() for example.例如,用 sc.nextLine() 替换它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.