所有内容都已编码,唯一阻止我运行该程序的是User Interface类中的以下2个错误。在“.add”下面有一条红色的波浪线,无论我做什么它都不会消失。任何帮助将不胜感激。
错误:
类型ArrayList中的方法add(BasketballStats)不适用于参数(Game)UserInterface.java
类型ArrayList中的方法add(BasketballStats)不适用于参数(Player)UserInterface.java
public class UserInterface {
public void begin() {
ArrayList < BasketballStats > bballstats = new ArrayList < BasketballStats > ();
boolean finished = false;
while (!finished) {
int selection = showMenu ();
switch(selection) {
case 1:
bballstats.add(addPlayer());
break;
case 2:
bballstats.add(addGame());
break;
case 3: JOptionPane.showMessageDialog (null, "Displaying Details of Player or Game Stats", null, JOptionPane.PLAIN_MESSAGE);
for (int i = 0; i < bballstats.size(); i++) {
JOptionPane.showMessageDialog (null, bballstats.get(i), "Players or Game Stats", JOptionPane.PLAIN_MESSAGE);
}
JOptionPane.showMessageDialog (null, "There are "+bballstats.size()+" record(s) in the list", "Total records", JOptionPane.PLAIN_MESSAGE);
break;
case 4: finished = true;
JOptionPane.showMessageDialog (null, "Program Terminated");
break;
default:
JOptionPane.showMessageDialog (null, "\n** Invalid Selection **\n", "ERROR", JOptionPane.ERROR_MESSAGE);
public class Player {
//Attributes
private String firstName;
private String lastName;
private double jerseyNumber;
private String mainPlayingPosition;
private String secondaryPlayingPosition;
private double height;
private double weight;
private double minutesPlayed;
//Constructors
public Player () {
super ();
firstName = null;
lastName = null;
jerseyNumber = 0;
mainPlayingPosition = null;
secondaryPlayingPosition = null;
height = 0.0;
weight = 0.0;
minutesPlayed = 0.0;
}
public Player (double p,double or,double dr,double a,double to,double pf,double b,double s,BasketballTeam bbt,String fn,String ln, double jn,String mpp, String spp, double h, double w, double mp) {
super ();
firstName = fn;
lastName = ln;
jerseyNumber = jn;
mainPlayingPosition = mpp;
secondaryPlayingPosition = spp;
height = h;
weight = w;
minutesPlayed = mp;
}
//Setters
public void setFirstName(String fn) {
firstName = fn;
}
public void setLastName(String ln) {
lastName = ln;
}
public void setJerseyNumber(int jn) {
jerseyNumber = jn;
}
public void setMainPlayingPosition(String mpp) {
mainPlayingPosition = mpp;
}
public void setSecondaryPlayingPosition(String spp) {
secondaryPlayingPosition = spp;
}
public void setHeight(double h) {
height = h;
}
public void setWeight(double w) {
weight = w;
}
public void setMinutesPlayed(double mp) {
minutesPlayed = mp;
}
//Getters
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public double getJerseyNumber() {
return jerseyNumber;
}
public String getMainPlayingPosition() {
return mainPlayingPosition;
}
public String getSecondaryPlayingPosition() {
return secondaryPlayingPosition;
}
public double getHeight() {
return height;
}
public double getWeight() {
return weight;
}
public double getMinutesPlayed() {
return minutesPlayed;
}
@Override
public String toString() {
return "Player's First Name: " + firstName +
"\n" + firstName + "'s Last Name: " + lastName +
"\n" + firstName + " " + lastName + "'s Jersey Number: " + jerseyNumber +
"\n" + firstName + " " + lastName + "'s Playing Position: " + mainPlayingPosition +
"\n" + firstName + " " + lastName + "'s Secondary Playing Position: " + secondaryPlayingPosition +
"\n" + firstName + " " + lastName + "'s Height: " + height +
"\n" + firstName + " " + lastName + "'s Weight: " + weight +
"\n" + firstName + " " + lastName + "'s Playing Time: " + minutesPlayed + super.toString();
}
}
package basketball;
public class Game {
//Attributes
private double gameNumber;
private String opponent, arenaLocation;
//Constructors
public Game () {
super();
gameNumber = 0;
opponent = null;
arenaLocation = null;
}
public Game (double p,double or,double dr,double a,double to,double pf,double b,double s,BasketballTeam bbt,double gn,String opp,String al) {
super ();
gameNumber = gn;
opponent = opp;
arenaLocation = al;
}
//Setters
public void setGameNumber(double gn) {
gameNumber = gn;
}
public void setOpponent(String opp) {
opponent = opp;
}
public void setArenaLocation(String al) {
arenaLocation = al;
}
//Getters
public double getGameNumber() {
return gameNumber;
}
public String getOpponent() {
return opponent;
}
public String getArenaLocation() {
return arenaLocation;
}
@Override
public String toString() {
return "Game Number: " + gameNumber +
"Opponent: " + opponent +
"Arena: " + arenaLocation + super.toString();
}
}
答案 0 :(得分:1)
您正尝试将Player
和Game
添加到ArrayList bballstats
,它只接受BasketballStats
类型的变量。 ArrayList
只能存储一种(ArrayList<TypeWhichCanBeStored>
)的值,因此您需要为每个值创建单独的列表。
ArrayList <BasketballStats> bballstats = new ArrayList <BasketballStats> ();
ArrayList <Game> games = new ArrayList <Game> ();
ArrayList <Player> players = new ArrayList <Player> ();