代码必须允许用户键入T,I,L的四个字母组合,并将其转换为单词TILT。但是,代码必须使用 - 和|打印出单词TILT。 - 表示字母T和I的顶部以及L. |的底部用于其他一切。需要有if和else语句和int行语句。
有没有人对如何实现这一点有任何建议?一切都有帮助! 这是我到目前为止,它只运行到打印用户输入的内容但它不打印使用 - 和|。
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("-----");
}
}
答案 0 :(得分:2)
此建议并不完全适合您的计划,但我相信您可以使其合适。我希望我不会为你编写代码来破坏它。
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();
}
}
当我使用参数printOut()
调用"TILT"
时,它会打印:
----- ----- | -----
| | | |
| | | |
| | | |
| ----- ----- |
如果需要,可以将字符串数组放入HashMap<Character, String[]>
以便于查找,而不是切换/ case语句。
如果您有任何疑问,请在评论中提问。