簡體   English   中英

為什么在嘗試輸入參數時出現錯誤?

[英]Why do I get an error when trying to enter a parameter?

我不知道為什么,但是我在啟動我放在一起的代碼時遇到了問題。 出於某種原因,當我在方法中輸入參數時,代碼會顯示致命錯誤。 我已經檢查了幾次,但無法弄清楚我做錯了什么。

任何意見,將不勝感激。

    public class songcode 
 {

    /**
     * @param args the command line arguments
     */
    
    public class Songwriter{
        private int song;
        //variable for the amount of songs played
        private int royalty;
        //variable for the amount of payments
        private int identification_number;
        //id number of the song writer
        public String first_name;
        //string for the first name of songwriter
        public String last_name;
        //string for the last name of the songwriter
        
            public int Adding_song(){
                song = song + 1;
                if(song > 100){
                    System.out.println("You are a big star!");
                }
                return(song);
            }
            
            public int Requesting_check(){
                royalty = royalty + 10;
                System.out.println("You just got payed");
                return royalty;
            }
            
            public static void main(String[] args){
                
            }
    }

}

請參閱評論以獲得一些快速幫助:)

public class SongWriter {
    
  private int song;
  //variable for the number of songs played

  private int royalty;
  //variable for the number of payments

  private int identification_number;
  //id number of the song writer

  public String first_name;
  //string for the first name of songwriter

  public String last_name;
  //string for the last name of the songwriter
  
  public SongWriter(int myFavSong){
      //define default values for vars above
      
      this.song = myFavSong;
      //'This' references the Songwriter object not required but useful practice.
      
  }
    
  // Lower case first word upcase second : camelCase  
  public int addingSong(int numSongs){
    song = song + numSongs;
    if(song > 100){
      System.out.println("You are a big star!");
     }
     return(song);
  }

  public int requestingCheck(){
    royalty = royalty + 10;
    System.out.println("You just got payed");
    return royalty;
  }
  // The main method shouldn't be nested another class
  public static void main(String[] args){
        //In java because our variables and functions are not static
        //you need a reference to your SongWrite object
        SongWriter songWriter = new SongWriter();
        
        // Notice I modified your function to take in an int as a param
        songWriter.song = addingSong(5);
        
        System.out.println(songWriter.song);
  }
}

由於缺乏信息,我們有點難以解決您的問題。 請准確地向我們提供您采取了哪些步驟以及您遇到了哪些錯誤。

從我現在看到的,由於您使用的是內部類(另一個類中的類),您應該將其設為靜態:

 static class Songwriter{/*your code here*/ }
當你得到它時,你可以通過調用你的外部類在你的 main() 中創建一個該類的對象,所以在你的情況下它將是:
 songcode.Songwriter name = new songcode.Songwriter();
現在,您可以通過使用 name.method() 來使用內部類中的方法,例如:
 name.Requesting_check(); for(int i = 0; i<200; i++){ name.Adding_song(); }
就我個人而言,我會創建一個名為 Songwriter.java 的新 Java 文件,添加一個構造函數並使用它,但也許這不是您在這里測試的內容 ;-)

希望它確實對您有所幫助,請下次提供更詳細的信息,這樣我們就可以給您一個確切的答案,而無需猜測出什么問題以及您想用代碼實現什么目的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM