0

我正在做一个练习,我需要创建一个具有 2 个构造函数的子类(1 为空并将值设置为 0,另一个正在获取值。在子类中我有一个 equals 方法和一个 betterfat 方法检查 2 个对象并告诉哪个对象更好。

我的问题在主类中(我必须从用户那里获得 3 个巧克力的 3 个值,检查数字并选择要使用的构造函数,但我的问题是当我放入 if 语句并在其中选择构造函数之后这些块我必须对这些对象使用一种方法我的问题是它说找不到变量(似乎是因为 ifstatment)。

代码是(子类脂肪)

public class Fats{

    private int _amount;
    private double _saturated;
    private double _unsaturated;
    public Fats(){//constractor thats sets all the values as 0
        this._amount=0;
        this._saturated=0;
        this._unsaturated=0;
    }

    /*constractor that build an object , if one of the
     *values not between 0-100 it sets all the values to 0.
     **/
    public Fats(int amount, double sat, double unsat){
        if(amount<0&&amount>100){//using if to check if the value is between 0-100
            this._amount=0;
            this._saturated=0;
            this._unsaturated=0;}
        else
            this._amount=amount;
        if(sat<0&&sat>100){
            this._amount=0;
            this._saturated=0;
            this._unsaturated=0;}
        else
            this._saturated=sat;
        if(unsat<0&&unsat>100){
            this._amount=0;
            this._saturated=0;
            this._unsaturated=0;}
        else
            this._unsaturated=unsat;}
    //getters
    public int getAmount(){
        return _amount;
    }

    public double getSaturated(){
        return _saturated;
    }

    public double getUnsaturated(){
        return _unsaturated;
    }
    //setters
    public void setAmount(int amount){
        if(amount<0&&amount>100){
            this._amount=(this._amount+0);}
        else
            this._amount=amount;}

    public void setSaturated(double sat){
        if(sat<00&&sat>100){
            this._saturated=(this._saturated+0);
        }
        else
            this._saturated=sat;
    }

    public void setUnsaturated(double unsat){
        if(unsat<00&&unsat>100){
            this._unsaturated=(this._unsaturated+0);
        }
        else
            this._unsaturated=unsat;
    }
    //equal method 
    public boolean equals (Fats other){
        if(other==this)//checking if other is me
            return true;
        if(!(other instanceof Fats))//if the other not a Fats
            return false;
        Fats otherFats = (Fats)other;//casting other to Fats object to check if the values are equals
        if(this._amount==otherFats._amount&&this._saturated==otherFats._saturated&&this._unsaturated==otherFats._unsaturated)
        //checking each calue of fats if its equals to other values
            return true;
        else
            return false;
    }

    public boolean betterFat (Fats other){
        Fats otherFats = (Fats)other;//casting the other object to be an Fats class object
        if(this._amount<otherFats._amount){
            return true;}
        if(this._amount==otherFats._amount&&this._saturated<otherFats._saturated){
            return true;}
            else
            if(this._amount==otherFats._amount&&this._saturated==otherFats._saturated&&this._saturated<otherFats._saturated)
                return true;
                else
        
            return false;
        
    }

    public String toString(){
        return "in 100 gr: "+"\n"+" "+" "+"Fat: -"+" "+this._amount+"\n"+" "+"Saturated Fat: –&quot;+"  "+this._saturated+"\n"+" "+"Unsaturated Fat:-"+"  "+this._unsaturated;
    }

        
        
        
}

这是主类(在主类中,我必须获取 3 个值并选择要使用的构造函数,我必须这样做 3 次,然后使用方法查看哪个更好,并使用 toString 将其显示给用户)

import java.util.Scanner;
public class FatDriver{
    public static void main(String[]args){
        Fats num1,num2,num3;
        Fats F1,F2,F3;//,j1,j2,j3;//,darkChocolateFat,milkChocolateFat,whiteChocolateFat;
        int x1,x2,x3,e1;//darkChocolateFat,milkChocolateFat,whiteChocolateFat;
        double y1,y2,y3,e2,e3;//darkChocolatesat,milkChocolatesat,whiteChocolatesat;
        double z1,z2,z3; //darkChocolateunsat,milkChocolateunsat,whiteChocolateunsat;
        Scanner scan= new Scanner(System.in);//using scaner for user input
        System.out.println("Please enter the values of Fat, Saturated Fat and Unsaturated Fat of DarkChocolate Fat");
         Fats whiteChocolate;
           Fats bitterChocolate;
        Fats milkChocolate; 
        x1=scan.nextInt();//darkChocolateFat
        y1=scan.nextDouble();//darkChocolatesat
        z1=scan.nextDouble();//darkChocolateunsat
        double r2,r3;
        
        int r1;
        boolean yes,no;
        //yes=true;
        //no=false;
        final double upRange =100;
        final double lowRange =0;
       // z1=scan.nextDouble();//darkChocolateunsat
        
        if(x1>lowRange||x1<=upRange||y1>lowRange||y1<=upRange||z1>lowRange||z1<=upRange){
             bitterChocolate = new Fats();
             bitterChocolate = new Fats(x1,y1,z1);
            System.out.println("one or more of the value you have entered is invalid");
        }else{ bitterChocolate = new Fats();};
System.out.println("Please enter the values of Fat, Saturated Fat and Unsaturated Fat of milkChocolate Fat");
        x2=scan.nextInt();//darkChocolateFat
        y2=scan.nextDouble();//darkChocolatesat
        z2=scan.nextDouble();
 if(x2<0||x2>100||y2<0||y2>100||z2<0||z2>100){
             milkChocolate = new Fats();
            System.out.println("one or more of the value you have entered is invalid222");
        };
System.out.println("Please enter the values of Fat, Saturated Fat and Unsaturated Fat of whiteChocolate Fat");
        x3=scan.nextInt();//darkChocolateFat
        y3=scan.nextDouble();//darkChocolatesat
        z3=scan.nextDouble();
if(x3<0||x3>100||y3<0||y3>100||z3<0||z3>100){
             whiteChocolate = new Fats();
            System.out.println("one or more of the value you have entered is invalid222");
        };
System.out.println(whiteChocolate);// this is just to check if it is works (i have tochoose the best of 3 using my method)
            System.out.println(bitterChocolate);
            System.out.println(milkChocolate);

我试过只是 toString 刚刚制作的对象,但它说:

“变量可能尚未初始化”

因此,在获得用户输入后我无法使用这些对象我可能会错过一些东西,这就是我寻求帮助的原因

4

0 回答 0