我正在尝试将一个随机数分配给一个对象数组,我试图将它分配给一个名为score的变量。我的主要类代码如下:
import java.util.Random;
public class App {
public static void main(String[] args) {
Bands[] bands = new Bands[5];
bands[0] = new Bands("Joe", "Rick", "Nick", "Dalton", "Doylestown, PA", "RockOn", 4000.50 , "Rock");
bands[1] = new Bands("Luke", "Bill", "Ian", "Matt", "State College, PA", "Blink182", 3500.50 , "Alternative");
bands[2] = new Bands("Corey", "Noah", "Jon", "Kaleb", "Philadelphia, PA", "Rise Against", 10000.50 , "Rock");
bands[3] = new Bands("Jake", "Joey", "Mike", "Mac", "New York, NY", "Thousand Foot Krutch", 2000.50 , "Rock");
bands[4] = new Bands("Bob", "Jeff", "Dom", "Mark", "King of Prussia, PA", "Skillet", 5500.50 , "Rock");
int score = compete(score);
}
}
然后我的Bands类代码位于下方。我无法让它正常工作:
import java.util.Random;
public class Bands {
private String singerName;
private String guitarName;
private String bassistName;
private String drummerName;
private String Hometown;
private String bandName;
private double income;
private String genre;
private int score;
public Bands(String singerName, String guitarName, String bassistName, String drummerName, String Hometown, String bandName, double income, String genre)
{
this.singerName = singerName;
this.guitarName = guitarName;
this.bassistName = bassistName;
this.drummerName = drummerName;
this.bandName = bandName;
this.Hometown = Hometown;
this.income = income;
this.genre = genre;
this.score = -1;
}
public int compete()
{
Random rand = new Random();
this.score = rand.nextInt(20);
return score;
}
public String getInfo()
{
String bandInfo = "Band: " + this.bandName + ", Singer: " + this.singerName + ", Guitarist: " + this.guitarName + ", Bassist: " + this.bassistName +
", Drummer: " + this.drummerName + ", Hometown: " + this.Hometown + ", Income: " + this.income + ", Genre: " +
this.genre + ", Final Score: " + this.score;
return bandInfo;
}
答案 0 :(得分:0)
问题是" int得分"代码行
public int compete()
这个compete
函数不需要参数,但是你在这个函数中传递一个参数
int score = compete(score);
您也无法声明变量score
并将其作为参数传递给一行中的函数。
答案 1 :(得分:0)
您的班级Bands
有方法竞争不参加任何参数,而在您的主要方法中,您调用的方法是竞争得分:
int score = compete(score);
所以我假设您想要调用另一个类中存在的竞争方法,以便解决此问题,您应该:
Bands b = new Bands(); b.setScore(score); int score = b.compete();
或
更改竞争方法以获取属性分数
public int compete(int score){
Random rand = new Random();
this.score = rand.nextInt(20);
return score;
}