Java - 更改数据字段?

Java - changing data fields?

有两件事我需要帮助,但我很难搞清楚。它们在下面列出 - 非常感谢任何帮助。

1) 我需要添加一个方法,提示用户输入一个整数 (1-3) 来更改音量(默认设置为 2)。我真的很难弄清楚如何进行这项工作。

2) 我需要添加一个类似的方法,提示用户输入一个整数(1 或 2)来插入耳机。

这是我的代码;一个是测试 class:

//Open Package
package headphone_wk6;

//Import scanner since this will require user input
import java.util.Scanner;


// Creat class for headphones
public class HeadPhone_WK6 {

//Call scanner
Scanner stdin = new Scanner(System.in);

//Initialize input variable
int Input;

//Declare constant Variables
public static final int LOW = 1;
public static final int MEDIUM = 2;
public static final int HIGH = 3;

//Declare Private variables
private int volume;
private boolean pluggedIn;
private String manufacturer;
private String headPhoneColor;

//Declare Strings
String pluggedInStat;
String volumeNow;


// Constructor for class
public HeadPhone_WK6(int volume, boolean pluggedIn, String manufacturer, String headPhoneColor) {
    this.volume = volume;
    this.pluggedIn = pluggedIn;
    this.manufacturer = manufacturer;
    this.headPhoneColor = headPhoneColor;
}

// Default Constructor for default settings
public HeadPhone_WK6() {
    volume = MEDIUM;
    pluggedIn = false;
    manufacturer = "";
    headPhoneColor = "";
}

// setVolume
public void setVolume(int volume) {
    this.volume = volume;
}

// getVolume
public int getVolume() {
    if (volume == 1) {
        volumeNow = "LOW";
    }
    else if (volume == 2) {
        volumeNow = "MEDIUM";
    }
    else {
        volumeNow = "HIGH";
    }
    return volume;
}


// setPluggedIn
public void setPluggedIn(boolean pluggedIn) {
    this.pluggedIn = pluggedIn;   
}

// getPluggedIn
public boolean getPluggedIn() {
    if(pluggedIn == true) {
        pluggedInStat = "Plugged In";
    }
    else {
        pluggedInStat = "Not Plugged In";
    }

    return pluggedIn;
}


// setManufacturer 
public void setManufacturer(String manufacturer) {
    this.manufacturer = manufacturer;
}

// getManufacturer
public String getManufacturer() {
    return manufacturer;
}

// setHeadPhoneColor
public void setHeadPhoneColor(String headPhoneColor) {
    this.headPhoneColor = headPhoneColor;
}

// getHeadPhoneColor
public String getHeadPhoneColor() {
    return headPhoneColor;
}

// method to create string
public String toString() {

    boolean phonesPluggedIn = this.getPluggedIn();
    String phoneManufacturer = this.getManufacturer();
    String phoneColor = this.getHeadPhoneColor();
    String currentVolume = this.volumeNow;

    //Build String for characteristics of phones
    StringBuilder characteristics = new StringBuilder();
    characteristics.append(String.format("\nThe Head Phone Manufacturer "
            + "is: %s", phoneManufacturer));
    characteristics.append(String.format("\nThe Color of the Head Phone Set "
            + "is: %s", phoneColor));
    characteristics.append(String.format("\nThe Head Phones are Currently: "
            + " %s", phonesPluggedIn));
    characteristics.append(String.format("\nThe Head Phone Volume is "
            + "Currently Set on: %s", currentVolume));

    //return string for characteristics
    return characteristics.toString();


}    

}

package headphone_wk6;

//Import scanner since this will require user input
import java.util.Scanner;

//Test class for head phone
public class HeadPhone_WK6_Test {

//Command line arguments
public static void main(String[] args) {

    //Initialize input variable
    int Input;

    //Call scanner
    Scanner stdin = new Scanner(System.in);

    HeadPhone_WK6 bestPair = new HeadPhone_WK6(2, false, "SONY", "BLUE");
    HeadPhone_WK6 worstPair = new HeadPhone_WK6(2, false, "BOSE", "BLACK");
    HeadPhone_WK6 decentPair = new HeadPhone_WK6(2, false, "RCA", "ORANGE");

    int bestPairVolume = bestPair.getVolume();
    boolean bestPairPluggedIn = bestPair.getPluggedIn();
    String bestPairManufacturer = bestPair.getManufacturer();
    String bestPairHeadPhoneColor = bestPair.getHeadPhoneColor();     
    String bestPairVolumeNow = bestPair.volumeNow;
    String bestPairPluggedInStat = bestPair.pluggedInStat;

    int worstPairVolume = worstPair.getVolume();
    boolean worstPairPluggedIn = worstPair.getPluggedIn();
    String worstPairManufacturer = worstPair.getManufacturer();
    String worstPairHeadPhoneColor = worstPair.getHeadPhoneColor();
    String worstPairVolumeNow = worstPair.volumeNow;
    String worstPairPluggedInStat = worstPair.pluggedInStat;

    int decentPairVolume = decentPair.getVolume();
    boolean decentPairPluggedIn = decentPair.getPluggedIn();
    String decentPairManufacturer = decentPair.getManufacturer();
    String decentPairHeadPhoneColor = decentPair.getHeadPhoneColor();
    String decentPairVolumeNow = decentPair.volumeNow;
    String decentPairPluggedInStat = decentPair.pluggedInStat;

    //Introduce HeadPhone helper
    System.out.print("Hi there! Let's have you try a pair of head phones "
            + "on and we'll see what you think of them! \nStart by choosing a "
            + "random pair of head phones. To do this, enter 1, 2, or 3: ");

    //Get user input for random pair of headphones
            Input = stdin.nextInt();

            //Loop for random headphone selection
            if (Input == 1){

                //echo user input
                System.out.println("You have chosen the best pair of "
                        + "headphones! Here is a list of the characteristics: ");

                System.out.println(bestPair.toString());

            //End if
            }

            else if (Input == 2){
                System.out.println("You have chosen the worst pair of "
                        + "headphones. Here is a list of the characteristics: ");

                System.out.println(worstPair.toString());


            //End If   
            }

            else if(Input == 3){
                System.out.println("You have chosen a decent pair of "
                        + "headphones. Here is a list of the characteristics:");

               System.out.println(decentPair.toString());

            //End If    
            }

            else{ 
                System.out.println("You have expressed that you want to see "
                        + "the default pair of headphones. They are the "
                        + "decent pair! Here's a list of the characteristics:");

                System.out.println(decentPair.toString());

            }






}  

}

也许可以尝试这样的方法,首先通过扫描仪获取输入,然后创建 HeadPhone 对象:

//Initialize input variable
    int Input;

    //Call scanner
    Scanner stdin = new Scanner(System.in);

    //Introduce HeadPhone helper
    System.out.print("Hi there! Let's have you try a pair of head phones "
            + "on and we'll see what you think of them! \nStart by choosing a "
            + "random pair of head phones. To do this, enter 1, 2, or 3: ");

    //Get user input for random pair of headphones
    Input = stdin.nextInt();
    System.out.println("Now give me value of plug in 1 or 2");
    int plugInState = stdin.nextInt();
    boolean pluggedIn=true;
    if(plugInState==1) pluggedIn = false;
    if(plugInState==2) pluggedIn = true;
    System.out.println("Now give me value volume (1/2/3");
    int volumeState = stdin.nextInt();
    HeadPhone_WK6 bestPair = new HeadPhone_WK6(volumeState, pluggedIn, "SONY", "BLUE");
    HeadPhone_WK6 worstPair = new HeadPhone_WK6(volumeState, pluggedIn, "BOSE", "BLACK");
    HeadPhone_WK6 decentPair = new HeadPhone_WK6(volumeState, pluggedIn, "RCA", "ORANGE");

你可以把if子句放在最后,根据你的input,你可以创建一个你需要的对象。如果您需要单独的方法,请在 main():

中执行此操作
        HeadPhone_WK6 hp = new HeadPhone_WK6();
    //Initialize input variable
    int Input;

    //Call scanner
    Scanner stdin = new Scanner(System.in);

    //Introduce HeadPhone helper
    System.out.print("Hi there! Let's have you try a pair of head phones "
            + "on and we'll see what you think of them! \nStart by choosing a "
            + "random pair of head phones. To do this, enter 1, 2, or 3: ");

    //Get user input for random pair of headphones
    Input = stdin.nextInt();
    System.out.println("Now give me value of plug in 1 or 2");
    int plugInState = stdin.nextInt();
    hp.setPluggedIn(plugInState);

    System.out.println("Now give me value volume (1/2/3");
    int volumeState = stdin.nextInt();
    hp.setVolume(volumeState);
    HeadPhone_WK6 bestPair = new HeadPhone_WK6(hp.getVolume(), hp.getPluggedIn(), "SONY", "BLUE");
    HeadPhone_WK6 worstPair = new HeadPhone_WK6(hp.getVolume(), hp.getPluggedIn(), "BOSE", "BLACK");
    HeadPhone_WK6 decentPair = new HeadPhone_WK6(hp.getVolume(), hp.getPluggedIn(), "RCA", "ORANGE");

在哪里

setPluggedIn(int pluggedInState) { if(plugInState==1) pluggedIn = false; else if(plugInState==2) pluggedIn = true; else { pluggedIn = false; } }