等于不在java中工作的方法。硬件

等于不在java中工作的方法。硬件,java,methods,equals,Java,Methods,Equals,我在代码中有两个equals方法,但它们都不起作用。我自己做了一个,然后我还通过源代码从Eclipse自动生成了一个。我已经用一个或另一个运行了多次程序,但都没有工作 import java.util.Arrays; import java.util.Scanner; import java.math.*; public class Item { static //the properties of an Item double cash=59; s

我在代码中有两个equals方法,但它们都不起作用。我自己做了一个,然后我还通过源代码从Eclipse自动生成了一个。我已经用一个或另一个运行了多次程序,但都没有工作

import java.util.Arrays;
    import java.util.Scanner;
    import java.math.*;
public class Item {
     static //the properties of an Item
     double cash=59;
     static double sum=0;
    private int priority;
    private String name;
    private double price;




    //default constructer
    public Item() {
        priority = -1;   //fill with default values
        price = 0.0;
        name = "No name yet";
    }


    public Item(int priority, String name, double price) {//constructor with all 3 arguments 
        this.priority = priority; 
        this.name = name;         
        this.price = price;
    }


    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        //priority must be between 1 and 7
        if (priority > 0 && priority <= 7) {

            this.priority = priority;
        } else {
            //otherwise default to 0
            System.err.println("Error, enter 1 through 7"); 
            //this.priority = 0;
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        //would I put equals method here. name.equals.name?

        this.name = name;
    }

    public double getPrice() {
        return price;

    }

    public void setPrice(double price) {

        //price between 0 and 100 inclusive
        if (price >= 0.00) {
            if (price <= 100.00) {
                this.price = price;
                cash = cash-=price;
                sum=sum+=price;

            } else {

                System.err.println("Error: price to high");
            }
        } else {
            System.err.println("Error: price to low");
        }
    }


    public boolean equals(Item otherItem){
        if(this.getPriority()==otherItem.getPriority());
            System.err.println("Error, Same Priorities detected");
            return true;





     }






    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + priority;
        return result;
    }


    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (!(obj instanceof Item)) {
            return false;
        }
        Item other = (Item) obj;
        if (priority != other.priority) {
            return false;
        }
        System.err.println("Error, Same Priorities detected");
        return true;
    }


    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("Item [Price=").append(getPrice()).append(", ");
        if (getName() != null) {
            builder.append("Name=").append(getName()).append(", ");
        }
        builder.append("Priority=").append(getPriority()).append("]");
        return builder.toString();
    }





    public static void main(String[] args) {

         Item[] list = new Item[2];
        Scanner keyboard = new Scanner(System.in);

        for (int i = 1; i <= list.length; i++) {

            if(cash==59)
            {
                System.out.println("You have 59 dollars");
            }


            Item anItem = new Item(); // new item object created 7 times

            System.out.println("Enter an item you want to add to your list " + i);
            anItem.setName(keyboard.next());

            System.out.println("Enter a price " + i);
            anItem.setPrice(keyboard.nextDouble());

            System.out.println("Enter the priority of the item " + i);
            anItem.setPriority(keyboard.nextInt());


            list[i-1] = anItem;
            System.out.println(Arrays.toString(list));
            System.out.println("Cash left "+cash);
            System.out.println("Sum of Items "+sum); 





      if(sum>59)
     {System.out.println("Error, you ran out of money");

     System.out.println(Arrays.toString(list));}

        }

    System.out.println( list[1].getPriority());


    }




    }
导入java.util.array;
导入java.util.Scanner;
导入java.math.*;
公共类项目{
静态//项的属性
双倍现金=59;
静态双和=0;
私人优先权;
私有字符串名称;
私人双价;
//默认构造函数
公共项目(){
优先级=-1;//用默认值填充
价格=0.0;
name=“还没有名字”;
}
公共项(int优先级、字符串名称、双倍价格){//具有所有3个参数的构造函数
优先权=优先权;
this.name=名称;
这个价格=价格;
}
public int getPriority(){
返回优先级;
}
公共无效设置优先级(整数优先级){
//优先级必须介于1和7之间
如果(优先级>0&&priority=0.00){

如果(price这个方法在这里完全刺痛了你的心**

public boolean equals(Item otherItem){
    if(this.getPriority()==otherItem.getPriority());
        System.err.println("Error, Same Priorities detected");
        return true;
}
首先,在
if
的条件之后有一个
,因此它没有做任何事情,因此您的方法总是返回
true

第二,这个方法重载了你应该重写和使用的
equals(Object)
方法


重写方法时,请使用
@Override
对它们进行注释。IDE将检查并验证您是否确实试图重写某个方法。

此方法完全是在一个**

public boolean equals(Item otherItem){
    if(this.getPriority()==otherItem.getPriority());
        System.err.println("Error, Same Priorities detected");
        return true;
}
首先,在
if
的条件之后有一个
,因此它没有做任何事情,因此您的方法总是返回
true

第二,这个方法重载了你应该重写和使用的
equals(Object)
方法


重写方法时,请使用
@Override
对它们进行注释。IDE将执行检查并验证您是否确实试图重写方法。

删除
equals()
您添加的方法。始终为您的重写方法使用
@Override
注释。您的equals方法错误,不是真正的方法重写,如果您使用注释,编译器将告诉您。请注意,您使用的方法参数不正确。您在哪里调用
equals()
方法?我觉得你没有使用它们。删除
equals()
您添加的方法。始终为您的重写方法使用
@Override
注释。您的equals方法错误,不是真正的方法重写,如果您使用注释,编译器将告诉您。请注意,您使用的方法参数不正确。您在哪里调用
equals()
methods?我觉得你没有使用它们。sry,typo。我在删除内容时意外地离开了;@user2803251在这种情况下,只有
System.err.println()
将是
if
块的一部分。它仍将始终返回
true
.sry,typo.I正在删除内容并意外离开@user2803251在这种情况下,只有
系统.err.println()
将是
if
块的一部分。它仍将始终返回
true