Re: [問題] 讓使用者輸入一個字元的程式碼是??
原文吃光…
試試,若下面的程式碼中,
把try區塊裡的東西寫在外面,把catch刪掉,
然後compiler完,輸入a,s,m,d以外的字~
=============================================
import java.io.*;
import java.util.Scanner;
public class test{
public enum Choose {a,s,m,d};
public static void main(String args[]) throws IOException {
Scanner key = new Scanner(System.in);
String cmd;
Choose chs = Choose.i;
boolean toCmd = true;
boolean again = true;
printAllCmd();
while(toCmd){
while(again){
try{
again = false;
cmd = key.nextLine();
chs = Choose.valueOf(cmd);
}
catch(IllegalArgumentException iax){
System.out.println("Error! Please enter again!");
again = true;
}
}
again = true;
switch (chs) {
case a:
System.out.println("success!");
break;
case s:
System.out.println("success!");
break;
case m:
System.out.println("success!");
break;
default:
System.out.println("Good-Bye!");
toCmd = false;
//break;
}
}
}
public static void printAllCmd(){
System.out.println("i. Insert a line");
System.out.println("m. Modify a line");
System.out.println("t. Cut");
System.out.println("c. Copy");
System.out.println("a. Paste");
System.out.println("u. Undo");
System.out.println("p. Print contents");
System.out.println("l. List commands");
System.out.println("e. Encode");
System.out.println("d. Decode");
System.out.println("s. Search");
System.out.println("f. Save file");
System.out.println("x. Exit");
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 124.11.187.194
→
11/16 08:52,
7年前
, 1F
11/16 08:52, 1F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 4 之 5 篇):