4

簡単に説明をさせてください。私は 7 月にある企業で Java に関する 5 週間のコースを受講しました。彼らは、コンソール アプリ、crud 操作、mysql、n 層アーキテクチャなどの基本的なものをカバーしていました。コースが終わってから、私は仕事に戻ったのであまり使用しませんでしたが、他の医学的理由が表面化しました....何とか何とか.

会社から、学んだことを反映する簡単なプログラムを作るように言われました。私はほとんど保持していないことがわかりました。

私はビデオゲームのStarageプログラムを作ることにしました。ビデオゲームをじっと見つめるために使用されるため、本棚を探す必要がなくなります (または、ゲームをどのように保管するか)。

要するに、あるクラスから別のクラスへのユーザー入力を取得できないようです。私のメソッド AddGame 私のプレゼンテーション層では、ロジック層の NewGame メソッドにユーザー入力を送信することになっています。私が取得し続けるエラーは、列「タイトル」をnullにすることはできないということです。

これが私の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;

}

これが私のNewGameメソッドです

    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();
}
}

休止状態の使用方法を学んでおらず、コンソール アプリの作成方法しか知りません。私が調べたものはすべて、休止状態または Web アプリのいずれかでした。(コードがずさんで安っぽく見えたらごめんなさい)どんなアドバイスもとても役に立ちます。前もって感謝します!

4

4 に答える 4

1

ゲームクラスにインスタンス変数のセッターがあると仮定すると、

これを使って

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 メソッドは、ユーザーが入力したテキスト行を返します。そのデータは、セッターを使用してゲームのオブジェクトのインスタンス変数に格納されています。

于 2013-10-23T07:21:25.637 に答える
1

AddGameメソッドでは、オブジェクトにデータを入力しませんGames。ユーザーからデータを読み取りますが、それを捨てるだけです。

Games必要なすべてのパラメーターを受け入れるコンストラクターをオブジェクトに追加することをお勧めします。

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、たとえばaddGameとである必要がありnewGameます。

于 2013-10-23T07:21:42.873 に答える
1

私が見ている最も明白な問題はここにあります:

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();

ユーザーが通過するものは何でも基本的に無視しています。私はあなたが次のようなことをしたいと仮定しています:

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

これにより、基本的にGamesオブジェクトが設定されます。あなたが抱えている問題は、Gamesオブジェクトが初期化されているが、実際には何も入力されていないため、プログラムがnull値について不平を言うことです。

于 2013-10-23T07:22:07.190 に答える
0

以下のようにコードを変更する必要があります

scanline.nextLine();から値をに渡す必要が ありg.setTitle(), g.setRating()g.setPlatform()g.setDeveloper().

System.out.println("Please enter the title of your game:");
g.setTitle(scanline.nextLine());
System.out.println("Please enter the rating of your game:");
g.setRating(scanline.nextLine());
System.out.println("Please enter the platform for your game:");
g.setPlatform(scanline.nextLine());
System.out.println("Please thenter the developer of your game:");
g.setDeveloper(scanline.nextLine());
aref.NewGame(g);
于 2013-10-23T07:21:35.507 に答える