为什么我在尝试输入参数时会出错?

cczfrluj  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(260)

我不知道为什么,但是我在启动我编写的代码时遇到了问题。出于某种原因,当我在方法中输入参数时,代码显示了致命错误。我检查了好几次,但都不知道我做错了什么。
如有任何建议,将不胜感激。

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

            }
    }

}
zzwlnbp8

zzwlnbp81#

有关快速帮助,请参见注解:)

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

0wi1tuuw2#

由于缺乏信息,我们解决你的问题有点困难。请详细地告诉我们你采取了哪些步骤,犯了哪些错误。
从我现在看到的情况来看,由于您使用的是内部类(另一个类中的类),所以应该使其成为静态的:
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文件,添加一个构造函数并使用它,但这可能不是您在这里测试的;-)
希望它确实能帮助你,请下次提供更详细的信息,这样我们就可以给你一个准确的答案,而不必猜测什么是错的,你想用你的代码实现什么。

相关问题