【问题标题】:System.out.print did not work as intendedSystem.out.print 没有按预期工作
【发布时间】: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 上。

标签: java maven netbeans


【解决方案1】:

我认为你必须输入所有用户输入的“电子邮件”、“密码”、“名字”、“姓氏” Name","Phone Num","Address","DOB" 输入每个值后按回车键。输入所有值后,将执行剩余代码。

【讨论】:

  • 是的,它可以工作,sql 命令也可以完美工作,只是我希望控制台提示用户他们应该输入什么信息。像这样:电子邮件:abc@gmail.com 密码:123
【解决方案2】:

问题已解决,我只是在我的 read() 方法中将 print() 替换为 println()。这个问题似乎属于maven和ant项目之间的一些不同。我在 ant 中尝试了相同的代码,它执行得很好。

新的更新: 这个网站帮助我:https://github.com/mojohaus/exec-maven-plugin/issues/159。 基本上,您只需要将目标从 exec:exec 更改为 exec:java

See detail here

【讨论】:

  • 是的,谢谢,已更新最新版本的发布代码。我只是将while循环更改为: while ((c = read("Command [c/r/u/d/f/x]").charAt(0)) != 'x')
猜你喜欢
  • 2021-10-19
  • 2020-03-18
  • 2012-06-14
  • 2014-11-15
  • 1970-01-01
  • 2012-07-02
  • 2011-09-07
  • 2013-03-03
  • 2015-05-18
相关资源
最近更新 更多