【发布时间】:2021-11-14 05:28:02
【问题描述】:
我目前正在用 java 做一个学校项目(我使用 NetBeans),我遇到了这个问题。我做了一些在线研究,但到目前为止还没有运气。这是我的代码的 sn-p: 问题是,每当我单击运行文件时,控制台仅显示 help() 方法中的消息,而不显示此消息“命令 [c/r/u/d/x]”。该消息仅在输入任何可能的选项后显示。任何帮助将不胜感激。
public static void main(String[] args) throws ClassNotFoundException, SQLException {
new TestDB().menu();
}
private DBConnector connector;
private Connection conn;
private DBManager manager;
public TestDB() throws ClassNotFoundException, SQLException {
connector = new DBConnector(); conn = connector.openConnection();
manager = new DBManager(conn);
}
private void testCreate() throws SQLException {
System.out.println("Adding customer to the database: ");
manager.addCustomer(read("Email"),read("Password"),read("First
Name"),read("Last Name"),read("Phone
Num"),read("Address"),read("DOB"));
System.out.println("Customer added successfully ");
}
private String read(String prompt) {
System.out.print(prompt + ": ");
System.out.flush();
return in.nextLine();
}
private String read(int prompt) {
System.out.print(prompt + ": ");
System.out.flush();
return in.nextLine();
}
private void menu() throws SQLException {
char c;
help();
while ((c = read("Command [c/r/u/d/f/x]").charAt(0)) != 'x') {
switch (c) {
case 'c':
testCreate();
break;
case 'r':
testFind();
break;
case 'u':
testUpdate();
break;
case 'd':
testDelete();
break;
default:
help();
break;
}
}
}
private void help() {
System.out.println("Database Operations: \n"
+ "c = Create User \n"
+ "r = Find User \n"
+ "u = Update User \n"
+ "d = Delete User \n");
}
【问题讨论】:
-
你能分享你的
main()方法吗? -
这里是:public static void main(String[] args) throws ClassNotFoundException, SQLException { new TestDB().menu(); } 私有 DBConnector 连接器;私人连接连接;私人 DBManager 经理;公共 TestDB() 抛出 ClassNotFoundException, SQLException { connector = new DBConnector(); conn = 连接器.openConnection();经理 = 新 DBManager(conn); }
-
试试这个,让我知道它是否有效:
c = read("Command [c/r/u/d/x]"); c.charAt(0); -
嗨,我试试你的,但是 c 的类型是 char,所以 Netbean 没有编译它。
-
编辑您的问题以包含您的 main() 方法,不要将其粘贴在 cmets 上。