2017-03-16 7 views
0

これで、オプション付きのバックパックオブジェクトを作成するメソッドを持つこのJavaオブジェクトクラスがあります。私はすべてのメソッドが動作しているが、情報を表示する最終的なメソッドが正しく動作していません。入力されているパラメータを受け入れていないようです。 オブジェクトクラスとランナークラスの両方が含まれています。Javaランナークラスは変数を受け入れません

/* Variables and parameters 
* Intro "Welcome to BackPackMaker, the ultimate backpack making experience. NOW LET'S MAKE SOME BACKPACKS!!" 
* Color(red,indigo,yellow,green,purple,orange,black,white) 
* No. of straps(1,2) 
* Size (small,medium,large,gigantic) 
* Pouches(1,2,3,4,5) 
* Water Bottle Slot(True,False) 
* Do you like Spongebob(True,False) if yes, "Hello SpongeBob, my name is PatBack!" 
* 
* Use or statements in the if loop to keep things simple 
* The if loop consists of checking to see whether one of the variables was used. 
* 
* All methods must be commented 
*/ 
import java.util.Scanner; 
public class Backpack { 
//Setting up the private variables 
private String color; //color of the backpack 
private String cchoice; 
private int straps; //amount of straps(some backpacks only have one) 
private int strapsans; 
private String size; //how big it is 
private String tsize; //how big it is 
private int pouches; //how many pouches there are 
private int tpouch; //how big it is 
private boolean slot; //if there is a water bottle slot on the side 
private boolean waterslot; 
private static int cost; //The cost based off of number of pouches and straps. 

public Backpack(){ 
color = "red"; //color of the backpack 
cchoice = "red"; 
straps = 2; //amount of straps(some backpacks only have one) 
strapsans = 2; 
size = "big"; //how big it is 
tsize = "big"; 
pouches = 2; //how many pouches there are 
tpouch = 2; 
slot = true; //if there is a water bottle slot on the side 
waterslot = true; 
cost = 100; //The cost based off of number of pouches and straps. 
} 


public String pickc(){ 
    String color = "blank"; 
    Scanner input = new Scanner(System.in); 
    System.out.println("Please choose a color."); 
    System.out.println("You can have red, blue, yellow, green, purple, or orange"); 
    color = input.nextLine(); 
    String cchoice = color; 
    if(cchoice.equals("red") || cchoice.equals("blue") || cchoice.equals("yellow") || cchoice.equals("green") || cchoice.equals("purple") || cchoice.equals("orange")) 
    { 
     return cchoice; 
    } 
    else 
    { 
     System.out.println("please enter a valid choice"); 
     return pickc(); 
    } 
    } 
public String getcolor(){ 
    return cchoice; 
} 

public String picks(){ 
    String size = "blank"; 
    Scanner input5 = new Scanner(System.in); 
    System.out.println("What size do you want? Available sizes are small, medium, and large"); 
    size = input5.nextLine(); 
    String tsize = size; 
    if(tsize.equals("small") || tsize.equals("medium") || tsize.equals("large")){ 
     return tsize; 
    } 
    else 
    { 
     System.out.println("please enter a valid choice"); 
     return picks(); 
    } 

} 

public String getsize(){ 

    return tsize; 
} 
public int pouchnum(){ 
    pouches = 0; 
    Scanner input2 = new Scanner(System.in); 
    System.out.println("How many pouches do you want?"); 
    pouches = input2.nextInt(); 
    int tpouch = pouches; 
    if(tpouch == 1 || tpouch == 2 || tpouch == 3 || tpouch == 4 || tpouch == 5){ 
     System.out.println(tpouch); 
     return tpouch; 
    } 
    else 
    { 
     System.out.println("Enter a valid number between 0 and 5"); 
     return pouchnum(); 
    } 
} 
public int getpouchnum(){ 
    return tpouch; 
} 

public boolean slotyes(){ 
    boolean slots = true; 
    Scanner input4 = new Scanner(System.in); 
    System.out.println("Do you want a water bottle space? Enter 1 for yes or anything else for no"); 
    int answer = input4.nextInt(); 
    boolean waterslot = slot; 
    if(answer == 1){ 
     slot = true; 
     return slot; 

    } 
    else if (answer == 2){ 
     slot = false; 
     return slot; 
    } 
    return false; 
} 

public boolean getslot(){ 
    return waterslot; 
} 

public int straps(){ 
    straps = 0; 
    Scanner input3 = new Scanner(System.in); 
    System.out.println("How many straps do you want? You can have up to 2"); 
    straps = input3.nextInt(); 
    int strapsans = straps; 
    if(strapsans == 1 || strapsans == 2){ 
     System.out.println(straps); 
     return strapsans; 
    } 
    else 
    { 
     System.out.println("1 or 2 straps only"); 
     return straps(); 
    } 


} 
public int getstraps(){ 
    return strapsans; 
} 

public void displayinfo(){ //Displays the various values of the backpack. 

    System.out.println("Your backpack is a " + tsize + ", " + cchoice + " backpack with " + tpouch + "pouch(s) and " + strapsans + "strap(s)."); 
} 
} 

import java.util.Scanner; 
public class BackPackMaker { 

    public static void main(String[] args) 
{ 
     System.out.println("Welcome to BackPack Maker, prepare for the ultimate midterm experience"); 
     System.out.println("NOW LET'S MAKE SOME BACKPACKS!"); 
     Backpack B1 = new Backpack(); 
     B1.pickc(); 
     B1.getcolor(); 
     B1.picks(); 
     B1.getsize(); 
     B1.pouchnum(); 
     B1.getpouchnum(); 
     B1.straps(); 
     B1.getstraps(); 
     System.out.println(B1.slotyes()); 
     B1.getslot(); 
     B1.displayinfo(); 


} 
} 
+0

最小限の完全な例を提供する方法 –

+0

期待される出力と現在得ているものを提供できるかどうかを[こちら](https://stackoverflow.com/help/mcve)でお読みください。 –

+0

私の予想される出力は、入力が何かを返す何かを得ることです。 I.あなたのバックパックは青です。代わりに私はいつも得ます:あなたのバックパックは2ポーチと2ストラップの大きい、赤いバックパックです。 – TPG

答えて

0

バックパックオブジェクトの変数に入力値を割り当てる必要があります。 メソッドの入力を渡した後 - > this.pouches = pouches;

if(tpouch == 1 || tpouch == 2 || tpouch == 3 || tpouch == 4 || tpouch == 5){ 
      System.out.println(tpouch); 
      this.pouches = tpouch; 
      return tpouch; 
     } 

Backpackクラスのオブジェクトを作成して元の値を変更していないため、毎回同じ値が返されます。

また、適切な値を表示する必要がありますが、私は本当にそれが使用されていない場合、なぜ "パウチ"変数について気にしないのか分かりません。可能な限りシンプルに保つようにしてください。使用しないものがあれば、これを取り除いてください。

+0

さて、それだけです。ありがとうございました。私は本当にこれについて強調しましたが、あなたは私を助けました。 – TPG

0

入力を受け取り、それをインスタンス変数に設定しないだけです。たとえば、バッグのサイズをString tsize = size;にすると、インスタンス変数ではなくローカル変数tsizeにサイズが設定されます。ちょうどthis.tsize=sizeでなければなりません。また、他の方法でも。

関連する問題