2017-03-16 62 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。还有其他方法。