如何从两个不同的循环将数据获取到构造函数中

我要完成一项作业,以创建一个应用程序,使用户可以输入有关棋盘游戏的信息。用户输入的数据将通过类进行存储/访问。我得到的步骤是创建一个名为BoardGame的类,用于存储具有8个字段的(模拟或非数字)棋盘游戏的集合。然后,我创建一个仅存储前三个字段的构造函数。然后,对于所有字段,我创建单独的get和set方法,以及toString方法以打印所有字段的所有数据。然后在主要方法中有3个部分。第1部分,创建一个名为boardGames的数组,其数组大小为4(现在,我将其设置为1进行测试)。创建一个循环以仅获取前三个信息。第2部分。在用户输入了棋盘游戏的三个基本部分之后,创建第二个循环以从用户那里获取剩余的五个信息。第3部分。一旦所有剩余数据由用户输入并存储,创建第三个循环以使用toString方法(来自BoardGame类)为每个对象打印数组中的所有数据。

我认为BoardGame中的所有内容均已正确设置。我还可以使第1步或第2步与第3步一起使用,问题是我无法使第1步和第2步同时工作。数组具有前3条信息或后5条信息

我尝试制作多个构造函数。我尝试了很多可能都是XD新秀错误的东西

import java.util.Scanner;

public class Main {


    public static void main(String[] args) {
        Scanner scnr = new Scanner(System.in);

        BoardGame[] boardGame = new BoardGame[1];

        // Loop for first three peices of info
        for (int i = 0; i < boardGame.length; i++) {

            String gameName,publisherName,yearPublished;

            System.out.print("What is the name of the board game? ");
            gameName = scnr.nextLine();

            System.out.print("Publisher name? ");
            publisherName = scnr.nextLine();

            System.out.print("Year published? ");
            yearPublished = scnr.nextLine();

            boardGame[i] = new BoardGame (gameName,yearPublished);

        }

        // Loop for remaining peices of info
        for (int i = 0; i < boardGame.length; i++) {
            String genre;
            double price;
            int minPlayerNum,maxPlayerNum,playTime;

            System.out.print("How much does " + boardGame[i].getGameName() + " cost? ");
            price = scnr.nextDouble();

            System.out.print("What is the minimum number of players for " + boardGame[i].getGameName() + "? ");
            minPlayerNum = scnr.nextInt();

            System.out.print("What is the maximum number of players for " + boardGame[i].getGameName() + "? ");
            maxPlayerNum = scnr.nextInt();

            System.out.print("What is the game genre? ");
            scnr.nextLine();
            genre = scnr.nextLine();

            System.out.print("How long on average does it take to play " + boardGame[i].getGameName() + " (in minutes)? ");
            playTime = scnr.nextInt();

            boardGame[i] = new BoardGame (price,minPlayerNum,genre,playTime);


        }
        for (int i = 0; i < boardGame.length; i++) {
            System.out.println(boardGame[i].toString());
            System.out.println();
        }

    }

}


public class BoardGame {
    // Fields
    private String gameName;
    private String publisherName;
    private String yearPublished;
    private double price;
    private int minPlayerNum;
    private int maxPlayerNum;
    private String genre;
    private int playTime;

    // Constructor 1
    public BoardGame(String gameName,String publisherName,String yearPublished) {
        this.gameName = gameName;
        this.publisherName = publisherName;
        this.yearPublished = yearPublished;
    }

    // Constructor 2
    public BoardGame(double price,int minPlayerNum,int maxPlayerNum,String genre,int playTime) {
        this.price = price;
        this.minPlayerNum = minPlayerNum;
        this.maxPlayerNum = maxPlayerNum;
        this.genre = genre;
        this.playTime = playTime;
    }

    // Get Methods
    public String getGameName() {
        return gameName;
    }

    public String getPublisherName() {
        return publisherName;
    }

    public String getYearPublished() {
        return yearPublished;
    }

    public double getPrice() {
        return price;
    }

    public int getMinPlayerNum() {
        return minPlayerNum;
    }

    public int getMaxPlayerNum() {
        return maxPlayerNum;
    }

    public String getGenre() {
        return genre;
    }

    public int getPlayTime() {
        return playTime;
    }

    // Set Methods
    public void setGameName(String gameName) {
        this.gameName = gameName;
    }

    public void setPublisherName(String publisherName) {
        this.publisherName = publisherName;
    }

    public void setYearPublished(String yearPublished) {
        this.yearPublished = yearPublished;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public void setMinPlayerNum(int minPlayerNum) {
        this.minPlayerNum = minPlayerNum;
    }

    public void setMaxPlayerNum(int maxPlayerNum) {
        this.maxPlayerNum = maxPlayerNum;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public void setPlayTime(int playTime) {
        this.playTime = playTime;
    }
    // Prints the data FIX_ME if min and max number of players is the same,only print one number
    public String toString() {
        return "Game name: " + gameName + "\nPublisher Name: " + publisherName + 
                "\nYear Published: " + yearPublished + "\nPrice: " + price + 
                "\nminimum number of players: " + minPlayerNum + "\nmaximum number of players: "
                + maxPlayerNum + "\ngenre: " + genre + "\nPlay time: " + playTime;
    }

}
`

根据我的设置方式,前三个将没有分配的值,或者后五个将没有分配的值。目前,它的设置方式使前三个数据为空。

hbxflihua 回答:如何从两个不同的循环将数据获取到构造函数中

在第二个循环中,而不是像当前代码中那样用新的对象覆盖当前的BoardGame对象:

boardGame[i] = new BoardGame (price,minPlayerNum,maxPlayerNum,genre,playTime);

您应该在第一个循环之前检索已经存储在数组中的BoardGame,并在其上调用设置方法:

boardGame[i].setPrice(price);
boardGame[i].setMinPLayerNum(minPlayerNum);
//etc...

这样,您将不会覆盖/替换任何值,而只是向已创建的对象添加属性。

本文链接:https://www.f2er.com/3128428.html

大家都在问