如何在Java中进行分栏中断

huangapple go评论75阅读模式
英文:

How to column break in java

问题

期望输出

输入考试分数:              输入成绩:
数学:                        数学:
科学:                        科学:
历史:                        历史:

                                     
如何获得此输出?

我的输出

输入考试分数:
数学:
科学:
历史:

输入成绩:
数学:
科学:
历史:

如何在下一列输入?
英文:

Expected Output

Enter exam score:             Enter grade:
Math:                         Math:
Science:                      Science:
History:                      History:                 

How to get this output?

My output

Enter exam score:                
Math:                                     
Science:                                        
History:

Enter Grade:
Math:
Science:                                                        
History:

How do I enter input in the next column?

答案1

得分: 3

好的,以下是您要求的翻译内容:

似乎您正在寻找System.out.printf(),在其中您可以使用%-来为字符串添加填充,以便在打印时使它们保持对齐。您可以通过更改30来调整确切的间距量,但这应该对您有用:

String mathStr = "数学:";
String scienceStr = "科学:";
String historyStr = "历史:";
System.out.printf("%-30s%s\n", "输入考试分数:", "输入成绩:");
System.out.printf("%-30s%s\n", mathStr, mathStr);
System.out.printf("%-30s%s\n", scienceStr, scienceStr);
System.out.printf("%-30s%s\n", historyStr, historyStr);

编辑: 添加了变量以向OP展示它也适用于这些变量。

英文:

Sounds like you're looking for System.out.printf() where you can use %- to add padding to your strings to make them even when printing. You can adjust the exact amount of spacing by changing the 30, but this should work for you:

String mathStr = "Math:";
String scienceStr = "Science:";
String historyStr = "History:";
System.out.printf("%-30s%s\n", "Enter exam score:", "Enter grade:");
System.out.printf("%-30s%s\n", mathStr, mathStr);
System.out.printf("%-30s%s\n", scienceStr, scienceStr);
System.out.printf("%-30s%s\n", historyStr, historyStr);

Edit: Added variables to show OP it works with those as well.

答案2

得分: 0

好的,这是翻译好的内容:

好的,我认为您想要为用户提供一种在控制台上以列方式输入数据的方法。

这很困难。问题在于当终端驱动程序处于正常("cooked")模式时,控制台仅在用户键入 ENTER 键时接受用户的输入并将其发送到您的程序,这会导致控制台显示驱动程序进入下一行。

为了解决这个问题,您必须执行以下操作之一:

  - 查找一个第三方的 Java 库,直接支持列显示布局。
  - 查找一个第三方的 Java 库,允许您将终端驱动程序设置为“原始”模式,并在控制台窗口中移动输入光标。阅读这个问题和答案以获取一些建议:

       - https://stackoverflow.com/questions/439799/whats-a-good-java-curses-like-library-for-terminal-applications

但我建议您不要这样做。如果您希望为用户实现一个“花哨”的输入屏幕来输入数据,请在 Swing、JavaFX 或使用 Web(HTML + CSS + Javascript)界面中实现。如果您需要从控制台接受输入,请让它看起来像这样:

    输入考试分数(0-100),然后输入等级(A-F):
    数学:99 A
    科学:88 B
    历史:21 F

英文:

OK, so I think you want to provide a way for the user to enter the data in a columnar display on a console.

That is difficult. The problem is that when the terminal driver is in normal ("cooked") mode, the console only accepts the user's input and sends it to your program when the user types ENTER. And that causes the console display driver to go to the next line.

To solve this, you have to do one of the following:

But I would recommend that you don't do this. If you want a "fancy" input screen for your users to enter input, implement it in Swing, or JavaFX, or using a web (HTML + CSS + Javascript) interface. If you need to accept input from the console, make it look like this:

Enter exam score (0-100) followed by a grade (A-F):
Math: 99 A
Science: 88 B  
History: 21 F

huangapple
  • 本文由 发表于 2020年10月10日 11:59:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64289820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定