2016-01-23 40 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; 

你可以使用两次第一种情况下,所有的其他情况下一次。