2013-10-30 67 views
0

我想使用一個數組在StringInstrument類中創建一個新對象。而不是StringInstrument ukulele = new StringInstrument();我把StringInstrument instrumentList [i] = new StringInstrument();;然而,它不斷給我錯誤「該數組只讀,從未寫入」和「變量instrumentList已經在方法main(String [])」中定義。我無法弄清楚我做錯了什麼。我的代碼顯示在下面。嘗試使用數組從一個類創建一個新對象

class StringInstrument {//begin class 
    //declare variables 
    boolean isTuned; 
    boolean isPlaying; 
    boolean band; 
    public String nameOfInstrument; 
    int numberOfStrings; 
    String nameofStringsInInstrument[] = {"E", "A", "D", "G", "B"}; //an array of string names 

    public StringInstrument() {//begin contructor 
     numberOfStrings = 5;  
     isTuned = false;  
     isPlaying = false; 
     band = false; 
    }//end constructor 

    public int NumberOfStrings(int stringNumber){//begin method 
     System.out.println("The number of strings for the " + nameOfInstrument + " is " + stringNumber); 
     return this.numberOfStrings = stringNumber; 
    }//end method 

    public String InstrumentNameGet() {//begin method 
     return nameOfInstrument; 
    }//end method 

    public void SetInstrumentName (String instrumentName) {//begin getter method 
      nameOfInstrument = instrumentName; 
     }//end method 

    public String InstrumentNameDisplay() {//begin method 
     System.out.println("Your instrument is the " + nameOfInstrument); 
     return nameOfInstrument; 
    }//end method 

    public boolean PlayInstrument(){//begin method 
     System.out.println("You are playing your " + nameOfInstrument); 
     return isPlaying = true; 
    }//end method 

    public boolean TuneInstrument(){//begin method 
     System.out.println("Tune " + nameOfInstrument); 
     return isTuned = true; 
    }//end method 

    public boolean stopTuneInstrument() {//begin method 
     System.out.println("The" + nameOfInstrument + " is out of tune."); 
     return isTuned = false; 
    }//end method 

    public boolean StopPlayInstrument() {//begin method 
     System.out.println("The " + nameOfInstrument + " has stopped playing"); 
     return isTuned = false; 
    }//end method 

    public boolean PlayInstrumentBand() {//begin method 
     System.out.println("The " + nameOfInstrument + " is playing in a band"); 
     return band = true; 
    }//end method 

    public boolean StopPlayInstrumentBand() {//begin method 
     System.out.println("The " + nameOfInstrument + " has stoped playing with the band"); 
     System.out.println("\n"); 
     return band = false; 
    }//end method 
}//end class 

public class RandyGilmanP3 {//begin class 

    public static void main(String[] args) throws Exception{//begin main 

      java.io.File file = new java.io.File("RandyGilmanP3.txt"); 

      //create a file  
      java.io.PrintWriter output = new java.io.PrintWriter(file); 


    //Declaring, creating, and intialize arrays 
      String[] instrumentList = new String [10]; 
      String[] instrumentContent = new String [10]; 
      int[] stringNumber = new int [10]; 

      //input string names into array 
      instrumentList[0] = "Guitar"; 
      instrumentList[1] = "Violin"; 
      instrumentList[2] = "Bass Guitar"; 
      instrumentList[3] = "Cello"; 
      instrumentList[4] = "Banjo"; 
      instrumentList[5] = "Sitar"; 
      instrumentList[6] = "Rabab"; 
      instrumentList[7] = "Viola"; 
      instrumentList[8] = "Harp"; 
      instrumentList[9] = "Ukulele"; 
      //input string amounts into array 
      stringNumber[0] = 5; 
      stringNumber[1] = 4; 
      stringNumber[2] = 5; 
      stringNumber[3] = 4; 
      stringNumber[4] = 5; 
      stringNumber[5] = 18; 
      stringNumber[6] = 3; 
      stringNumber[7] = 4; 
      stringNumber[8] = 47; 
      stringNumber[9] = 4; 

      for (int i = 0; i < instrumentContent.length; i++){//begin for loop 
       StringInstrument instrumentList[i] = new StringInstrument(); 
       output.println(instrumentList[i].InstrumentNameDisplay()); 
       output.println(instrumentList[i].NumberOfStrings(stringNumber[i])); 
       output.println(instrumentList[i].TuneInstrument()); 
       output.println(instrumentList[i].PlayInstrument());  
       output.println(instrumentList[i].PlayInstrumentBand()); 
       output.println(instrumentList[i].StopPlayInstrument()); 
      }//end for loop 

     }//end main 
    }//end class 


I thought I would add the correct version, it took about 9 hours but I think I finally achieved the desired result. 




class StringInstrument {//begin class 
    //declare variables 
    boolean isTuned; 
    boolean isPlaying; 
    boolean band; 
    public String nameOfInstrument; 
    int numberOfStrings; 
    String nameofStringsInInstrument[] = {"E", "A", "D", "G", "B"}; //an array of string names 


    public StringInstrument() {//begin contructor 

     numberOfStrings = 5;  
     isTuned = false;  
     isPlaying = false; 
     band = false; 
    }//end constructor 

    public int NumberOfStrings(int stringNumber){//begin method 
     System.out.println("The number of strings for the " + nameOfInstrument + " is " + stringNumber); 
     return numberOfStrings = stringNumber; 
    }//end method 

    public String InstrumentNameGet() {//begin method 
     return nameOfInstrument; 
    }//end method 

    public void SetInstrumentName (String instrumentName) {//begin getter method 
      nameOfInstrument = instrumentName; 
     }//end method 

    public String InstrumentNameDisplay() {//begin method 
     System.out.println("Your instrument is the " + nameOfInstrument); 
     return nameOfInstrument; 
    }//end method 

    public boolean PlayInstrument(){//begin method 
     System.out.println("You are playing your " + nameOfInstrument); 
     return isPlaying = true; 
    }//end method 

    public boolean TuneInstrument(){//begin method 
     System.out.println("Tune " + nameOfInstrument); 
     return isTuned = true; 
    }//end method 

    public boolean stopTuneInstrument() {//begin method 
     System.out.println("The" + nameOfInstrument + " is out of tune."); 
     return isTuned = false; 
    }//end method 

    public boolean StopPlayInstrument() {//begin method 
     System.out.println("The " + nameOfInstrument + " has stopped playing"); 
     return isPlaying = false; 
    }//end method 

    public boolean PlayInstrumentBand() {//begin method 
     System.out.println("The " + nameOfInstrument + " is playing in a band"); 
     return band = true; 
    }//end method 

    public boolean StopPlayInstrumentBand() {//begin method 
     System.out.println("The " + nameOfInstrument + " has stoped playing with the band"); 
     System.out.println("\n"); 
     return band = false; 
    }//end method 

    //Create an array of instrument objects 
    public static StringInstrument[] createInstrumentArray(){//begin method 
      StringInstrument[] instruments = new StringInstrument [10]; 

      //Loop that inputs random integers into the array 
       for (int i = 0; i < instruments.length; i++){//begin loop 
        instruments[i] = new StringInstrument(); 

       }//end loop 
      return instruments; 
    }//End method 

    public static void printInstrumentArray(StringInstrument[] instruments)throws Exception{ 

      //request input from user 
      java.io.File file = new java.io.File("RandyGilmanP3.txt"); 
      if(file.exists()){ 
       System.out.println("FILE ALREADY EXIST"); 
       System.exit(1); 
      } 
      //create a file  
      java.io.PrintWriter output = new java.io.PrintWriter(file);  
      String[] instrumentList = new String [10]; 
      int[] stringNumber = new int [10]; 
      //input string names into array 
      instrumentList[0] = "Guitar"; 
      instrumentList[1] = "Violin"; 
      instrumentList[2] = "Bass Guitar"; 
      instrumentList[3] = "Cello"; 
      instrumentList[4] = "Banjo"; 
      instrumentList[5] = "Sitar"; 
      instrumentList[6] = "Rabab"; 
      instrumentList[7] = "Viola"; 
      instrumentList[8] = "Harp"; 
      instrumentList[9] = "Ukulele"; 

      stringNumber[0] = 5; 
      stringNumber[1] = 4; 
      stringNumber[2] = 5; 
      stringNumber[3] = 4; 
      stringNumber[4] = 5; 
      stringNumber[5] = 18; 
      stringNumber[6] = 3; 
      stringNumber[7] = 4; 
      stringNumber[8] = 47; 
      stringNumber[9] = 4; 
       //Print an array of instruments and their actions 
       for (int i = 0; i < instruments.length; i++){//begin for loop 
        instruments[i].SetInstrumentName(instrumentList[i]); 
        output.println(instruments[i].InstrumentNameDisplay()); 
        output.println(instruments[i].NumberOfStrings(stringNumber[i])); 
        output.println(instruments[i].TuneInstrument()); 
        output.println(instruments[i].PlayInstrument());  
        output.println(instruments[i].PlayInstrumentBand()); 
        output.println(" "); 

       }//end for loop 
       output.close(); 
      }//end method 
}//end class 

public class RandyGilmanP3 {//begin class 

    public static void main(String[] args) throws Exception{//begin main 
     //declare instruments 
     StringInstrument[] instruments; 

     //create instrument array 
     instruments = StringInstrument.createInstrumentArray(); 

     //Print instrument array 
     StringInstrument.printInstrumentArray(instruments); 
    }//end main 



}//end class 
+0

更改StringInstrument instrumentList [i] = new StringInstrument(); to instrumentList [i] = new StringInstrument(); – davibq

+0

'instrumentList'應該是'String'還是'StringInstrument'的數組?你不能將它用於兩者。 –

+0

是instrumentList是一個String []還是一個StringInstrument []?如果第一個,你不能在for循環上爲它分配一個StringInstrument變量。如果第二個,你不能在頂部分配字符串名稱。 –

回答

0

像這樣的東西替換你的for循環:

StringInstrument[] stringInstruments = new StringInstrument [10]; 
for (int i = 0; i < stringInstruments.length; i++){//begin for loop 
     stringInstruments[i] = new StringInstrument(); 
     stringInstruments[i].SetInstrumentName(instrumentList[i]); //You want to initialize the name of the instrument here. 
     output.println(stringInstruments[i].InstrumentNameDisplay()); 
     output.println(stringInstruments[i].NumberOfStrings(stringNumber[i])); 
     output.println(stringInstruments[i].TuneInstrument()); 
     output.println(stringInstruments[i].PlayInstrument());  
     output.println(stringInstruments[i].PlayInstrumentBand()); 
     output.println(stringInstruments[i].StopPlayInstrument()); 
    }//end for loop 

不能使用相同的陣列來存儲一個字符串和StringInstrument。

+0

好的,確定的工作,所以謝謝你;然而,現在的結果如下:您的樂器爲空 空字符串的數量爲5 調零null 您正在播放null null正在播放樂隊 null已停止播放對於一次迭代,我怎麼才能讓它有吉他,小提琴或其他什麼東西來代替? –

+0

@RandyGilman,你確定'SetInstrumentName'工作正常嗎? –

+0

我開始變得非常困惑,我可能不得不退後一段時間,它似乎在工作。 –

0

你的第一陣列:

字符串[] instrumentList =新的String [10];

和在循環中的陣列:

StringInstrument instrumentList [I] =新StringInstrument();

必須有不同的變量名稱。

您還需要在循環外聲明並初始化您的StringInstrument數組,然後在循環並在其上運行各種方法之前爲其賦值。

+0

StringInstrument是我沒有添加代碼部分的類,因爲它很長。 –

相關問題