这就是我在实验室里被问到的:
- 在您的实验室4项目中创建一个名为ASCIIArt的新类。
- 在开始之前,我希望您决定要转换成5个字符 ASCII艺术。选择你可以在键盘上找到的任何5个(不包括下面的例子和功能键,如Enter,Backspace,F1,Esc ......),并考虑如何将它们变成ASCII艺术。
- 回到代码,告诉用户你可以为他们变成艺术品的字符,然后询问用户他们希望看到哪些字符变成ASCII艺术。
- 使用switch语句/结构,您应该实现所选的所有5个字符的大版本。
- 如果用户输入的是无效字符(不是您选择的5个字符中的一个),请告诉他们他们做出了无效选择(提示:在switch语句中使用默认情况)
- 确保您在整个程序中都有评论(包括程序开头的标题评论)。
醇>
这就是我所拥有的
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说未定义。
答案 0 :(得分:1)
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
{
}
}
答案 1 :(得分:0)
import java.io.IOException;
import java.util.Scanner;
public class ASCIIArt {
public static void main(String[] args) throws IOException {
functionDumpText();
}
public static void functionDumpText() throws IOException
{
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();
functionDumpText();
}
}
}
}