简体   繁体   English

在Java中打印与字符串等效的特定于字符的行

[英]Printing a character specific line equivalent to string in Java

To keep it short and concise I'm attempting to write a program that not only turns user input into a title. 为了简短起见,我试图编写一个程序,不仅将用户输入转换为标题。 but also prints out a user generated decorative underline beneath the output. 而且还会在输出下方打印出用户生成的装饰性下划线。 example; 例;

"prompt to get user input" “提示获取用户输入”

input: i am a title, * 输入:我是标题,*

output: 输出:

I Am A Title
* ** * *****

I was thinking of using a method that receives a String and a char and uses that to calculate the number of chars it has to output. 我正在考虑使用一种接收字符串和一个char的方法,并使用该方法来计算它必须输出的char数。 I was already able to get the title to output in capitals thanks to a post here. 多亏了这里的帖子,我已经能够以大写形式获得标题。 Now it's just the characters. 现在只是角色。 Even if it's just ways to think about the problem. 即使这只是思考问题的方法。 Any help would be appreciated. 任何帮助,将不胜感激。

public static void printUnderline(String s, char c){
    char[] arr =s.toCharArray();
    for(int i=0;i<arr.length;i++)
        if(arr[i] == ' ')
            System.out.print(' ');
        else
            System.out.print(c);
}
        String str = "i am a title";
        String aux = "";
        String[]titles = str.split(" ");

        for(int i=0; i<titles.length;i++){
            aux += titles[i].replaceFirst(titles[i].charAt(0)+"", Character.toUpperCase(titles[i].charAt(0))+"") + " ";
        }

        System.out.println(aux);
        if(str.contains(" ")){
            aux = str;
            aux = aux.replaceAll("[A-Za-z]",  "*");

            System.out.println(aux);

        }else{
            System.out.println(false);
        }

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

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