为什么在尝试输入参数时出现错误?

时间:2020-09-03 02:06:38

标签: java

我不确定为什么,但是启动合并在一起的代码时遇到问题。由于某种原因,当我在方法中输入参数时,代码显示致命错误。我已经检查了几次,但无法弄清楚我做错了什么。

任何建议将不胜感激。

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

}

2 个答案:

答案 0 :(得分:0)

请参阅评论以获取一些快速帮助:)

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

答案 1 :(得分:0)

由于缺乏信息,因此我们很难解决您的问题。请准确地向我们提供您采取了哪些步骤以及遇到了哪些错误。

根据我现在所看到的,由于您正在使用内部类(另一个类内部的类),因此应将其设为静态:

static class Songwriter{/*your code here*/ }
得到后,可以通过调用外部类在main()中创建该类的对象,因此在您的情况下为: static class Songwriter{/*your code here*/ } 现在,您可以通过使用name.method()来使用内部类中的方法,例如:
songcode.Songwriter name = new songcode.Songwriter();
我个人将创建一个名为Songwriter.java的新Java文件,添加一个构造函数并使用该构造函数,但也许那不是您在此处测试的内容;-)

希望它确实对您有所帮助,请在下一次提供更详细的信息,因此我们可以为您提供准确的答案,而无需猜测问题出在哪里以及您希望用代码实现什么样的效果。

相关问题