获取错误并且看不到创建控制台菜单的错误
Getting errors and cannot see the mistake creating a console menu
这是产生 2 个错误的代码(我很抱歉,因为这是我第一次使用这个网站,并且不完全确定如何做所有的事情,但错误已在第一个错误和 s 的菜单中标记为 m第二个错误的scanchoice)
java:22: error: cannot find symbol choiceentry = menu();
java:52: error: cannot find symbol choiceentry = scanchoice.nextInt();
import java.util.*;
import java.io.*;
public class Student
{
public static void main(String[] args)
{
int choiceentry;
Scanner input = new Scanner(System.in);
choiceentry = menu();
while (choiceentry != 6)
{
if(choiceentry == 1)
{
// ..do something
}
else if(choiceentry == 2)
{
//..something else
}
else if(choiceentry == 3)
{
//...something else
}
else if(choiceentry == 4)
{
// ..something else
}
else if(choiceentry == 5)
{
//..something else
}
else if(choiceentry == 6)
{
System.exit(0);
}
else
{
System.out.println("Enter \"1\", \"2\", \"3\", \"4\", \"5\" or \"6\"");
choiceentry = scanchoice.nextInt();
}
}
}
}
这是我用来设置菜单的代码,构建良好
import java.util.*;
import java.io.*;
public class Enroll
{
//Creation of Console Menu
public static int menu()
{
int selection;
Scanner input = new Scanner(System.in);
/***************************************************/
System.out.println("Please Select an Option:");
System.out.println("-------------------------");
System.out.println("0 - Input Course Details");
System.out.println("1 - Search");
System.out.println("2 - Add Student");
System.out.println("3 - Delete Student");
System.out.println("4 - Report (Percentage of M & F Students)");
System.out.println("5 - Save");
System.out.println("6 - Quit");
selection = input.nextInt();
return selection;
}
//End Menu
}
编辑:很好,@Tom,我刚刚对应该处理 != 6
案例的答案进行了修改。
您需要通过 Enroll
class 访问 menu()
函数,如下所示:
choiceentry = Enroll.menu();
请注意,Enroll
class 中的唯一方法 menu()
是静态的,因此您不需要创建 Enroll
[=36] 的实例=].
这一行:
choiceentry = scanchoice.nextInt();
应该是这样才能使用Scanner
参考:
choiceentry = input.nextInt();
另外一件事,您似乎希望用户能够输入多个命令,直到他们选择退出。看看下面代码中 hasAnswered
标志的使用。
有改动的代码:
import java.util.*;
import java.io.*;
public class Student
{
public static void main(String[] args)
{
int choiceentry;
Scanner input = new Scanner(System.in);
choiceentry = Enroll.menu(); //Access through the Enroll class
if(choiceentry == 6)
{
//Exit if user entered 6
System.exit(0);
}
while (true) //I hate to put while(true) in code, but it seems appropriate here
{
boolean hasAnswered = false; //use a flag to determine if the user entered a valid command
if(choiceentry == 1)
{
hasAnswered = true;
// ..do something
}
else if(choiceentry == 2)
{
hasAnswered = true;
//..something else
}
else if(choiceentry == 3)
{
hasAnswered = true;
//...something else
}
else if(choiceentry == 4)
{
hasAnswered = true;
// ..something else
}
else if(choiceentry == 5)
{
hasAnswered = true;
//..something else
}
else
{
System.out.println("Enter \"1\", \"2\", \"3\", \"4\", \"5\" or \"6\"");
choiceentry = input.nextInt(); //use the Scanner
if(choiceentry == 6)
{
//Exit if user entered 6
System.exit(0);
}
}
if (hasAnswered == true){
hasAnswered == false;
//user had issued a valid command, prompt for the next command
choiceentry = Enroll.menu(); //Access through the Enroll class
if(choiceentry == 6)
{
//Exit if user entered 6
System.exit(0);
}
}
}
}
}
这是产生 2 个错误的代码(我很抱歉,因为这是我第一次使用这个网站,并且不完全确定如何做所有的事情,但错误已在第一个错误和 s 的菜单中标记为 m第二个错误的scanchoice)
java:22: error: cannot find symbol choiceentry = menu();
java:52: error: cannot find symbol choiceentry = scanchoice.nextInt();
import java.util.*;
import java.io.*;
public class Student
{
public static void main(String[] args)
{
int choiceentry;
Scanner input = new Scanner(System.in);
choiceentry = menu();
while (choiceentry != 6)
{
if(choiceentry == 1)
{
// ..do something
}
else if(choiceentry == 2)
{
//..something else
}
else if(choiceentry == 3)
{
//...something else
}
else if(choiceentry == 4)
{
// ..something else
}
else if(choiceentry == 5)
{
//..something else
}
else if(choiceentry == 6)
{
System.exit(0);
}
else
{
System.out.println("Enter \"1\", \"2\", \"3\", \"4\", \"5\" or \"6\"");
choiceentry = scanchoice.nextInt();
}
}
}
}
这是我用来设置菜单的代码,构建良好
import java.util.*;
import java.io.*;
public class Enroll
{
//Creation of Console Menu
public static int menu()
{
int selection;
Scanner input = new Scanner(System.in);
/***************************************************/
System.out.println("Please Select an Option:");
System.out.println("-------------------------");
System.out.println("0 - Input Course Details");
System.out.println("1 - Search");
System.out.println("2 - Add Student");
System.out.println("3 - Delete Student");
System.out.println("4 - Report (Percentage of M & F Students)");
System.out.println("5 - Save");
System.out.println("6 - Quit");
selection = input.nextInt();
return selection;
}
//End Menu
}
编辑:很好,@Tom,我刚刚对应该处理 != 6
案例的答案进行了修改。
您需要通过 Enroll
class 访问 menu()
函数,如下所示:
choiceentry = Enroll.menu();
请注意,Enroll
class 中的唯一方法 menu()
是静态的,因此您不需要创建 Enroll
[=36] 的实例=].
这一行:
choiceentry = scanchoice.nextInt();
应该是这样才能使用Scanner
参考:
choiceentry = input.nextInt();
另外一件事,您似乎希望用户能够输入多个命令,直到他们选择退出。看看下面代码中 hasAnswered
标志的使用。
有改动的代码:
import java.util.*;
import java.io.*;
public class Student
{
public static void main(String[] args)
{
int choiceentry;
Scanner input = new Scanner(System.in);
choiceentry = Enroll.menu(); //Access through the Enroll class
if(choiceentry == 6)
{
//Exit if user entered 6
System.exit(0);
}
while (true) //I hate to put while(true) in code, but it seems appropriate here
{
boolean hasAnswered = false; //use a flag to determine if the user entered a valid command
if(choiceentry == 1)
{
hasAnswered = true;
// ..do something
}
else if(choiceentry == 2)
{
hasAnswered = true;
//..something else
}
else if(choiceentry == 3)
{
hasAnswered = true;
//...something else
}
else if(choiceentry == 4)
{
hasAnswered = true;
// ..something else
}
else if(choiceentry == 5)
{
hasAnswered = true;
//..something else
}
else
{
System.out.println("Enter \"1\", \"2\", \"3\", \"4\", \"5\" or \"6\"");
choiceentry = input.nextInt(); //use the Scanner
if(choiceentry == 6)
{
//Exit if user entered 6
System.exit(0);
}
}
if (hasAnswered == true){
hasAnswered == false;
//user had issued a valid command, prompt for the next command
choiceentry = Enroll.menu(); //Access through the Enroll class
if(choiceentry == 6)
{
//Exit if user entered 6
System.exit(0);
}
}
}
}
}