我如何在同一行java中输出 [英] How I can output in same line java

查看:32
本文介绍了我如何在同一行java中输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我完成了所有代码.它可以运行,我也可以得到我的输出.但我只能得到不同的行输出,我想在同一行得到输出.有什么办法可以做到这一点.我使用 switch case ,我可以用 switch case 来做,或者任何其他语句都可以输出.

So far I done all the code. it can run and also I can get my output. But I only can get different line output , I want get output in same line. There have any way to do that . I use switch case , can I do it with switch case or any other statement can output that.

public class practice
{
    public static void main(String args[])
    {
        String name="Farhana Konka";
        String initials= "";
        for(String s: name.split(" "))
        {
            initials+=s.charAt(0);
        }

        char[] myInitials=initials.toCharArray();
        for(int i=0; i<myInitials.length; i++)
        {
            switch(myInitials[i])
            {
            case 'f' :
            case 'F' :

                System.out.println("FFFFF ");
                System.out.println("F     ");
                System.out.println("F     ");
                System.out.println("FFF   ");
                System.out.println("F     ");
                System.out.println("F     ");
                System.out.println("F     ");
                break;
            case 'K' :
            case 'k' :

                System.out.println("K   K ");
                System.out.println("K  K  ");
                System.out.println("K K   ");
                System.out.println("KK    ");
                System.out.println("K K   ");
                System.out.println("K  K  ");
                System.out.println("K   K ");
                break;
            default:
                System.out.print("Nothing happen");
            }
        }

    }
}

它在不同的行显示输出.但我希望它在同一条线上.类似于下面的内容但是我得到的输出只有不同的行.我如何更改我的代码来做到这一点.我尝试过,但我没能做到.

It showing output in different line. But i want it on same line . Something like below But I am getting output only different line. How I can change my code to do that. I try but I failed to do it.

推荐答案

您需要比单独处理每个字母更聪明.您需要处理每一行的所有字母!因为您不能追溯修改该行的内容.

You need to be way smarter than just processing every letter individually. You would need to process all of the letters for each line! Because you cannot modify the line's content retroactively.

因此,不仅仅是大小写切换,您需要将每个字母的各个行存储在数组中,然后处理第一行的首字母,输入新行,处理第二行的首字母,输入新行,等等.

So instead of just a mere case-switch, you need to store the individual lines in arrays for each letter, then process the initials for the first line, enter new line, process the initials for the second line, enter new line, and so on.

为了写入同一行,您需要使用 System.out.print() 但这本身并不能解决您的问题.

In order to write into the same line, you need to use System.out.print() but that in itself will not solve your problem.

好吧,我根据我所说的将我的版本放在一起,然后打印出来:

Well I put my version together based on what I said, and it prints me this:

FFFFFF   AA   RRRRR  H    H   AA   N    N   AA      K    K  OOOO  N    N K    K   AA   
F       A  A  R    R H    H  A  A  NN   N  A  A     K  K   O    O NN   N K  K    A  A  
F      A    A RRRRR  HHHHHH A    A N N  N A    A    KK     O    O N N  N KK     A    A 
FFFF   AAAAAA R  R   H    H AAAAAA N  N N AAAAAA    K K    O    O N  N N K K    AAAAAA 
F      A    A R   R  H    H A    A N   NN A    A    K  K   O    O N   NN K  K   A    A 
F      A    A R    R H    H A    A N    N A    A    K    K  OOOO  N    N K    K A    A 

魔术线是这样的:

Map<Character, List<String>> charMap = new HashMap<>();

虽然这可以只用字符串数组来完成,但这更容易.

Although this can be done with mere String arrays, but this was easier.

祝你任务顺利.

我讨厌那些限制你如何完成它们的任务,只是为了让你可以编写更糟糕的代码.

I hate assignments that limit you in how you can do them just so you can write worse code.

    Map<Character, List<String>> charMap = new HashMap<>();

    List<String> listSpace = new ArrayList<>();
    listSpace.add("  ");
    listSpace.add("  ");
    listSpace.add("  ");
    listSpace.add("  ");
    listSpace.add("  ");
    listSpace.add("  ");
    charMap.put(' ', listSpace);


    List<String> listA = new ArrayList<>();
    listA.add("  AA  ");
    listA.add(" A  A ");
    listA.add("A    A");
    listA.add("AAAAAA");
    listA.add("A    A");
    listA.add("A    A");
    charMap.put('a', listA);

    List<String> listF = new ArrayList<>();
    listF.add("FFFFFF");
    listF.add("F     ");
    listF.add("F     ");
    listF.add("FFFF  ");
    listF.add("F     ");
    listF.add("F     ");
    charMap.put('f', listF);

    List<String> listR = new ArrayList<>();
    listR.add("RRRRR ");
    listR.add("R    R");
    listR.add("RRRRR ");
    listR.add("R  R  ");
    listR.add("R   R ");
    listR.add("R    R");
    charMap.put('r', listR);

    List<String> listH = new ArrayList<>();
    listH.add("H    H");
    listH.add("H    H");
    listH.add("HHHHHH");
    listH.add("H    H");
    listH.add("H    H");
    listH.add("H    H");
    charMap.put('h', listH);

    List<String> listK = new ArrayList<>();
    listK.add("K    K");
    listK.add("K  K  ");
    listK.add("KK    ");
    listK.add("K K   ");
    listK.add("K  K  ");
    listK.add("K    K");
    charMap.put('k', listK);

    List<String> listN = new ArrayList<>();
    listN.add("N    N");
    listN.add("NN   N");
    listN.add("N N  N");
    listN.add("N  N N");
    listN.add("N   NN");
    listN.add("N    N");
    charMap.put('n', listN);

    List<String> listO = new ArrayList<>();
    listO.add(" OOOO ");
    listO.add("O    O");
    listO.add("O    O");
    listO.add("O    O");
    listO.add("O    O");
    listO.add(" OOOO ");
    charMap.put('o', listO);

    for(int lineCount = 0; lineCount < 6; lineCount++)
    {
        for(int i = 0; i < name.length(); i++)
        {
            Character c = name.charAt(i);
            List<String> lineList = charMap.get(c);
            String currentLine = lineList.get(lineCount);
            System.out.print("" + currentLine);
            System.out.print(" ");
        }
        System.out.println("");
    }
}

现在我写了这个:

    String[] arraySpace = new String[]
    {
        "  ",
        "  ",
        "  ",
        "  ",
        "  ",
        "  "
    };

    String[] arrayA = new String[]
    {
        "  AA  ",
        " A  A ",
        "A    A",
        "AAAAAA",
        "A    A",
        "A    A"            
    };

    String[] arrayF = new String[]
    {
        "FFFFFF",
        "F     ",
        "F     ",
        "FFFF  ",
        "F     ",
        "F     " 
    };

    String[] arrayR = new String[]
    {
        "RRRRR ",
        "R    R",
        "RRRRR ",
        "R  R  ",
        "R   R ",
        "R    R"    
    };

    String[] arrayH = new String[]
    {
        "H    H",
        "H    H",
        "HHHHHH",
        "H    H",
        "H    H",
        "H    H"
    };

    String[] arrayK = new String[]
    {
        "K    K",
        "K  K  ",
        "KK    ",
        "K K   ",
        "K  K  ",
        "K    K"        
    };

    String[] arrayN = new String[]
    {
        "N    N",
        "NN   N",
        "N N  N",
        "N  N N",
        "N   NN",
        "N    N"
    };

    String[] arrayO = new String[]
    {
        " OOOO ",
        "O    O",
        "O    O",
        "O    O",
        "O    O",
        " OOOO "
    };

    String name = "farhana konka";
    name = name.toLowerCase();
    for(int j = 0; j < 6; j++)
    {
        for(int i = 0; i < name.length(); i++)
        {
            switch(name.charAt(i))
            {
                case 'a':
                    System.out.print(arrayA[j]);
                    break;
                case 'f':
                    System.out.print(arrayF[j]);
                    break;
                case 'r':
                    System.out.print(arrayR[j]);
                    break;
                case 'n':
                    System.out.print(arrayN[j]);
                    break;
                case 'o':
                    System.out.print(arrayO[j]);
                    break;
                case 'k':
                    System.out.print(arrayK[j]);
                    break;
                case ' ':
                    System.out.print(arraySpace[j]);
                    break;
                case 'h':
                    System.out.print(arrayH[j]);
                    break;
                default:
                    break;
            }
            System.out.print("  ");
        }
        System.out.println("");
    }
}

输出:

FFFFFF    AA    RRRRR   H    H    AA    N    N    AA        K    K   OOOO   N    N  K    K    AA    
F        A  A   R    R  H    H   A  A   NN   N   A  A       K  K    O    O  NN   N  K  K     A  A   
F       A    A  RRRRR   HHHHHH  A    A  N N  N  A    A      KK      O    O  N N  N  KK      A    A  
FFFF    AAAAAA  R  R    H    H  AAAAAA  N  N N  AAAAAA      K K     O    O  N  N N  K K     AAAAAA  
F       A    A  R   R   H    H  A    A  N   NN  A    A      K  K    O    O  N   NN  K  K    A    A  
F       A    A  R    R  H    H  A    A  N    N  A    A      K    K   OOOO   N    N  K    K  A    A  

开关盒,耶!它做完全相同的事情,除了现在我需要手动做一些我正在自动做的事情(通过地图中的字符索引数组).多傻.

Switch-case, yay! It does exactly the same thing, except now I need to do something manually that I was doing automatically (indexing the arrays by the character in a map). How silly.

这篇关于我如何在同一行java中输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆