2017-03-16 89 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

請閱讀[這裏](https://stackoverflow.com/help/mcve)如何提供一個最小的完整示例 –

+0

您能否提供預期的輸出和您目前正在獲取的內容? –

+0

我的預期輸出是我會得到返回輸入內容的東西。 I.E你的揹包是藍色的。相反,我總是得到:你的揹包是一個大的,紅色的揹包,有2pouch(s)和2strap(s)。 – 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。還有其他方法。