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
4

2 回答 2

0

你的第一个数组:

字符串 [] 仪器列表 = 新字符串 [10];

和循环中的数组:

StringInstrument instrumentList[i] = new StringInstrument();

必须有不同的变量名。

您还需要在循环之外声明和初始化您的 StringInstrument 数组,并在循环之前为其赋值并在其上运行各种方法。

于 2013-10-30T23:35:26.260 回答
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

您不能使用同一个数组来存储 String 和 StringInstrument。

于 2013-10-30T23:45:46.733 回答