Java runner类不会接受变量

时间:2017-03-16 18:59:36

标签: java object input

好的,所以我有这个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();


}
}

2 个答案:

答案 0 :(得分:0)

您需要将输入值分配给背包对象的变量。 在方法中传递输入后 - > this.pouches =小袋;

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

每次都会收到相同的值,因为您已经创建了Backpack类的对象,并且从未更改其原始值。

此外,您需要显示正确的值,我真的不知道为什么您甚至关心" pouches"变量如果没有用的话。尽量保持简单 - 如果有什么东西你不能使用 - 摆脱它。

答案 1 :(得分:0)

您只是接受输入而不是将其设置为实例变量。例如,在获取行包大小String tsize = size;时,这会将大小设置为局部变量tsize而不是实例变量。它应该只是this.tsize=size。还有其他方法。