需要编译Java的帮助(访问器、变异器、构造函数)

需要编译Java的帮助(访问器、变异器、构造函数),java,constructor,tostring,accessor,mutators,Java,Constructor,Tostring,Accessor,Mutators,因此,我已经在这个项目上工作了一段时间,由于某种原因,我在掌握Java方面遇到了很多困难 目标是创建3个对象,每个对象包含3个水果,每个水果都有自己的价格/价值 目前,我在计算这些值时遇到了困难,我确信还有更多的错误,正如我所说的,到目前为止,我在Java方面遇到了很多问题 我目前最大的问题是costofBox()方法 任何有帮助的建议都将不胜感激,我已经为此工作了一个多星期了 以下是整个计划: public class Project8 { private String fruit1; pr

因此,我已经在这个项目上工作了一段时间,由于某种原因,我在掌握Java方面遇到了很多困难

目标是创建3个对象,每个对象包含3个水果,每个水果都有自己的价格/价值

目前,我在计算这些值时遇到了困难,我确信还有更多的错误,正如我所说的,到目前为止,我在Java方面遇到了很多问题

我目前最大的问题是
costofBox()
方法

任何有帮助的建议都将不胜感激,我已经为此工作了一个多星期了

以下是整个计划:

public class Project8
{

private String fruit1;
private String fruit2;
private String fruit3;
private String Bundle1;
private String Bundle2;
private String Bundle3;
private int costofBox;
double total;
int broccoli;
int tomato;
int kiwi;
int kale;
int orange;

public String toString()
{
    String output = "The box contains: " + Bundle1 + ", " + Bundle2 + ", " + Bundle3 +
    "and the cost is $" + costofBox();
    return output;
}

public String getBundle1()
{
    return Bundle1;
}
public String getBundle2()
{
    return Bundle2;
}
public String getBundle3()
{
    return Bundle3;
}


public void setBundle1(String Bundle1) 
{
    Bundle1=fruit1;
}
public void setBundle2(String Bundle2) 
{
    Bundle2=fruit2;
}
public void setBundle3(String Bundle3) 
{
    Bundle3=fruit3;
}

public double costofBox()
{
    double total=0;
    if(Bundle1.equals("broccoli"))
        total+=6;
    else if(Bundle1.equals("tomato"))
        total+=5;
    else if(Bundle1.equals("kiwi"))
        total+=8;
    else if(Bundle1.equals("kale"))
        total+=4;
    else if(Bundle1.equals("orange"))
        total+=7;
    if(Bundle2.equals("broccoli"))
        total+=6;
    else if(Bundle2.equals("tomato"))
        total+=5;
    else if(Bundle2.equals("kiwi"))
        total+=8;
    else if(Bundle2.equals("kale"))
        total+=4;
    else if(Bundle2.equals("orange"))
        total+=7;
    if(Bundle3.equals("broccoli"))
        total+=6;
    else if(Bundle3.equals("tomato"))
        total+=5;
    else if(Bundle3.equals("kiwi"))
        total+=8;
    else if(Bundle3.equals("kale"))
        total+=4;
    else if(Bundle3.equals("orange"))
        total+=7;

    return total;
}

public Project8()
{    
    fruit1 = "broccoli" + "kale" + "orange";
    fruit2 = "kale" + "kiwi" + "orange";
    fruit3 = "broccoli" + "tomato" + "kiwi";
}

public Project8(String fruit1, String fruit2, String fruit3)
{
    String Bundle1=fruit1;
    String Bundle2=fruit2;
    String Bundle3=fruit3;
}

public static void main (String [] args)
{
    Project8 Bundle1=new Project8 ("broccoli", "kale", "orange");
    Project8 Bundle2=new Project8 ("kale", "kiwi", "orange");
    Project8 Bundle3=new Project8 ("broccoli", "tomato", "kiwi");



    System.out.println("Week 1: " + Bundle1.toString());
    System.out.println("Week 2: " + Bundle2.toString());
    System.out.println("Week 3: " + Bundle3.toString());
    System.out.println("Week4: The box contains:,, and the cost is $0.0");
    }
}

提前感谢你们中能帮助我的人

您的问题在于此构造函数:

public Project8(String fruit1, String fruit2, String fruit3)
{
    String Bundle1=fruit1;
    String Bundle2=fruit2;
    String Bundle3=fruit3;
}
由于这些赋值前面的类型字符串,您正在声明新的局部变量!这意味着类的字段:

private String Bundle1;
private String Bundle2;
private String Bundle3;
。。。没有给定值。当您尝试访问它们时,您会看到异常,因为它们为空

如果将构造函数更改为:

public Project8(String fruit1, String fruit2, String fruit3)
{
    Bundle1 = fruit1;
    Bundle2 = fruit2;
    Bundle3 = fruit3;
}
然后您的项目将正常执行



顺便说一句,有很多方法可以缩短程序的长度,使事情更简洁,减少重复。我建议,一旦你成功了,你就去StackOverflow的姐妹网站:他们会给你改进的建议。如果你决定这么做,请给我留言

您的问题在于此构造函数:

public Project8(String fruit1, String fruit2, String fruit3)
{
    String Bundle1=fruit1;
    String Bundle2=fruit2;
    String Bundle3=fruit3;
}
由于这些赋值前面的类型字符串,您正在声明新的局部变量!这意味着类的字段:

private String Bundle1;
private String Bundle2;
private String Bundle3;
。。。没有给定值。当您尝试访问它们时,您会看到异常,因为它们为空

如果将构造函数更改为:

public Project8(String fruit1, String fruit2, String fruit3)
{
    Bundle1 = fruit1;
    Bundle2 = fruit2;
    Bundle3 = fruit3;
}
然后您的项目将正常执行




顺便说一句,有很多方法可以缩短程序的长度,使事情更简洁,减少重复。我建议,一旦你成功了,你就去StackOverflow的姐妹网站:他们会给你改进的建议。如果你决定这么做,请给我留言

看起来您最好使用
映射来表示每种商品的价格。您应该查看
中的
循环和
,而
循环变量,如
Bundle1
,应始终以小写字符开头。没有什么可以强制执行这一点,但它是一种广泛使用的约定,可以使您的代码更容易理解。类似地,类应该以大写字母开头。您没有正确引用值,而不是将值存储在变量中,而是将值存储在新的字符串值中,这就是为什么您无法访问在toString()中传递给对象的值methodGo查看@Michael的答案看起来您最好使用
地图来表示每种商品的价格。您应该查看
for
循环和
,而
循环变量,如
Bundle1
,应始终以小写字符开头。没有什么可以强制执行这一点,但它是一种广泛使用的约定,可以使您的代码更容易理解。类似地,类应该以大写字母开头。您没有正确引用值,而不是将值存储在变量中,而是将值存储在新的字符串值中,这就是为什么您无法访问在toString()中传递给对象的值methodGo看看@Michael的answerConstructor都错了,他已经创建了他的字符串包,他需要做的是用这个来传递变量。Bundle1=fruit1你是什么意思?您不需要
这个。
因为没有名称冲突。此外,我已经用我建议的更改编译并运行了它,它运行正常。是的,但如果您使用this.bundle,它也是正确的。我仍然投了赞成票,因为这是正确的解决方案。非常感谢!你不知道我读了这么多不同书籍的章节,看了这么多视频,我很高兴它能运行,尽管我知道它很草率,但我使用我做的对象来学习。@distro没问题。很乐意提供帮助。构造函数完全错了,他已经创建了他的字符串包,他需要做的是使用此函数将变量传递给它。Bundle1=fruit1您的意思是什么?您不需要
这个。
因为没有名称冲突。此外,我已经用我建议的更改编译并运行了它,它运行正常。是的,但如果您使用this.bundle,它也是正确的。我仍然投了赞成票,因为这是正确的解决方案。非常感谢!你不知道我读了这么多不同书籍的章节,看了这么多视频,我很高兴它能运行,尽管我知道它很草率,但我使用我做的对象来学习。@distro没问题。很乐意帮忙。