简体   繁体   English

方法不能应用于给定类型

[英]Method cannot be applied to given type

public class HurdalQuentonA2Q1 {

  public static void main(String[] args) {

    double kgs ;

    double lbs ;

    double convertLBtoKG = convertLBtoKG(kgs, lbs) ;

  }
  static double convertLBtoKG(double lbs) {

    return lbs * 0.454 ; 


  }

  static double readWeight() {

    Scanner input = new Scanner(System.in) ;
    System.out.println("Enter your preferred system of weight measurement, k(for kg) or, p(for lb)") ;
    String userinput = input.nextLine() ;
      if(userinput == "p") {
        Scanner keyboard = new Scanner(System.in) ;
        System.out.println("Enter your dog's weight in lbs") ;
         double lbs = keyboard.nextDouble() ; 
          double kgs = convertLBtoKG ;

upon compilation an error pops up stating 编译时弹出错误,指出

method convertLBtoKG in class HurdalQuentonA2Q1 cannot be applied to given types
required: double
found: double,double

I have no clue whats causing this 我不知道是什么原因造成的

convertLBtoKG method has only 1 parameter, but you are calling it with 2 parameters. convertLBtoKG方法只有1个参数,但是您使用2个参数调用它。

static double convertLBtoKG(double lbs) 

change your method call to: 将您的方法调用更改为:

double convertLBtoKG = convertLBtoKG(lbs) ;

Your convertLBtoKG method: 您的convertLBtoKG方法:

static double convertLBtoKG(double lbs) {

is defined as a method that needs one double parameter ( double lbs ) and returns a double (the double after the word static ). 被定义为需要一个double参数( double lbs )并返回double (单词static后面的double )的方法。 To use it, you have to call it in a way that's consistent with the profile: give it one double parameter, and then do something with the value of the method call, which will be what the method returns. 要使用它,您必须以与配置文件一致的方式来调用它:给它一个double参数,然后使用方法调用的值做一些事情,这将是方法返回的结果。 Thus, instead of 因此,代替

double convertLBtoKG = convertLBtoKG(kgs, lbs) ;

you want 你要

lbs = convertLBtoKG(kgs);  // THIS IS STILL WRONG, SEE BELOW

(don't say double lbs = ... since you already declared double lbs; earlier). (不要说double lbs = ...因为您已经声明了double lbs;更早)。

Also, you will need to assign something to kgs before you use it in the method call. 另外,在方法调用中使用之前,您需要为kgs分配一些东西。

EDIT: Although I tried to fix the line that was giving you the error, it's totally wrong, and it doesn't belong there. 编辑:尽管我试图修复给您错误的行,但这是完全错误的,并且不属于该行。 The code that actually does the conversion is in readWeight , but you never call readWeight . 实际执行转换的代码在readWeight ,但是您永远不会调用readWeight You need to add something to main that calls it. 您需要在main中添加一些调用它的东西。 Then, in readWeight , instead of this line: 然后,在readWeight ,而不是以下行:

      double kgs = convertLBtoKG ;

this is where you would want to call the convert method: 这是您要调用convert方法的位置:

      double kgs = convertLBtoKG(lbs);

I think you need to study carefully how the flow of a program works. 我认为您需要仔细研究程序流程的工作方式。 Your main program has to call readWeight in order to get the code in readWeight called; 你的main程序有来电readWeight为了得到代码readWeight调用; the language doesn't automatically wire things together because you've put convertLBtoKG in both main and readWeight . 该语言不会自动将所有内容连接在一起,因为您已将convertLBtoKG放在mainreadWeight

您的convertLBtoKG的函数声明仅允许传递一个参数,但同时传递lbs和kgs。

You are passing in two double values to a method which is only accepting a single double argument. 您正在将两个double值传递给仅接受单个double参数的方法。 Either overload the method to create one which accepts two doubles, or call it with the appropriate arguments. 重载该方法以创建一个接受两个双精度值的方法,或者使用适当的参数对其进行调用。

Your methods get only 1 parameter double. 您的方法仅获得1个参数加倍。 But you called convertLBtoKG(kgs, lbs) 2 parameter in here. 但是您在这里调用了convertLBtoKG(kgs,lbs) 2参数。

upon compilation, the compiler will try to match the number of parameters and their type. 在编译时,编译器将尝试匹配参数的数量及其类型。 In this case, you are calling with two parameters but accepting only one. 在这种情况下,您使用两个参数进行调用,但仅接受一个。

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

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