【问题标题】:Method in class can not be applied to given types (java)类中的方法不能应用于给定类型(java)
【发布时间】:2013-09-08 07:12:51
【问题描述】:

我正在尝试在我的主要方法中使用另一个类中的方法,但我遇到了错误。它告诉我类中的方法不能应用于给定的类型。

public class studentclass {
public void inputloop(String[] args){

    Scanner scan = new Scanner(System.in);
    String[][] student = new String[10][];
    Double[][] results = new Double[10][];
    Double[] total = new Double[10];


    String tableln = "";

   for(int index = 0; index < student.length; index++){
    System.out.println("\nPlease enter student " + (index+1) + "'s details");
    String userinput = scan.nextLine();
    student[index] = userinput.split(",");
    results[index] = new Double[4];

    results[index][0] = Double.parseDouble(student[index][2]); 
    results[index][1] = Double.parseDouble(student[index][3]);
    results[index][2] = Double.parseDouble(student[index][4]);
    results[index][3] = Double.parseDouble(student[index][5]);
    total[index] = (results[index][0]*0.1+results[index][1]*0.4+
                    results[index][2]*0.2+results[index][3]*0.3);

    System.out.println("\nStudent name\tFAN \t\tResult1\tResult2\tResult3\tResult4\tTotal");
    tableln = tableln + "\n" + student[index][0] + "\t" + student[index][1] + "\t"
             + results[index][0] + "\t" + results[index][1] + "\t" + results[index][2] + "\t"
             + results[index][3] + "\t" + total[index];
    System.out.println(tableln);
    }
}

然后在我的主要方法中输入这个。

    public static void main(String[] args) {
    studentclass info = new studentclass();
    info.inputloop();
    }

它说“类 studentclass 中的方法 inputloop 不能应用于给定的类型。必需的 String[] 没有发现任何争论。”请帮我。谢谢

【问题讨论】:

    标签: java class methods


    【解决方案1】:

    你的方法的签名是:

    public void inputloop(String[] args)
    

    您应该将一个字符串数组传递给该方法:

    info.inputloop(someStringArray);
    

    更多信息请见this:

    括号中的参数列表——逗号分隔的输入列表 参数,前面是它们的数据类型,用括号括起来,()。 如果没有参数,则必须使用空括号。

    我没有看到你在使用这个参数,所以你可以简单地将你的方法签名更改为

    public void inputloop()
    

    现在这将是一个有效的调用,就像你所做的那样。

    【讨论】:

      【解决方案2】:

      您的方法声明表明它希望传递对字符串数组的引用:

      public void inputloop(String[] args)
      

      因此,如果传入String[] 类型的值,则只能以其当前形式调用该方法。

      但是,您实际上并没有在方法中的任何地方使用 args,所以我建议您只需将声明更改为:

      public void inputloop()
      

      我建议你阅读section on defining methods in the Java tutorial,或者在一本很好的Java 入门书籍中找到关于声明(和调用)方法的部分。如果您目前没有一本用于学习 Java 的书,我建议您尽快获得一本。虽然 Stack Overflow 非常适合回答特定问题,但它不适合学习基本概念 - 声明和调用方法比 任何 的答案要多得多。这个特殊的问题,一次一点地学习它是非常低效的。

      【讨论】:

      • 啊,是的,它有效。我显然还是新手。我有一本书 Addison & Wesley java 软件解决方案。但有些事情对我来说仍然没有意义。感谢您的帮助。
      • @Michael:我很高兴你有一本书——希望它是一本好书,虽然我不知道具体那本书。一定要重新阅读有关方法签名及其含义的部分。
      【解决方案3】:

      当方法应该获取字符串数组时,您正在尝试使用inputloop。

      我猜这个想法是将命令行args 传递给它。做:

      public static void main(String[] args)
      {
         studentclass info = new studentclass();
         info.inputloop(args);
      }
      

      【讨论】:

        【解决方案4】:

        这样试试

        public static void main(String[] args) {
            studentclass info = new studentclass();
            String[] student ={"John", "Mark"};
            info.inputloop(student);
            }
        } 
        

        或

        您可以将 public void inputloop(String[] args) 更改为 从未使用 public void inputloop() 作为输入参数

        【讨论】:

          猜你喜欢
          • 2019-03-22
          • 1970-01-01
          • 1970-01-01
          • 2012-10-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-01
          相关资源
          最近更新 更多