gpt4 book ai didi

java - 需要帮助将用户输入发送到另一种方法

转载 作者:可可西里 更新时间:2023-11-01 07:35:20 29 4
gpt4 key购买 nike

让我快速解释一下。我在 7 月份通过一家公司参加了为期 5 周的 Java 类(class)。他们涵盖了基本的东西,比如控制台应用程序、crud 操作、mysql 和 n 层架构。类(class)结束后,我没怎么用它,因为我回去工作了,其他医疗原因浮出水面……等等。

公司让我做一个简单的程序来反射(reflect)我学到的东西。原来我保留的很少。

我决定制作一个视频游戏节目。它可以用来查看你的视频游戏,这样你就不必搜索你的书柜(或者你如何存储你的游戏。)

切入正题,我似乎无法获得从一个类到另一个类的用户输入。我的表示层中的方法 AddGame 应该将用户输入发送到逻辑层中的 NewGame 方法。我不断收到的错误是“标题”列不能为空。

这是我的 AddGame 方法

    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;

}

这是我的新游戏方法

    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 的,要么是网络应用程序。 (抱歉,如果代码看起来草率或蹩脚)请任何建议都会非常有帮助。提前致谢!

最佳答案

假设您的游戏类具有实例变量的 setter ,

用这个

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 方法返回用户输入的文本行,该数据使用 setter 存储在 Games 对象的实例变量中。

关于java - 需要帮助将用户输入发送到另一种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19535232/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com