首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >需要帮助将用户输入发送到另一个方法

需要帮助将用户输入发送到另一个方法
EN

Stack Overflow用户
提问于 2013-10-23 07:16:47
回答 4查看 187关注 0票数 4

让我快速解释一下。7月份,我通过一家Java公司参加了为期5周的课程。它们涵盖了一些基本内容,如控制台应用程序、crud操作、mysql和n层体系结构。自从课程结束后,我没有多用它,因为我回去工作了,还有其他一些医学上的原因,surfaced....blah之类的。

公司告诉我要做一个简单的程序来反映我学到的东西。结果我只保留了很少的东西。

我决定制作一个视频游戏启动程序。它可以用来盯着你的电子游戏,这样你就不用搜索你的书架了(或者你怎么储存你的游戏)。

为了解决这个问题,我似乎无法从一个clas获得用户输入到另一个clas。我的表示层中的方法AddGame应该将用户输入发送到逻辑层中的NewGame方法。我一直遇到的错误是,列'title‘不能为null。

这是我的AddGame方法

代码语言:javascript
运行
复制
    public static Games AddGame() {
    Games g = new Games();
    Logic aref = new Logic();
    Scanner scanline = new Scanner(System.in);
    System.out.println("Please enter the title of your game:");
    scanline.nextLine();
    System.out.println("Please enter the rating of your game:");
    scanline.nextLine();
    System.out.println("Please enter the platform for your game:");
    scanline.nextLine();
    System.out.println("Please thenter the developer of your game:");
    scanline.nextLine();
    aref.NewGame(g);
    return g;

}

这是我的NewGame方法

代码语言:javascript
运行
复制
    public static void NewGame(Games g)
{
    try {
         Class.forName(driver).newInstance();
         Connection conn = DriverManager.getConnection(url+dbName,userName,password);
         PreparedStatement ps = (PreparedStatement) conn.prepareStatement("INSERT INTO games(Title,Rating,Platform,Developer) " +
                "VALUES(?,?,?,?)");
         ps.setString(1, g.getTitle());
         ps.setString(2, g.getRating());
         ps.setString(3, g.getPlatform());
         ps.setString(4, g.getDeveloper());
         ps.executeUpdate();
         conn.close();

         } catch (Exception e) {
         e.printStackTrace();
}
}

我还没有学会如何使用hibernate,我只知道如何制作控制台应用程序。我查到的所有东西要么是hibernate,要么是web应用程序。(不好意思,如果代码看起来很草率或拙劣)请任何建议都将是非常有用的。提前感谢!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-10-23 07:21:25

假设您的游戏类有实例变量的设置器,

使用这个

代码语言:javascript
运行
复制
System.out.println("Please enter the title of your game:");
g.setTitle(scanner.nextLine());

System.out.println("Please enter the rating of your game:");
g.setRating(scanner.nextLine());

System.out.println("Please enter the platform for your game:");
g.setPlatform(scanner.nextLine());

System.out.println("Please thenter the developer of your game:");
g.setDeveloper(scanner.nextLine());

nextLine方法返回用户输入的文本行,该数据正在使用setters存储在游戏对象的实例变量中。

票数 1
EN

Stack Overflow用户

发布于 2013-10-23 07:21:42

AddGame方法中,不使用任何数据填充Games对象。您从用户处读取数据,但只需将其丢弃。

我建议您在Games对象中添加一个构造函数,它接受所有必要的参数,例如:

代码语言:javascript
运行
复制
public static Games addGame() {

    Scanner scanline = new Scanner(System.in);
    System.out.println("Please enter the title of your game:");
    String title = scanline.nextLine();

    System.out.println("Please enter the rating of your game:");
    String rating = scanline.nextLine();

    System.out.println("Please enter the platform for your game:");
    String platform = scanline.nextLine();

    System.out.println("Please thenter the developer of your game:");
    String developer = scanline.nextLine();

    Games g = new Games(title, rating, platform, developer);
    aref.NewGame(g);
    return g;
}

附带注意: Java方法应该是camelCase__,例如addGamenewGame__。

票数 1
EN

Stack Overflow用户

发布于 2013-10-23 07:22:07

我所看到的最明显的问题是:

代码语言:javascript
运行
复制
Games g = new Games();
Scanner scanline = new Scanner(System.in);
System.out.println("Please enter the title of your game:");
scanline.nextLine();
System.out.println("Please enter the rating of your game:");
scanline.nextLine();
System.out.println("Please enter the platform for your game:");
scanline.nextLine();
System.out.println("Please thenter the developer of your game:");
scanline.nextLine();

实际上,您忽略了用户传递的任何内容。我假设你想做这样的事:

代码语言:javascript
运行
复制
Scanner scanline = new Scanner(System.in);
System.out.println("Please enter the title of your game:");
g.setTitle(scanline.nextLine());
...

这将基本上填充您的Games对象。您所遇到的问题是Games对象是初始化的,但从未真正填充任何内容,这会导致程序抱怨null值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19535232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档