如何用短划线写出T I L T?

时间:2016-12-02 02:39:05

标签: java eclipse

该程序是为了能够让用户输入T,I,L的四个字母组合。

无论他们输入什么,都会被翻译成TILT,然后使用短划线( - )打印出TILT,用于T和I的顶部以及L的下半部分。 然后使用|其余部分的字母。基本上,这意味着让程序用这些字符绘制字母。但是,我不确定如何为代表“Do Something”添加内容。

这是我的代码:

import java.util.Scanner;

public class letters {

    private String input;

    //This method prompts the user to enter a combination of the letters T,I,L
    public void readIn() { 

        Scanner keyboard new Scanner(System.in);

        System.out.println("Enter four letters that consist of any combination of " + "T, I, or L:");

        input = keyboard.next().toUpperCase();

        System.out.println("You entered the following" + input);

        keyboard.close();
    }

    //end readIn
    public void printOut() {

        int line, count;
        char currentLetter;

        for (line = 0; line < 5; line++)
        {
            for (count = 0; count <4; count++)
            {
                currrentLetter = input.charAt(count);

                if (currentLetter == 'T')
                    System.out.println(" Do DOMETHING)(int line)
                else if (currentLetter == 'I')
                    // Do Something
                else
                    //Do SOmething
                    )
            }
        }

        //This method will print out the appropriate line for the letter T
        private void printT(int line)
        {
            if(line == 0)
                System.out.println("-----");
            else
                System.out.println("  |  "):
        }

        private void  printL(int line)
        {
            if (line < 4)
                System.out.println("|   ")
            else  
                System.out.println("-----");
        }

        private void printI(int line)
        {
            if (line == 0 || line == 4)
                System.out.println("----");
            else
                System.out.println("  |  ");
        }
}

1 个答案:

答案 0 :(得分:0)

这是我的解决方案。您可以输入T,I,L的任何组合来打印出来作为单词。我看不出你的方法如何能够以横向方式打印出TILT。我认为您需要将所有内容存储在String [] []中,然后立即将它们全部打印出来。

import java.util.Scanner;

public class Main {
private static String[][] letterList;

public static void main(String[] args) {
    letterList=new String[4][4];

    System.out.println("Enter any letter combo of T, I, or L");
    Scanner scanner=new Scanner(System.in);
    String input=scanner.nextLine();

    char[] charArray=input.toCharArray();
    int letterPos=0;

    for(char c: charArray) { 
        assignLetter(c,letterPos);
        letterPos++;
    }
    print();
}


public static void assignLetter(char c, int letterPos) {
    switch (c) {
    case 'T':
        addT(letterPos);
        break;
    case 'I':
        addI(letterPos);
        break;
    case 'L':
        addL(letterPos);
        break;
    }
}


public static void addT(int letterPos){
    letterList[0][letterPos]="-------";
    letterList[1][letterPos]="   |   ";
    letterList[2][letterPos]="   |   ";
    letterList[3][letterPos]="   |   ";
}

public static void addI(int letterPos){
    letterList[0][letterPos]=" | ";
    letterList[1][letterPos]=" | ";
    letterList[2][letterPos]=" | ";
    letterList[3][letterPos]=" | ";
}

public static void addL(int letterPos){
    letterList[0][letterPos]="|     ";
    letterList[1][letterPos]="|     ";
    letterList[2][letterPos]="|     ";
    letterList[3][letterPos]="|---- ";
}

private static void print() {
    for (int i=0; i<=3; i++) {
        System.out.println();
        for (int j=0; j<=3; j++) {
            System.out.print(letterList[i][j]);
        }
    }
}

}