简体   繁体   中英

How to use Java to print out letters?

The code must allow the user to type in a four letter combination of T,I,L and translate that into the word TILT. However, the code must print out the word TILT using - and |. The - is for the top parts of the letters T and I and the bottom of L. | is used for everything else. There needs to be if and else statements and int line statements.

Does anyone have any advice on how to achieve this? Anything helps! This is what I have so far, it runs only up to where it prints back what the user has entered but it does not print it out using - and |.

import.java.util.Scanner
public class letters
{
private char letter1, letter2, letter3, letter4;
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");

    letter1 = keyboard.next().charAt(0);
    letter2 = keyboard.next().charAt(0);
    letter3 = keyboard.next().charAt(0);
    letter4 = keyboard.next().charAt(0);

    System.out.println("You entered the following:" + letter1 +letter2 + letter3 + letter4);


}
// This method tracks which line to print out at a given time
public void printOut()
 {
     for (int line = 0; line , 5; line++)
          drawLine(line);
     }

 public void drawLine(int Line)
   {
    if (line == 0)
   {
      if (letter1 == 'T' || letter1 == 'I')
          System.out.println("-----");
       else
      System.out.println("|");
    }   
       else if (line < 4)
   {
       if(letter1 == 'L')
      System.out.println("  |  ")
      }
         if (line == 4)

         {
           if(letter1 == 'T')
           System.out.println("  |  ");

          else
             System.out.println("-----");
          }
  }

This suggestion doesn't fit exactly into your program, but I am sure you can make it fit:

static String[] linesT = { "-----",
                           "  |  ",
                           "  |  ",
                           "  |  ",
                           "  |  " };

static String[] linesI = { "-----",
                           "  |  ",
                           "  |  ",
                           "  |  ",
                           "-----" };

static String[] linesL = { "|    ",
                           "|    ",
                           "|    ",
                           "|    ",
                           "-----" };

private static void printOut(String letters) {
    for (int line = 0; line < 5; line++) {
        for (int letter = 0; letter < letters.length(); letter++) {
            switch (letters.charAt(letter)) {
                case 'T':
                    System.out.print(linesT[line]);
                    break;
                case 'I':
                    System.out.print(linesI[line]);
                    break;
                case 'L':
                    System.out.print(linesL[line]);
                    break;

                default:
                    throw new IllegalArgumentException("Cannot print letter " + letters.charAt(letter));
            }
            System.out.print(' ');
        }
        System.out.println();
    }
}

When I call printOut() with argument "TILT" , it prints:

----- ----- |     ----- 
  |     |   |       |   
  |     |   |       |   
  |     |   |       |   
  |   ----- -----   |   

If you want, you can put the string arrays into a HashMap<Character, String[]> for easier lookup instead of the switch/case statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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