我有一个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?
答案 0 :(得分:1)
在提供的代码中,您都不会读入menu
的值,因此menu
始终设置为相同的值。另外,您可以在while循环之外打印菜单,因此它只会打印一次菜单。