Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查是否选择了开关箱_Java_C# - Fatal编程技术网

Java 检查是否选择了开关箱

Java 检查是否选择了开关箱,java,c#,Java,C#,我有一个练习,使用开关盒。假设代码是 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(con

我有一个练习,使用开关盒。假设代码是

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,他们将无法再次选择。

您可以为每个案例调用方法,并使用一些布尔值来标记每个案例是否已被选择,使用If语句检查案例是否已被选择,如果已被选择,则打印一个通知以选择另一个案例,否则执行该操作


您也可以不使用单独的方法来实现这一点,但我非常喜欢模块化代码。:)

您可以为每个案例调用方法,并使用一些bool标记每个案例是否已被选择,使用if语句检查案例是否已被选择,如果已被选择,则打印通知以选择另一个案例,否则执行操作


您也可以不使用单独的方法来实现这一点,但我非常喜欢模块化代码。:)

我假设一个包含此开关盒的函数被反复调用。如果你想要这样的功能,我会指出。使用集合存储选择。如果一个集合已经包含某个选项,请采取您认为合适的任何操作

使用集合比使用全局布尔数组要好,因为这可能会浪费大量内存,具体取决于您的选择数量


这是否回答了您的问题?

我假设包含此开关盒的函数会被反复调用。如果你想要这样的功能,我会指出。使用集合存储选择。如果一个集合已经包含某个选项,请采取您认为合适的任何操作

使用集合比使用全局布尔数组要好,因为这可能会浪费大量内存,具体取决于您的选择数量


这是否回答了您的问题?

当用户按下输入键时

如果输入为匹配大小写,则 将此输入存储在一个列表中

下次您必须
检查此输入时

如果它是否属于列表中

如果属于,则案例将不会执行 如果不属于,则将执行该案例

试着这样做:

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);
List choiceList=new ArrayList();
智力选择;
做{
choice=//此处的用户输入;
if(choiceList.contains(选项)){
继续;
}
唱诗班成员。添加(选择);
开关(选择){
案例1://打印某物;中断;
案例2://打印某物;中断;
案例3://打印某物;中断;
默认值://打印默认值;中断;
}
}while(条件);

当用户按下输入键时

如果输入为匹配大小写,则 将此输入存储在一个列表中

下次您必须
检查此输入时

如果它是否属于列表中

如果属于,则案例将不会执行 如果不属于,则将执行该案例

试着这样做:

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);
List choiceList=new ArrayList();
智力选择;
做{
choice=//此处的用户输入;
if(choiceList.contains(选项)){
继续;
}
唱诗班成员。添加(选择);
开关(选择){
案例1://打印某物;中断;
案例2://打印某物;中断;
案例3://打印某物;中断;
默认值://打印默认值;中断;
}
}while(条件);

为每个选项定义一个私有bool变量,并在用户调用每个选项时将其设置为true。然后在运行该选项代码之前再次检查其是否为真

例如:

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);

为每个选项定义一个私有bool变量,并在用户调用每个选项时将其设置为true。然后在运行该选项代码之前再次检查其是否为真

例如:

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);

正如其他一些答案所说,您可以使用
布尔值来检查是否使用了案例。但是,您也可以使用
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
。使用
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);
    }
}


您可以使用第一个案例两次,所有其他案例一次。

这是
Java
还是
C
还是
Any
?这是
Java
还是
C
还是
Any
?非常感谢!!!而且C#way using List也非常适合你!!!使用列表的C#方式也同样有效