将点添加到 system.out.println 中
基本上我有一条线的结尾是 40, 如果添加 2 个单词,每个单词长 10 个字母,那么我就剩下 20 个空格 20 个空格应该填满点……例如 你好......................你
或
堆栈...................... ......溢出
现在我的代码工作正常,我只是不知道如何在 system.out.println 中满足这一点,以便将结果打印到控制台。
在测试输入干扰词后应该有多少个点之后,我正在考虑使用 whileloop 一次打印一个点。
目前我有这个显然不起作用
{
System.out.println (word1 + ".." + word2);
while (wordlegnth1 + wordlegnth2 < LINELENGTH)
{
System.out.println (word1 + "." + word2);
wordlegnth1++;
}
well basically I have a line thats has a finial in of 40,
if a add 2 words that are 10 letters each long that leaves me with 20 spaces
thouse 20 spaces should be filled with dots ...... for example
hello................................you
or
stack...........................overflow
now I have the code working fine, I just have no idea on how to please this in a system.out.println so that the result is printed to the console.
I was thinking of using a whileloop to print the dots one at a time, after testing how many number of dots there should be once bother words are entered.
At the moment I have this which clearly doesn't work
{
System.out.println (word1 + ".." + word2);
while (wordlegnth1 + wordlegnth2 < LINELENGTH)
{
System.out.println (word1 + "." + word2);
wordlegnth1++;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
注意:使用StringBuilder是为了避免由于String不可变性而导致的性能损失
Note: the use of StringBuilder is to avoid performance penalty due to String immutability
输出:
Output:
为什么不从以下内容开始:
它可以改进(例如尝试非常大的字符串,或者使用 StringBuilder 而不是 System.out.print)。
Why not start with something like:
It can be improved on (try strings that are very large for example, or use a StringBuilder rather than System.out.print).
将 apache 的 commons-lang 添加到您的类路径并使用 StringUtils.leftPad()
为什么要编写一个方法来执行其他人已经编写并测试过的操作?
add apache's commons-lang to your classpath and use
StringUtils.leftPad()
why write a method to do something someone else has already written + tested?