2016-01-23 38 views
1

我有一個使用開關盒的練習。假設代碼如檢查是否選擇開關盒

int choice; 
do { 

    choice = //user input here; 

    switch (choice) { 
    case 1: //print something; break; 
    case 2: //print something; break; 
    case 3: //print something; break; 
    default: //print default; break; 
    } 
} while(condition); 

我希望用戶只能選擇一種情況。如果他們確實選擇了案例1,他們不能再選擇。

+0

這是'Java'還是'C#'或'Any'? – Ian

回答

0

定義私有布爾變量爲您的每一個選項,並設置其真,如果用戶調用每一個。然後在再次運行該選項代碼之前檢查其是否爲真。

例如:

bool _case1=false; 
int choice; 
do{ 
choice = //user input here; 
switch(choice){ 
    case 1: 
    if(!_case1) 
     { 
     //print something; 
     _case1=true; 
     } 
    else 
     { 
     //Tell the user "You have selected this option once" 
     }  
    break; 
    //etc 

} 
}while(condition); 
+0

非常感謝你!而使用List <>的C#方式也可以 –

0

您可以調用每種情況的方法,並有一些標記是否已選擇每個案例的布爾值,使用if語句檢查案例是否已被選中,如果是,則打印通知以選擇另一個案例,否則執行那個行動。

你也可以做到這一點,沒有單獨的方法,但我是模塊代碼的粉絲。 :)

0

我假設一個包含此開關情況的函數被反覆調用。如果你想要這樣的功能,我會指向Set。使用該組來存儲選項。如果一套已經包含某種選擇,則採取您認爲合適的任何行動。

使用一個集合比擁有一個全局的布爾值數組要好,因爲這可能會浪費大量內存,具體取決於您擁有的選擇數量。

這是否回答你的問題?

0

當用戶按下輸入

如果輸入是匹配的情況下則 存儲此輸入在一個列表中。

下一次你必須check this input

如果它屬於列表與否,

如果屬於然後如果不屬於那麼它將執行情況的情況下,將不執行 。

嘗試這種方式:

List<Integer> choiceList = new ArrayList<Integer>(); 

int choice; 
do { 
    choice = //user input here; 

    if (choiceList.contains(choice)) { 
    continue; 
    } 

    choiceList.add(choice); 

    switch(choice) { 
    case 1: //print something; break; 
    case 2: //print something; break; 
    case 3: //print something; break; 
    default: //print default; break; 
    } 
} while(condition); 
0

由於一些其他的答案說,你可以使用boolean檢查的情況下已使用與否。但是,您也可以使用int。使用int的優點是您可以指定每個案例可以使用多少次。例如,以下代碼只能使用每個案例一次。

import java.util.Scanner; 

public class Test { 
    public static void main(String[] args) { 
     Scanner user_input = new Scanner(System.in); 
     int choice; 
     int case1 = 0, case2 = 0, case3 = 0; 
     int case1lim = 1, case2lim = 1, case3lim = 1; 

     System.out.println("You may use may enter the number 1 " + case1lim + " times"); 
     System.out.println("You may use may enter the number 2 " + case2lim + " times"); 
     System.out.println("You may use may enter the number 3 " + case3lim + " times"); 

     do { 
      System.out.println("Please enter a number between 1 and 3"); 
      choice = user_input.nextInt(); 

      if(choice == 1 && case1 < case1lim || choice == 2 && case2 < case2lim || choice == 3 && case3 < case3lim) { 
       switch(choice){ 
        case 1: //print something; 
          case1++; 
          break; 
        case 2: //print something; 
          case2++; 
          break; 
        case 3: //print something; 
          case3++; 
          break; 
        default: //print default; 
          break; 
       } 
      } else { 
       System.out.println("Please pick another number since you have already used that case or you entered a bad value"); 
      } 
     } while(true); 
    } 
} 

但是如果你改變了行

int case1lim = 1, case2lim = 1, case3lim = 1; 

的值

int case1lim = 2, case2lim = 1, case3lim = 1; 

你可以使用兩次第一種情況下,所有的其他情況下一次。