所以我一直在研究這個項目一段時間,並且由於某種原因我抓住了很多麻煩。需要幫助編譯Java(訪問器,變異函數,構造函數)
目標是創建3個包含3個水果的對象,每個水果都有自己的價格/價值。
我在加入當前的值時遇到了問題,我敢肯定還有更多的錯誤,因爲我說到目前爲止我遇到了很多麻煩。
我最大的問題是目前的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");
}
}
謝謝你的時間提前,對於那些你誰可以幫我了!
看起來你最好用'Map'來表示每件商品的價格。 –
你應該看看'for'loop和'while'loop – jhamon
變量,比如'Bundle1',應該總是以小寫字母開頭。沒有任何東西可以實現這一點,但它是一個廣泛使用的約定,它會使您的代碼更易於理解。同樣,類應以大寫字母開頭。 – Michael