2017-01-30 98 views
0

我想分配一個隨機數到一個對象數組,我試圖將它分配給一個名爲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);   
}  
} 

然後我的樂隊班級代碼位於下方。我無法得到它的正常工作:

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; 
} 
+2

您有什麼問題? – alayor

+0

這個代碼編譯? – nullpointer

+1

'我無法正常工作:'你會得到什麼錯誤?你需要清楚地描述你的問題 – Yousaf

回答

0

你的類Bands有法競爭是不帶任何參數,並在main方法你調用有得分的方法高下:

int score = compete(score); 

所以我假設你要調用存在於您的競爭方法在另一類因此要解決這個問題喲應該:

  1. 在帶類,創建您的屬性得分的二傳手
  2. 創建一個新的樂隊對象:
  3. 將比分
  4. 打電話給你的競爭方法
 
    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; 
} 
0

問題是代碼的「整數分數」行

public int compete() 

compete函數需要任何參數,但你傳遞參數在這個功能

int score = compete(score); 

你也不能聲明var可用score並將其作爲參數傳遞給一行中的函數。