[英]How do I combine to while loops so that I get the same output as I do with two while loops?
[英]How do I get data into a constructor from two different loops
我有一个家庭作业,要创建一个允许用户输入有关棋盘游戏集合的信息的应用程序。 用户输入的数据将通过 class 存储/访问。 我给出的步骤是创建名为 BoardGame 的 class,用于存储具有 8 个字段的(模拟或非数字)棋盘游戏集合。 然后我创建一个只存储前三个字段的构造函数。 然后,对于所有字段,我创建了单独的 get 和 set 方法,以及一个 toString 方法来打印所有字段的所有数据。 然后在 main 方法中有 3 个部分。 第 1 部分,创建一个名为 boardGames 的数组,其数组大小为 4(现在我只是将其设置为 1 进行测试)。 创建一个循环以仅获取前三条信息。 第 2 部分。在用户输入棋盘游戏的三个基本部分后,创建第二个循环以从用户那里获取剩余的五个信息。 第 3 部分。一旦用户输入并存储了所有剩余数据,创建第三个循环以使用 toString 方法(来自 BoardGame 类)为每个 object 打印数组中的所有数据。
我认为 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, publisherName, 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, maxPlayerNum, 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;
}
}
`
根据我的设置方式,前三个没有分配值,或者最后 5 个没有。 它目前的设置方式是使前三个数据块 null。
在您的第二个循环中,不要像当前代码中那样用新的 BoardGame object 覆盖当前的BoardGame
object:
boardGame[i] = new BoardGame (price, minPlayerNum, maxPlayerNum, genre, playTime);
您应该通过第一个循环检索已经存储在您的数组中的BoardGame
,并在其上调用您的设置器:
boardGame[i].setPrice(price);
boardGame[i].setMinPLayerNum(minPlayerNum);
//etc...
像这样,您不会覆盖/替换任何值,您只是向已经创建的 object 添加属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.