简体   繁体   English

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

[英]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: 当你得到它时,你可以通过调用你的外部类在你的 main() 中创建一个该类的对象,所以在你的情况下它将是:
 songcode.Songwriter name = new songcode.Songwriter();
Now you can use your methods that are within your inner class by using name.method() like: 现在,您可以通过使用 name.method() 来使用内部类中的方法,例如:
 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 ;-) 就我个人而言,我会创建一个名为 Songwriter.java 的新 Java 文件,添加一个构造函数并使用它,但也许这不是您在这里测试的内容 ;-)

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.希望它确实对您有所帮助,请下次提供更详细的信息,这样我们就可以给您一个确切的答案,而无需猜测出什么问题以及您想用代码实现什么目的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试使用具有相同名称和参数类型的两个方法时,为什么会出现编译错误? - Why do I get a compilation error when I try to have two methods with the same name and parameter type? 为什么在尝试从表单中获取数据时会出现此错误? - why do i get this error when trying to get data from a form? 尝试对ResultSet中检索到的列的值求和时为什么会出现错误? - Why do I get an error when trying to sum values of a column retrieved in a ResultSet? 尝试连接到系统的数据库服务器时,为什么会出现此错误? - Why do I get this error when trying to connect to a system's database server? 为什么在尝试将词法分析器作为 ANTLRv4 中 CommonTokenStream 的输入时出现错误 - Why do I get an error when trying to give a lexer as input of CommonTokenStream in ANTLRv4 尝试从数据适配器检索bean时为什么会出现错误(ClassCastException)? - Why do I get error (ClassCastException) when trying to retrive bean from my data adapter? 尝试对ResultSet中检索到的列的值求和时为什么会出现错误? - Why do I get an error when trying to sum values of a column retrieved in a ResultSet? 尝试创建新控制台时为什么会出现异常? - Why do I get an exception when trying to create a new Console? 尝试加密字符串时为什么会出现BadPaddingException? - Why do I get BadPaddingException when trying to encrypt a string? 尝试在表上绘制图形时为什么会出现NullPointerException? - Why do I get a NullPointerException when trying to draw graphics on a table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM