将点添加到 system.out.println 中

发布于 2024-10-01 02:22:10 字数 535 浏览 0 评论 0原文

基本上我有一条线的结尾是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

无人接听 2024-10-08 02:22:10
final int LINE_LENGTH = 40;

String word1 = ...;
String word2 = ...;

StringBuilder sb = new StringBuilder(LINE_LENGTH);
sb.append(word1);
for (int i = 0; i + word1.length() + word2.length() < LINE_LENGTH; i++) {
    sb.append(".");
}
sb.append(word2);

System.out.println(sb.toString());

注意:使用StringBuilder是为了避免由于String不可变性而导致的性能损失

final int LINE_LENGTH = 40;

String word1 = ...;
String word2 = ...;

StringBuilder sb = new StringBuilder(LINE_LENGTH);
sb.append(word1);
for (int i = 0; i + word1.length() + word2.length() < LINE_LENGTH; i++) {
    sb.append(".");
}
sb.append(word2);

System.out.println(sb.toString());

Note: the use of StringBuilder is to avoid performance penalty due to String immutability

白日梦 2024-10-08 02:22:10
/**
 * Formats a given string to 40 characters by prefixing it with word1 and 
 * suffixing it with word2 and using dots in the middle to make length of final
 * string = 40.
 * If string will not fit in 40 characters, -1 is returned, otherwise 0 is
 * returned.
 * 
 * @param word1 the first word
 * @param word2 the second word
 * @return 0 if string will fit in 40 characters, -1 otherwise
 */
public static int formatString(String word1, String word2) {
    final int LINELENGTH = 40;

    // determine how many dots we need
    int diff = LINELENGTH - (word1.length() + word2.length());

    if (diff < 0) {
        // it's too big
        System.out.println("string is too big to fit");
        return -1;
    }
    // add the dots
    StringBuilder dots = new StringBuilder();
    for (int i = 0; i < diff; ++i) {
        dots.append(".");
    }

    // build the final result
    String result = word1 + dots.toString() + word2;

    System.out.println(result);

    return 0;

}

public static void main(String[] args) throws Throwable {
    formatString("stack", "overflow");
    String str = "";
    for (int i = 0; i < 21; ++i) {
        str += "a";
    }
    formatString(str, str);
}

输出:

stack...........................overflow
string is too big to fit
/**
 * Formats a given string to 40 characters by prefixing it with word1 and 
 * suffixing it with word2 and using dots in the middle to make length of final
 * string = 40.
 * If string will not fit in 40 characters, -1 is returned, otherwise 0 is
 * returned.
 * 
 * @param word1 the first word
 * @param word2 the second word
 * @return 0 if string will fit in 40 characters, -1 otherwise
 */
public static int formatString(String word1, String word2) {
    final int LINELENGTH = 40;

    // determine how many dots we need
    int diff = LINELENGTH - (word1.length() + word2.length());

    if (diff < 0) {
        // it's too big
        System.out.println("string is too big to fit");
        return -1;
    }
    // add the dots
    StringBuilder dots = new StringBuilder();
    for (int i = 0; i < diff; ++i) {
        dots.append(".");
    }

    // build the final result
    String result = word1 + dots.toString() + word2;

    System.out.println(result);

    return 0;

}

public static void main(String[] args) throws Throwable {
    formatString("stack", "overflow");
    String str = "";
    for (int i = 0; i < 21; ++i) {
        str += "a";
    }
    formatString(str, str);
}

Output:

stack...........................overflow
string is too big to fit
神仙妹妹 2024-10-08 02:22:10

为什么不从以下内容开始:

int lengthA = word1.length();
int lengthB = word2.length();
int required = LINE_LENGHT - (lengthA + lengthB);

for(int i = 0; i < required; i++)
{
    System.out.print(".");
}

它可以改进(例如尝试非常大的字符串,或者使用 StringBuilder 而不是 System.out.print)。

Why not start with something like:

int lengthA = word1.length();
int lengthB = word2.length();
int required = LINE_LENGHT - (lengthA + lengthB);

for(int i = 0; i < required; i++)
{
    System.out.print(".");
}

It can be improved on (try strings that are very large for example, or use a StringBuilder rather than System.out.print).

橘寄 2024-10-08 02:22:10

将 apache 的 commons-lang 添加到您的类路径并使用 StringUtils.leftPad()

System.out.println(str1 + StringUtils.leftPad(str2, 40 - str1.length(), '.'));

为什么要编写一个方法来执行其他人已经编写并测试过的操作?

add apache's commons-lang to your classpath and use StringUtils.leftPad()

System.out.println(str1 + StringUtils.leftPad(str2, 40 - str1.length(), '.'));

why write a method to do something someone else has already written + tested?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文