简体   繁体   English

for循环中的字符打印出来的值太远了

[英]characters in for loop printing out too far away from values

I am very new to java and for loops. 我是java和for循环的新手。 I would like my code to print out this 我希望我的代码打印出来

Andy      **
Kristy    **********
Amy       ***** `

But my I am getting this instead 但是我得到了这个

Andy          **
Kristy          **********
Amy          *****

How do I change my for loop to stop the spacing from the names being this way? 如何更改for循环以停止与这种方式的名称间距? Thank you. 谢谢。

public static void drawChart(String name, int age) {
    System.out.print(name);   
    for(int j = 1; j <= 10; j++) {
        System.out.print(" ");
    }

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

    System.out.println(" "); 
}

You can use String.format() utility to write the name in a certain width. 您可以使用String.format()实用程序以特定宽度写入名称。 If the name is less than that width, it'll fill the rest with spaces. 如果名称小于该宽度,它将用空格填充其余部分。 Something like that. 这样的事情。

public static void drawChart(String name, int age){
        String nameWithPadding = String.format("%-20s", name);
        System.out.print(nameWithPadding);
        for (int i = 0; i < age; i++)
            System.out.print('*');
        System.out.println();
}

Here, this part %-20s is writing the name in 20 chars. 这里,这部分%-20s正在写出20个字符的名字。 First it writes the name, followed by spaces until it reaches 20 chars. 首先它写入名称,然后是空格,直到它达到20个字符。

Instead of printing 10 spaces, you can print a number of spaces with is a function of the name length: 您可以使用名称长度的函数打印多个空格,而不是打印10个空格:

System.out.print(name);   

for(int j = 1; j <= 20 - name.length(); j++) { // the value 20 should be 
                                               // higher than the longest name
    System.out.print(" ");
}

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

System.out.println(" ");

If you are using Java 8 you can use String::join like this : 如果您使用的是Java 8,可以像这样使用String :: join

public static void drawChart(String name, int age) {
    String spaces = String.join("", Collections.nCopies(20 - name.length(), " "));
    String stars = String.join("", Collections.nCopies(age, "*"));
    System.out.println(name + spaces + stars);
}

The above answers are good, but they do not consider the situation when you call the function with the longest name and then you call the function again with the next longest name. 上面的答案很好,但是当你调用名字最长的函数然后再用下一个最长的名字再次调用函数时,他们不会考虑这种情况。 In that case the spacing would be different. 在这种情况下,间距会有所不同。 Here is the solution with due respect to that issue: 以下是适当考虑该问题的解决方案:

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.Map;
import java.util.Set;

public class Search {

    public static void drawChart(Multimap<String, Integer> persons){
        //we use Multimap because we may have more than one person with the same name
        Set<String> names = persons.keySet();
        int maxLength = 0;
        for (String name : names) {
            if (name.length() > maxLength) {
                maxLength = name.length(); //define the longest name
            }
        }

        for (Map.Entry<String, Integer> entry : persons.entries()) {
            String name = entry.getKey();
            System.out.print(name);
            if (name.length() < maxLength) {
                //adjusting the number of spaces required to be on the same level
                //with the longest name
                int diff = maxLength - name.length();
                for (int i = 0; i < diff; i++) {
                    System.out.print(" ");
                }
            }
            Integer age = entry.getValue();
            for (int i = 0; i < age; i++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        Multimap<String, Integer> map = ArrayListMultimap.create();
        map.put("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 2);
        map.put("ffffffffffffffffffffffffffffffffffffffffffffffffff" +
            "fffffffffffffffffffffffff", 3);
        map.put("zz", 3);
        map.put("zz", 6);
        map.put("ffff", 9);
        drawChart(map);
    }
}

The output: 输出:

zz                                                                                    ***
zz                                                                                    ******
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff           ***
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa**
ffff                                                                                  *********

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM