2017-05-29 44 views
0

所以我對Java真的很陌生,我想要做的就是讓用戶輸入一個關鍵字,它將傳遞給不同的條件。如果關鍵字以「c」開始,它將執行cmethod。如果關鍵字以「n」開始,它將執行n方法。 問題是,如果用戶既不輸入以「c」或「n」開頭的內容,也想顯示一條消息。以下是我已經能夠思考(我知道,這個代碼將不能工作,雖然)如果用戶輸入不符合條件,返回消息

public static void main(String[] args) { 
    System.out.println("This program can randomize characters or numbers."); 
    System.out.println("Please input \"c\" for characters, and \"n\" for numbers. "); 
    Scanner choices = new Scanner(System.in); 

    if (choices.next().startsWith("c")) { 
     cmethod(); 
    } 

    else { 
     nmethod(); 
    } 

    //how do I execute the code below if none of the conditions are met? 
    else { 
     System.out.println("Keyword is wrong."); 
    } 
} 

編輯:試過否則,如果使用,但後來nmethod()被執行,用戶必須輸入「n」兩次。

+0

哪裏是'別人if' –

+0

更換第二否則與其他假設,即檢查輸入的字符是'N' ... – CBroe

+0

我建議嘗試將問題分解成幾個步驟...第1步:閱讀這個詞。第2步:檢查「c」。步驟3:如果不是「c」,請檢查「n」,步驟4:Sysout。您需要將讀取值存儲到變量中,因爲調用choices.next()兩次會導致有趣的結果。 –

回答

3

您必須將next()解壓縮爲變量。如果您不這樣做,next()將始終返回當前關鍵字並繼續下一個。 此外,如果你想檢查另一個關鍵字,你必須使用else if

public static void main(String[] args) { 
    System.out.println("This program can randomize characters or numbers."); 
    System.out.println("Please input \"c\" for characters, and \"n\" for numbers. "); 
    Scanner choices = new Scanner(System.in); 

    String keyword = choices.next(); 

    if (keyword.startsWith("c")) { 
     cmethod(); 
    } else if (keyword.startsWith("n")) { 
     nmethod(); 
    } else { 
     System.out.println("Keyword is wrong."); 
    } 
} 
+2

終於有人得到它。 – stealthjong

+0

謝謝!奇蹟般有效。 – Eustion

+0

很多人對'else if'感到憤怒^^ 作爲一種精確度,使用開關可能更合乎邏輯,因爲它屬於「應用不同行爲並獲得後備案例作品「 – Nathan

2

else關鍵字正是你所需要的。你應該用什麼來解決你的問題是else if。爲避免讀取if之間的其他輸入,只需將choices.next()存儲在變量中,然後在您的if的語句中調用此變量。

String entry = choices.next(); 
if (entry.startsWith("c")) { 
    cmethod(); 
} 
else if (entry.startsWith("n")) { 
    nmethod(); 
} 
else { 
    // default behaviour if wrong entry 
} 

的nmethod()將被稱爲僅當輸入不是「c」和是「N」,而如果這些條件都不滿足時,它會進入else一部分。

+0

你不能有多個'else'語句。中間線需要'else if'。 –

+1

夥計們...他不想檢查FIRST關鍵字對「c」,然後SECOND對「n」... –

+0

現在你的答案完美 –

1

即使你已經在這裏提供了一些可行的解決方案,這似乎不可思議,沒有人談到,很容易switch聲明。

基本上是:

public static void main(String[] args) { 
    System.out.println("This program can randomize characters or numbers."); 
    System.out.println("Please input \"c\" for characters, and \"n\" for numbers. "); 
    Scanner choices = new Scanner(System.in); 

    switch(choices.next().charAt(0)) { 
     case 'c': 
      cmethod(); 
      break; 
     case 'n': 
      nmethod(); 
      break; 
     default: 
      System.out.println("Keyword is wrong."); 
    } 
} 

此連接的事實next()功能只計算一次其他的答案。

+0

想知道如何用'switch'語句做到這一點。感謝您的信息! – Eustion

0

在你可以傳遞方法名稱的地方使用反射,如果方法存在,那麼它將被發現,否則它會拋出異常,你可以顯示消息。

java.lang.reflect.Method method; 
try { 
    method = this.getClass().getMethod(methodName, param1.class, 
param2.class, ..); 
} catch (SecurityException e) { ... } 
catch (NoSuchMethodException e) { ... } 

在下面的測試示例中嘗試使用示例將正確和錯誤的方法名稱。

public class Test { 

public static void main(String[] args) { 

    Object obj = new Test(); 

    java.lang.reflect.Method method; 
    try { 
     method = obj.getClass().getMethod("tryone1"); 
     System.out.println("Found"); 
    } catch (SecurityException e) { 
     System.out.println(" SecurityException Found"); 
    } catch (NoSuchMethodException e) { 
     System.out.println("Not Found"); 
    } 

} 

public void tryone() { 
    // TODO Auto-generated method stub 

} 

public void trytwo() { 
    // TODO Auto-generated method stub 

} 

} 

根據需要自定義上述示例。例如,如果c以參數形式出現,則String methodname = arg [0] +「method」並在getMethod(「tryone1」)方法中作爲方法參數名傳遞。

+0

想想將來如何運行任何可用的方法,並在未找到時顯示錯誤。 –

+0

我相信你不明白他的問題。他只有兩個選擇他必須選擇,並且不需要動態適應參數。 R – Nathan

0

嘗試:

public static void main(String[] args) { 
    System.out.println("This program can randomize characters or numbers."); 
    System.out.println("Please input \"c\" for characters, and \"n\" for numbers. "); 
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
    try{ 
    String s = br.readLine(); 
    if (s.startsWith("c")) { 
     System.out.println("c"); 
     System.exit(1); 
    } 
    if (s.startsWith("n")){ 
     System.out.println("n"); 
     System.exit(1); 
    }else { 
    System.out.println("Keyword is wrong."); 
    } 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

爲我工作,希望它爲你。

1

另一種方法:使用開關的情況下

String s =choices.next(); 
char first = s.charAt(0); 

switch(first){ 
    case 'c': cmethod(); break; 
    case 'n':nmethod(); break; 
    default : System.out.println("Keyword is wrong."); 
} 
相關問題