2014-11-03 180 views
3

我寫了一些代碼來檢查用戶是否輸入了1到5之間的數字,現在我還想讓我的代碼允許用戶輸入字母A,S,D或M.if if else語句的多個變量

有沒有一種方法可以組合代碼,我可以識別用戶是否輸入了1-5或A,S,D,M?

如何編輯下面的代碼,以便用戶可以輸入整數或字符?我是否必須在循環下面編寫一段代碼,以確定用戶沒有輸入1-5,但是輸入A,S,D或M,就像跳出循環一樣?或者它是一個獨立的循環。我感到很困惑!

import java.util.InputMismatchException; 
import java.util.Scanner; 

public class Selection { 
    Scanner readInput = new Scanner(System.in); 

    int selectionOne() { 
     int inputInt; 
     do { //do loop will continue to run until user enters correct response 
      System.out.print("Please enter a number between 1 and 5, A for Addition, S for subtraction, M for multiplication, or D for division: "); 
      try { 
       inputInt = readInput.nextInt(); //user will enter a response 
       if (inputInt >= 1 && inputInt <=5) { 
        System.out.print("Thank you"); 
        break; //user entered a number between 1 and 5 
       } else { 
        System.out.println("Sorry, you have not entered the correct number, please try again."); 
       } 
       continue; 
      } 
      catch (final InputMismatchException e) { 
       System.out.println("You have entered an invalid choice. Try again."); 
       readInput.nextLine(); // discard non-int input 
       continue; // loop will continue until correct answer is found 
      } 
     } while (true); 
     return inputInt; 
    } 
} 
+2

如果你的輸入可以是字母,但您可以在定義輸入字段作爲一個int,你猜什麼事情發生?你期望你的用戶輸入一個ASCII碼嗎?提示:字符串+解析。 – MarsAtomic 2014-11-03 22:28:31

+0

我對此很新,你能否進一步解釋如何將字符串加入到這個中?謝謝 – userQuestion 2014-11-03 22:46:06

+0

有沒有任何答案對您有幫助? – 2014-11-04 00:11:51

回答

0

如果您的輸入既可以是字符也可以是字母,爲什麼不改爲查找字符或字符串?然後,您可以毫無困難地查找「1」或「A」。

+0

這裏有回聲嗎? – MarsAtomic 2014-11-03 22:42:04

+0

我該如何修改它,以便查找字符串或字符?我對此很新,這對我來說很難理解。謝謝 – userQuestion 2014-11-03 22:42:41

+0

糟糕。我甚至沒有看評論。我會刪除,你可以回答。 – Tyler 2014-11-03 22:43:01

1

我建議不要使用int輸入,只需使用String輸入,並在需要時將其轉換爲整數。您可以使用Integer.parseInt(String)String轉換爲int

所以,當你檢查,如果輸入的是有效的,你需要檢查,如果輸入等於"A""S",1-5 "M""D",或任何值時,它被轉換爲int

所以要檢查它是否是其中一個角色,你可以這樣做:

if (input.equals("A") || input.equals("S") || input.equals("M") || input.equals("D")) 

然後通過5進行測試,如果它的值爲1的int,你可以這樣做:

if (Integer.parseInt(input) >= 1 && Integer.parseInt(input) <= 5) 

只要解析輸入到int,然後檢查範圍,因爲你已經完成。

此方法的返回類型將是String而不是int。如果你需要它是一個int出於任何原因,你可以解析值爲int,然後返回。但我只是將它作爲String返回。

我改變的最後一件事是catch塊。現在,而不是一個InputMismatchException(因爲他們現在可以進入String S,我把它改成NumberFormatException,如果String不能被轉換爲int嘗試是這將發生。例如,Integer.parseInt("hello")將拋出一個NumberFomatException因爲"hello"不能被表示爲一個整數。但是,Integer.parseInt("1")會被罰款,並會返回1

請注意,您應該首先測試的String等價,這樣你就不會進入你的block你有機會來測試所有之前你需要的條件。

的方法是這樣的:

String selectionOne() { 
    String input; 
    do { //do loop will continue to run until user enters correct response 
     System.out.print("Please enter a number between 1 and 5, A for Addition, S for subtraction, M for multiplication, or D for division: "); 
     try { 
      input = readInput.nextLine(); //user will enter a response 
      if (input.equals("A") || input.equals("S") || input.equals("M") || input.equals("D")) { 
       System.out.println("Thank you"); 
       break; //user entered a character of A, S, M, or D 
      } else if (Integer.parseInt(input) >= 1 && Integer.parseInt(input) <= 5) { 
       System.out.println("Thank you"); 
       break; //user entered a number between 1 and 5 
      } else { 
       System.out.println("Sorry, you have not entered the correct number, please try again."); 
      } 
      continue; 
     } 
     catch (final NumberFormatException e) { 
      System.out.println("You have entered an invalid choice. Try again."); 
      continue; // loop will continue until correct answer is found 
     } 
    } while (true); 
    return input; 
} 
+0

非常感謝你的幫助 – userQuestion 2014-11-04 01:02:42

+0

@userQuestion很好。如果您覺得我已經正確回答了您的問題,請點擊答案左側的複選標記以接受該問題。 – 2014-11-04 01:13:21

1

正如@MarsAtomic提到的,首先你應該將輸入更改爲String,而不是一個int這樣你就可以輕鬆地處理字符和數字。

變化:

int inputInt; 

要:

String input; 

然後改變:

inputInt = readInput.nextInt(); 

要:

input = readInput.next(); 

爲了適應閱讀String而不是int

現在你在2主要情況(2子情況)達到:

1) input is a single character 
    a) input is a single digit from 1-5 
    b) input is a single character from the set ('A', 'S', 'D', 'M') 
2) input is an error value 

而且,由於你是不是叫Scanner.nextInt,你並不需要使用try/catch語句,可以在else打印錯誤塊。

此外,你應該有你的方法返回一個charString代替int的,所以你可以同時返回1-5A,S,D,M。我會假設你想要返回一個char。如果您想要返回String,請在代碼段中將return input而不是return val

注:代碼波紋管可以簡化和縮短,我只是增加了變數和意見,企圖使每一步清楚地讀取或轉換的東西。你可以看看@ mikeyaworski的回答,以獲得更簡潔的方式。

這裏是你的代碼可能看起來怎麼樣:

char selectionOne() { 
     String input; 
     do { 
      input = readInput.next(); 
      // check if input is a single character 
      if(input.length() == 1) { 
       char val = input.charAt(0); 
       // check if input is a single digit from 1-5 
       if(Character.isDigit(val)) { 
        int digit = Integer.parseInt(input); 
        if (digit >= 1 && digit <=5) { 
         System.out.print("Thank you"); 
         return val; // no need to break, can return the correct digit right here 
        } else { 
         System.out.println("Sorry, you have not entered the correct number, please try again."); 
        } 
       } else { 
        // check if input is in our valid set of characters 
        if(val == 'A' || val == 'S' || val == 'M' || val == 'D') { 
         System.out.print("Thank you"); 
         return val; // return the correct character 
        } else { 
         System.out.println("Sorry, you have not entered the correct character, please try again."); 
        } 
       } 
      } else { 
       System.out.println("Sorry, you have not entered the correct input format, please try again."); 
      } 
     } while(true); 
    }