2017-03-31 8 views
0

私は今このプロジェクトに取り組んできましたが、何らかの理由でJavaを握るのに苦労しています。Javaをコンパイルするのに助けが必要です

目的は3つのフルーツをそれぞれ含む3つのオブジェクトを作成し、各フルーツは独自の価格/価値を持っています。

現在、値を追加するのに問題があります。私は今までJavaに多くの問題を抱えていると言っていたので、はるかに間違っていると確信しています。

私の最大の問題は、現在costofBox()メソッドです。

私はこれを1週間以上にわたって作業してきました。ここで

は、全体のプログラムです:

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"); 
    } 
} 

は私を助けることができるあなたの人々のために前もってありがとうございます!

+0

あなたは 'Map 'を使って各項目の価格を表すのがよいでしょう。 –

+0

'for'loopと' while'loopを見てください – jhamon

+0

'Bundle1'のような変数は、常に小文字で始める必要があります。これを強制するものはありませんが、コードを理解しやすくするために広く使用されています。同様に、クラスは大文字で始まる必要があります。 – Michael

答えて

3

あなたの問題は、このコンストラクタである:

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; 
} 

その後、プロジェクトが正常に実行されます。


これ以外にも、プログラムの長さを短縮し、より簡潔にし、自分自身を少なくする方法はたくさんあります。 StackOverflowの姉妹サイトであるCode Reviewにお越しいただいたら、改善のための提案をお寄せします。あなたがそれをすることを決めるなら、私はこの答えにコメントをつけてください!

+0

コンストラクターはすべて間違っていて、彼は既に彼の文字列バンドルを作成しています。彼は***バリエーションを渡す必要があります。これは*** thisBundle1 = fruit1 *** –

+0

平均?あなたは名前の衝突がないので、「これ」は必要ありません。また、私は自分の提案された変更でコンパイルして実行し、正しく機能します。 – Michael

+0

これは本当ですが、this.bundleを使用すると正しいこともあります。私はまだそれが正しい解決策をupvoted –

関連する問題