我在創建一類字符串工具問題時遇到了兩個問題。首先要求如下:將代碼寫入輸出文件,非公開類型錯誤
- main代碼中輸出文件的名稱必須是命令行中指定的名稱,您可以在其中找到所需的代碼。根據要求的規定,測試類必須在命令行中使用參數啓動:
java mynamep3tst myfilename.txt here myfilename.txt是所有輸出都必須執行的文件。這個文件名應該在程序中使用如下:
java.io.File file = new java.io.File(args [0]);
java.io.PrintWriter output = new java.io.PrintWriter(file);
和當你有一個消息被髮送到該文件,
output.println(消息);
我一直在這一點上收到一個錯誤:
public static void printInstrumentArray(StringInstrument[] instruments)throws Exception{
java.io.File file = new java.io.File(args[0]);
java.io.PrintWriter output = new java.io.PrintWriter(file);
錯誤說「找不到符號」,我知道是做什麼的時候我還沒有宣佈陣列。當我聲明數組args []時,它會說「線程中的異常」main「java.lang.NullPointerException」。
下一個問題是
當我運行這個它的工作原理,但它不斷彈出錯誤「通過公共API出口非公開型」。我曾試圖公開「StringInstrument []樂器」。但它會彈出錯誤「非法開始操作」。
public static void main(String[] args) throws Exception{//begin main //declare instruments StringInstrument[] instruments; //create instrument array instruments = createInstrumentArray(); //Print instrument array printInstrumentArray(instruments); }//end main //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{ java.io.File file = new java.io.File(args[0]); java.io.PrintWriter output = new java.io.PrintWriter(file); //declare and initialize arrays 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
謝謝你爲第二個問題工作。然而,當你說傳球到底是什麼意思?對不起,我是Java的新手。 –
您需要將args添加到rintInstrumentArray的參數列表中,使其看起來像這樣: 'public static void printInstrumentArray(StringInstrument [] instruments,String args [])throws Exception {' 然後更改行你在這裏調用它: 'printInstrumentArray(instruments,args);' – NapoleonBlownapart