Java代码、ASCII艺术、switch语句/结构、输入字符然后将其打印在屏幕上

标签 java switch-statement ascii-art

这是我在实验室中被要求的:

  1. Create a new class called ASCIIArt inside of your lab 4 project. 2. Before getting started, I want you to decide on 5 characters you would like to turn into ASCII art. Choose any 5 you can find on the keyboard (excluding the examples below and function keys like Enter, Backspace, F1, Esc...) and think about how you can make them into ASCII art. 3. Back to the code, tell the user what characters you can turn into art for them, and then ask the user what character they would like to see turned into ASCII Art. 4. Using a switch statement/structure, you should implement the large versions of all 5 characters you have chosen. 5. If the user enters in an invalid character (not one of the 5 you choose) tell them that they made an invalid choice (hint: use the default case in your switch statement) 6. Make sure you have comments throughout your program (including your header comment at the beginning of your program).

这是我所拥有的

import java.util.Scanner;
public class ASCIIArt {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Chose a letter to print: E, T, F, Z, I.");
        int ASCIIArt = keyboard.nextInt();
        switch (ASCIIArt) {
            case 'E': {
                System.out.println("*****");
                System.out.println("*    ");
                System.out.println("*****");
                System.out.println("*    ");
                System.out.println("*****");
                break;
            }
            case 'T': {
                System.out.println("*****");
                System.out.println("  *  ");
                System.out.println("  *  ");
                System.out.println("  *  ");
                System.out.println("  *  ");
                break;
            }
            case 'F': {
                System.out.println("*****");
                System.out.println("*    ");
                System.out.println("*****");
                System.out.println("*    ");
                System.out.println("*    ");
                break;
            }
            case 'Z': {
                System.out.println("*****");
                System.out.println("   * ");
                System.out.println("  *  ");
                System.out.println(" *   ");
                System.out.println("*****");
                break;
            }
            case 'I': {
                System.out.println("*****");
                System.out.println("  *  ");
                System.out.println("  *  ");
                System.out.println("  *  ");
                System.out.println("*****");
                break;
            }
        }
    }
}

我哪里搞砸了,不知何故我无法在最后添加默认含义。 ECLIPSE 说它未定义。

最佳答案

import java.io.IOException;
import java.util.Scanner;

public class ASCIIArt {
    public static void main(String[] args) throws IOException {

        while (true)
        {
            Scanner keyboard = new Scanner(System.in);

            System.out.println("Chose a letter to print: E, T, F, Z, I.");

            String asciiString = keyboard.next();

            switch (asciiString.charAt(0)) {

                case 'E': {
                    System.out.println("*****");
                    System.out.println("*    ");
                    System.out.println("*****");
                    System.out.println("*    ");
                    System.out.println("*****");
                    break;
                }
                case 'T': {
                    System.out.println("*****");
                    System.out.println("  *  ");
                    System.out.println("  *  ");
                    System.out.println("  *  ");
                    System.out.println("  *  ");
                    break;
                }
                case 'F': {
                    System.out.println("*****");
                    System.out.println("*    ");
                    System.out.println("*****");
                    System.out.println("*    ");
                    System.out.println("*    ");
                    break;
                }
                case 'Z': {
                    System.out.println("*****");
                    System.out.println("   * ");
                    System.out.println("  *  ");
                    System.out.println(" *   ");
                    System.out.println("*****");
                    break;
                }
                case 'I': {
                    System.out.println("*****");
                    System.out.println("  *  ");
                    System.out.println("  *  ");
                    System.out.println("  *  ");
                    System.out.println("*****");
                    break;  
                }
                default:
                {
                    System.out.println("Invalid char - press anykey");
                    System.in.read();                       
                }       
            }  
        }

    }

    public static void functionDumpText() throws IOException
    {

    }
}

关于Java代码、ASCII艺术、switch语句/结构、输入字符然后将其打印在屏幕上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28879880/

相关文章:

java - 我的 ASCII 艺术在 JLabel 上是错误的

java - 使用 Java 关闭 Tomcat 实例

ios - 根据按下按钮更新变量

java - 内存泄漏 - android.os.Message 持有的 Android Activity

C 中 switch 语句中的 case

java - 使用 switch 语句为 jbuttons java 提供操作

python - 如何垂直打印此列表?

html - 使用 CSS 将表样式设置为 ASCII 艺术表

java - 以编程方式删除所有 SdCard 文件

java - 为什么我的 slider 编辑器在 Vaadin 8 网格中不起作用?