方法等于搜索if是否等于两个数字

时间:2017-06-13 16:26:30

标签: java equals

我想帮助我。我应该创建一个等于检查两个对象的方法。如果对象相等则返回true,否则返回false。 对我来说我已经为此做过工作,但我采用了不同的方式,过去采用了这种方法

public static boolean trueorfalse(int cars,int bikes){
   boolean y;
   if(get.car==get.bikes)
   {
    System.out.println("are equal");
    y=true;
   }
    else
   {
    System.err.println("arent equal");
    y=false;
   }
 return y;
}

现在方法相同:

public static boolean equal(int cars,int bikes){  //i change the name?
       if(get.car==get.bikes)   // may needs here equalsof() something like this? or is completely wrong i am.
       {
        System.out.println("are equal");
        y=true;
       }
        else
       {
        System.err.println("arent equal");
        y=false;
       }
     return y;
    }

如果你可以帮助我,我会很高兴因为我认为我已经解决了但我不确定。谢谢,我想提到这是我第一次来这里我不知道是因为是“begginer “你没有帮助。

1 个答案:

答案 0 :(得分:1)

我想说,你太接近了 现在,它已经修好了!

public static boolean equal(int cars,int bikes{
if(car==bikes)
//since you are comparing two ints, you don't require any !
{
System.out.println("are equal");
y=true;
}
else
{
System.err.println("arent equal");
/*Preferably use System.out.println().Having an inequality as this cannot be 
attributed to error. It's okay if you stick to your view.There is no hard and fast rule either !*/ 
y=false;
}
return y;
}