Re: [問題] 讓使用者輸入一個字元的程式碼是??

看板java作者 (侯小)時間17年前 (2009/01/04 09:28), 編輯推噓0(001)
留言1則, 1人參與, 7年前最新討論串4/5 (看更多)
原文吃光… 試試,若下面的程式碼中, 把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
test
11/16 08:52, 1F
文章代碼(AID): #19O12xr8 (java)
討論串 (同標題文章)
文章代碼(AID): #19O12xr8 (java)