如何重新启动开关菜单?

时间:2019-09-17 03:12:11

标签: java

我有一个switch菜单,我想在完成case之后重新启动。我了解它是通过While循环完成的,但问题是它以case 1

开头

这是我的文件UI,具有switch菜单:

     out.println("Choose an option:");
     out.println("1. Array of guests");
     out.println("2. Array of hosts");``

     while(exit == false) {

        // Calls functions defined in CL
         switch(menu){
             ``
             case 1:

                 CL.guests = new String [CL.define_size()];
                 CL.fill_array(CL.guests);

                 break;

             case 2:

                 CL.hosts = new String [CL.define_size()];
                 CL.fill_array(CL.hosts);

                 break;

        }

这是我的具有功能的CL文件

public class CL  {

public static BufferedReader in = new BufferedReader(
        new InputStreamReader(System.in));
public static PrintStream out = System.out;

static String [] guests;
static String [] hosts;

//Los arreglos se definen con un método

public static int define_size () throws IOException {

    out.println("Enter the size of the array");
    int number = Integer.parseInt(in.readLine());

    return number;       
}

public static void fill_array (String parray []) throws IOException{        
    for(int i = 0; i < parray.length; i++){
        out.println("Enter the value of " + (i+1) );
        parray[i] = in.readLine();            
    }                
}

public static void print_array(String [] parray){        
    for(int i = 0; i < parray.length; i++){
        out.println(parray[i]);
    }        
}

输出:

Choose an option::
1. Register guests
2. Register hosts

//Menu

1
Enter the size of the array
2
Enter the name of value 1
guest1 
Enter the size of the array // why does it loop to case 1 instead of menu? 

1 个答案:

答案 0 :(得分:1)

在提供的代码中,您都不会读入menu的值,因此menu始终设置为相同的值。另外,您可以在while循环之外打印菜单,因此它只会打印一次菜单。