这是我在实验室中被要求的:
- 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/