简体   繁体   中英

running boolean method from main

I'm trying to compare two integers or double parameters by using the method from main. I don't understand what the problem is. The parameters bitterChocolate_amount and milkChocolate_amount are defined as integers.

Main:

boolean x = equals(bitterChocolate_amount,milkChocolate_amount)

Method:

public boolean equals (Fat other)
{
    if (this == other) {
        return true;
    }
    else {
        return false;
    }
}

The error message is

  required: Object
  found: int,int
  reason: actual and formal argument lists differ in length
1 error

This public boolean equals (Fat other) is your method accept only one argument. But boolean x = equals(bitterChocolate_amount,milkChocolate_amount) in this you passing two argument.

Do like this

boolean x = equals(bitterChocolate_amount,milkChocolate_amount);

Method:

public boolean equals (int bitterChocolate, int milkChocolate)
{
      if (bitterChocolate == milkChocolate)
           return true;
       else 
           return false;
}

这个public boolean equals (Fat other)方法只接受一个参数,但是你在这里传递2个参数equals(bitterChocolate_amount,milkChocolate_amount)另外正如你所说的比较2个整数那么方法将是这样的

public boolean equals (int other,int someother)

the problem is your actual and formal parameters doesn't match
you can edit your method like this.

 boolean x = equals(bitterChocolate_amount,milkChocolate_amount)

Method:

public boolean equals (int value1,int value2)
{

      if (value1 == value2)
       {
           return true;
       }
       else 
       {
           return false;
       }
}

Actually there is no need for an "equals" method. Simply use operators like:

if(int_a == int_b) { // returns true or false
    //my code...
}

Take a look: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Like mentioned above, you're trying to pass two arguments (bitterChocolate_amount,milkChocolate_amount) to a method with only one available parameter (Fat other).

Not to mention the parameter type (Fat) is different to the arguments you're trying to pass in (int, int).

I can't really tell from the scope of the code you provided, but since you're calling that from a main class without any mention of an object instance you probably want to be declaring the method as static too.

This is bread and butter stuff here, I think you should be left alone to figure it out yourself as you'll learn a heck of a lot more that way. I'd recommend a good book if you're interested in long term investment; Head First books and Deitel and Deitel are pretty good.

使用一个参数调用方法并使用两个参数。

The error message is self-explanatory required: Object found: int,int

The way you defined your equals method, it expects one parameter of type "Fat". I assume you must have created the Fat class.

Now you are calling this method using two integer parameters, that's why its saying

required :object (of type "Fat") and found: int, int

Now lets focus on your requirement

  1. If you just want to compare two integer values, modify your equals method to the one defined below

     public boolean equals (int value1,int value2) { if (value1 == value2) { return true; } else { return false; } } 
  2. In your equals method, you have used "this", which means you should call it on an object. So it should be like this

     Fat f = new Fat(3); Fat g = new Fat(4); f.equals(g) 

    But in this case, as you are comparing objects, (this==other) will not work as both the objects are different. == compares the object themselves and not the value they are storing. SO I am not sure what you want to achieve.

It will be better to get the right answer if you clarify your requirements.

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