m0_62667564 2022-10-29 13:07 采纳率: 84.8%
浏览 43
已结题

用equals 方法比较两个数字的大小

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int input1 = scanner.nextInt();
int input2 = scanner.nextInt();
SimpleInteger n1 = new SimpleInteger(input1);
SimpleInteger n2 = new SimpleInteger(input2);
System.out.println("n1's value is "+n1.getValue());
System.out.println("n1 is even? " + n1.isEven());
System.out.println("n1 is odd? " + n1.isOdd());
System.out.println("n1 is prime? " + n1.isPrime());
System.out.println("n1 equals n2? "+n1.equals(n2));
}
}

class SimpleInteger{
private int value;
public int getValue(){
return value;
}
public SimpleInteger(int value){
this.value = value;
}
public boolean isPrime(){
int i;
if(value==1){
return false;
}
else {
for (i = 2; i < value; i++) {
if (value % i == 0)
return false;
}
}
if(i==value){
return true;
}
else{
return false;
}
}
public boolean isEven(){
if(value%2==0)
{
return true;
}
else
{
return false;
}
}
public boolean isOdd(){
if(value%2==0)
{
return false;
}
else
{
return true;
}
}
/*public boolean equals(SimpleInteger n2){

}*/

}
如果equals方法这么写:
public boolean equals(int n2){
if(value == n2){
return true;
}
else{
return false;
}
}
当输入两个相同的数字时也会输出false, 在不改变主函数的情况下应该怎么改,才能正确输出,麻烦帮忙修改一下

  • 写回答

4条回答 默认 最新

  • a5156520 2022-10-29 13:39
    关注

    把equals方法的参数变为SimpleInteger类型即可,因为如果是int类型,就自动调用了Object类的equals方法了, 可以在原来代码的equals加一个打印语句,测试其是否被调用就知道了,修改如下:

    参考链接:
    java小笔记(重写equals)_CHSN的博客-CSDN博客_java重写equals方法

    
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            
            Scanner scanner = new Scanner(System.in);
            int input1 = scanner.nextInt();
            int input2 = scanner.nextInt();
            
            SimpleInteger n1 = new SimpleInteger(input1);
            SimpleInteger n2 = new SimpleInteger(input2);
            
            System.out.println("n1's value is "+n1.getValue());
            System.out.println("n1 is even? " + n1.isEven());
            System.out.println("n1 is odd? " + n1.isOdd());
            System.out.println("n1 is prime? " + n1.isPrime());
            System.out.println("n1 equals n2? "+n1.equals(n2));
        }
    }
    
    class SimpleInteger{
        
        private int value;
        public int getValue(){
            return value;
        }
        public SimpleInteger(int value){
            this.value = value;
        }
        
        public boolean isPrime(){
            int i;
            if(value==1){
                return false;
            }
            else {
                for (i = 2; i < value; i++) {
                    if (value % i == 0)
                        return false;
                    }
            }
            
            if(i==value){
                return true;
            }
            else{
                return false;
            }
        }
        
        public boolean isEven(){
            if(value%2==0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        
        public boolean isOdd(){
            if(value%2==0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        /*public boolean equals(SimpleInteger n2){
        
        }*/
    
    
        public boolean equals(SimpleInteger n2){
            //System.out.println("equals is run!");
            if(value == n2.getValue()){
                return true;
            }
            else{
                return false;
            }
        }
        
    }
    
    
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 11月6日
  • 已采纳回答 10月29日
  • 创建了问题 10月29日

悬赏问题

  • ¥20 关于CMOS电路图的提问
  • ¥15 Verilog hdl密码锁设计
  • ¥35 基于python的有ssl加密传输的socket聊天室
  • ¥15 数码管亮度控制器设计
  • ¥15 kafka客户端跨网段访问,看日志提示连接的还剩内网地址,且访问不通
  • ¥15 关于c语言代码的问题
  • ¥15 c51单片机控制步进电机
  • ¥20 Visual studio无法检测到设备
  • ¥30 vue 页面窗口放大或者缩小元素会变化
  • ¥15 questasim仿真报错