简体   繁体   中英

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.

static double convertLBtoKG(double lbs) 

change your method call to:

double convertLBtoKG = convertLBtoKG(lbs) ;

Your convertLBtoKG method:

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

Also, you will need to assign something to kgs before you use it in the method call.

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 . You need to add something to main that calls it. Then, in readWeight , instead of this line:

      double kgs = convertLBtoKG ;

this is where you would want to call the convert method:

      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; the language doesn't automatically wire things together because you've put convertLBtoKG in both main and readWeight .

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

You are passing in two double values to a method which is only accepting a single double argument. 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. But you called convertLBtoKG(kgs, lbs) 2 parameter in here.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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