我正在尝试建立一个食谱程序,询问人们有关食谱的信息并存储它。 菜单位于一个只运行的案例陈述中,他们会为他们尝试访问的内容推送任何数字。但是,无论何时按下任何按钮,它都会跳过该选项中的内容并返回菜单。我试过调试,但似乎它识别输入,然后回到菜单。这是它尝试访问的类和第一种方法(但不是
package potluck;
import java.util.*;
import potluck.*;
public class Controller {
private Scanner input;
private Member member;
private AdminMember admin;
private final static int USER_LOGIN = 0;
private final static int CREATE_MEMBER = 1;
private final static int CREATE_ADMIN = 2;
private final static int CREATE_RECIPE = 3;
private final static int COMMENT = 4;
private final static int DELETE_RECIPE = 5;
private final static int EXIT = 8;
public Controller(){
input = new Scanner(System.in);
startUp();//no better name to be thought of
}
public void startUp() {
// TODO Auto-generated method stub
int choice;
do {
this.displayMenu();
choice = input.nextInt();
input.nextLine();// clears carriage return
//depending on choice takes to a different menu
switch (choice) {
// case USER_LOGIN:
// member.enterPassword();
// break;
case CREATE_MEMBER:
new Member();
break;
case CREATE_ADMIN:
Member.addAdmin();
break;
case CREATE_RECIPE:
Recipe.addRecipe();
break;
case COMMENT:
Recipe.addComment();
break;
case DELETE_RECIPE:
Recipe.deleteRecipe();
break;
case EXIT:
System.out.println("Thanks for using our software");
System.exit(choice);
break;
default:
System.out.println("Error, Invalid selection.");
}
} while (choice != 8); //choice 8 exits
}
private void displayMenu() {
System.out.println("1 Create Member");
System.out.println("2 Create Admin Member");
System.out.println("3 Create Recipe");
System.out.println("4 Leave Comment");
System.out.println("5 Delete Recipe");
System.out.println("8 Exit");
System.out.println("Please enter menu option, to exit enter 8");
}
}
这是它尝试访问的成员类。 包装盒; import java.util.Scanner; 公共级会员{
private int memberID = 0;
private String name;
private String alias;
private String email;
private String username;
private String password;
private Scanner scanner = new Scanner (System.in);
public void Member(){ //constructor
memberID =+ 1;
name = "";
alias = "";
email = "";
username = "";
password = "";
setName();
setAlias();
setEmail();
setUsername();
setPassword();
System.out.println("Member Created");
}
public void addMember() {
new Member();
}
所以就像回顾一样。我现在只想让CREATE_MEMBER选项正常工作。无论出现什么问题,其余的都可能出错。
答案 0 :(得分:4)
您没有将Member
分配给您的变量:
member = new Member();
另请注意,您所谓的构造函数实际上并不是一个。您需要将void
删除为一个:
public Member() { //constructor
// code
}
答案 1 :(得分:2)
您使用Member
类就像调用其static
方法一样。您必须调用构造函数并创建类的实例。
Member member = new Member();
任何类的构造函数都不需要返回类型,因此请删除void
字。
public Member() {
...
}
如果要将1
作为属于** all * instances的全局变量添加到您的memberID,则必须将该变量定义为static
。所以:
private static int memberID = 0;
最后,您的addmember(..)
方法无效。如果要在创建的实例Member member = new Member();
中初始化实例变量,则必须修改构造函数或创建大量的setter。
public class Member {
// constructor
public Member(String name, String alias ...) {
this.name = name;
this.alias = alias;
...
}
// setters
public void setName(String name) {
this.name = name;
}
public void setAlias(String alias) {
this.alias = alias;
}
...
}