简体   繁体   中英

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

I'm not sure why, but I'm having a problem launching the code that I put together. For some reason, the code shows fatal errors when I enter a parameter within the methods. I have checked it several times but could not figure out what I did wrong.

Any advice would be appreciated.

    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){
                
            }
    }

}

See the comments for some quick help :)

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);
  }
}

because of lack information it is a little hard for us to solve your problem. Please provide us exacly what steps did you take and what errors did you get.

From what i see now, since your are using inner class (class inside of another class), you should make it static:

 static class Songwriter{/*your code here*/ }
When you got that you can create an object of that class in your main() by calling your outer class, so in your case it would be:
 songcode.Songwriter name = new songcode.Songwriter();
Now you can use your methods that are within your inner class by using name.method() like:
 name.Requesting_check(); for(int i = 0; i<200; i++){ name.Adding_song(); }
Personally i would create a new java file called Songwriter.java, add a constructor and work with that, but maybe that's not what you were testing here ;-)

Hope it did help you and please next time provide more detailed informations, so we can give you an exact answer without guessing what's wrong and what exacly do you want to achieve with your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM