我从事这个项目已经有一段时间了,但由于某种原因,我在掌握 Java 方面遇到了很多困难。
目标是创建 3 个对象,每个对象包含 3 个水果,每个水果都有自己的价格/值(value)。
我目前在添加这些值时遇到了麻烦,我确信还有更多错误,正如我所说的,到目前为止,我在 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;
}
由于这些赋值前面的 String 类型,您正在声明新的局部变量!这意味着您的类的字段:
private String Bundle1;
private String Bundle2;
private String Bundle3;
...从未被赋予值。当您尝试访问它们时,您会收到所看到的异常,因为它们是 NULL。
如果将构造函数更改为:
public Project8(String fruit1, String fruit2, String fruit3)
{
Bundle1 = fruit1;
Bundle2 = fruit2;
Bundle3 = fruit3;
}
那么你的项目就会正确执行。
<小时/>顺便说一句,有很多方法可以减少程序的长度,使事情更加简洁,并减少重复。我建议一旦你成功了,你就可以访问 Code Review这是 StackOverflow 的姐妹网站:他们会给你改进的建议。如果您确实决定这样做,请对此答案发表评论!
关于java - 需要编译 Java 的帮助(访问器、修改器、构造器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43144256/