【问题标题】:How do I revise this Java Code to enter 1, 2, or 3 so it prints low medium or high如何修改此 Java 代码以输入 1、2 或 3,以便打印低中或高
【发布时间】:2018-12-08 06:20:28
【问题描述】:

我已经编写了下面的代码,但在音量设置方面遇到了问题。其他一切都有效。我需要使用 1、2 或 3 将音量设置为低、中或高。我的输出始终为 0 或只是我输入的数字。这一切都必须在同一个类 java 文件中完成(而不是使用测试类)。有人可以告诉我我缺少什么来纠正音量问题吗?

包装耳机;

导入 java.util.Scanner;

/** * 文件:Headphones.java * 目的:耳机类列出耳机是否插入, * 颜色、制造商、体积,并为用户提供选择 * 更改音量设置 */ 公共类耳机{

// Define class variables
private boolean pluggedIn, wirelessBluetooth;
private String color;
private int volume = MEDIUM;
private String manufacturer;
private String model;
public static final int LOW = 1;
public static final int MEDIUM = 2;
public static final int HIGH = 3;
public int getVolume;


// New scanner to receive user inputs
Scanner scannerIn = new Scanner(System.in);

// Default Constructor
public Headphones() {
    this.pluggedIn = false;
    this.wirelessBluetooth = false;
    this.color = "black";
    this.manufacturer = "sony";
    this.model = "2.0";
    this.volume = MEDIUM;
}
    public int getVolume() {
    return volume;

} //End Default Constructor

// Method to prompt for user input values
public void inputValues(){    

    // Plugged in prompt
    System.out.print("Are the headphones plugged in [true or false]?  ");
    pluggedIn = scannerIn.nextBoolean(); 

    // wireless bluetooth prompt
    System.out.print("Are headphones wireless bluetooth [true or false]?  ");
    wirelessBluetooth = scannerIn.nextBoolean(); 

    // Color prompt
    System.out.print("What is the color of the headphones?  ");
    color = scannerIn.next();

    // Volume prompt
    System.out.print("What volume are the headphones set to [1,2, 3]?  ");
    getVolume = scannerIn.nextInt();

     // Manufacturer prompt
    System.out.print("Enter the manufacturer of the headphones:  ");
    manufacturer = scannerIn.next();

    // Model prompt
    System.out.print("Enter the model of the headphones:  ");
    model = scannerIn.next();

} //End inputValues method    

// Verification method - printing input values
public void dataVerification() {
    System.out.println("\n\t\u266F \u2669 \u266D \u266F \u2669 \u266D \u266F \u2669 \u266D \u266F \u2669 \u266D \u266F \u2669 \u266D \u266F \u2669 \u266D \n \t Headphones plugged in:  " + pluggedIn + "\n \t Wireless Bluetooth:     " + wirelessBluetooth + "\n \t Color of headphones:    " + color 
            + "\n \t Volume:                " + getVolume + "\n \t Manufacturer:          " + manufacturer + "\n \t Model:                 " + model + " \n \t\u266D \u2669 \u266F \u266D \u2669 \u266F \u266D \u2669 \u266F \u266D \u2669 \u266F \u266D \u2669 \u266F \u266D \u2669 \u266F");
} //End dataVerification method

// Change volume of headphones
public void changeVolume() {
    int volumeChange;

    // Prompting user with option to change volume
    System.out.print("\nWould you like to change the volume"
            + "[y or n]?  ");
    volumeChange = scannerIn.next().charAt(0);

    // if yes, prompt for volume change level
    if (volumeChange == 'y') {
        System.out.print("\nWhat volume would you like to set the headphones [1, 2, or 3]"
                + " to?  ");
        volume = scannerIn.nextInt();

        System.out.println("\nThe volume is now set to:  " + getVolume //confirmation of volume change message
                + " ");


    } //End volumeChange If
} //End changeVolume method

public static void main(String[] args) {
    Headphones headphone = new Headphones();

    // New scanner to receive user inputs
    Scanner check = new Scanner(System.in);

    //variable to exit program
    int endProgram = 4;

    while (true) {

    headphone.inputValues();
    headphone.dataVerification();
    headphone.changeVolume();

        //Prompt user whether to loop program or exit
        System.out.println("Do you wish to enter data for an additional pair of headphones? [Enter 4 to continue or enter 0 to exit program.]");
        endProgram = check.nextInt();


        if (endProgram == 0) {
            break;

        }//End if endProgram == 0
     }//End if true

【问题讨论】:

  • 您的问题是您的班级中既有volume 成员又有getVolume 成员,并且您将它们混淆了。删除其中一个,您的代码应该可以更好地工作

标签: java string prompt volume


【解决方案1】:
public int getVolume;   //comment this line in your code. Use volume variable instead of getVolume in everywhere of your code

需要强调两点,这将有助于你提高

  1. 尽量避免对变量使用动词。这是一个最佳实践
  2. 在考虑 OOP 概念时,系统应该有一个变量 为一处房产保值。但是在这里,您使用了两个单独的变量来保持音量状态。

导入 java.util.Scanner;

public class Headphones
{
    // Define class variables
    private boolean pluggedIn, wirelessBluetooth;
    private String color;
    private int volume = MEDIUM;
    private String manufacturer;
    private String model;
    public static final int LOW = 1;
    public static final int MEDIUM = 2;
    public static final int HIGH = 3
   //    public int getVolume;                //No need to use seperate variable for volume and changed value


   // New scanner to receive user inputs
    Scanner scannerIn = new Scanner(System.in);

    // Default Constructor
 public Headphones()
    {
        this.pluggedIn = false;
        this.wirelessBluetooth = false;
        this.color = "black";
        this.manufacturer = "sony";
        this.model = "2.0";
        this.volume = MEDIUM;
    }
    public int getVolume()
    {
        return volume;
    } //End Default Constructor

    // Method to prompt for user input values
    public void inputValues()
    {

        // Plugged in prompt
        System.out.print("Are the headphones plugged in [true or false]?  ");
        pluggedIn = scannerIn.nextBoolean();

        // wireless bluetooth prompt
        System.out.print("Are headphones wireless bluetooth [true or false]?  ");
        wirelessBluetooth = scannerIn.nextBoolean();

       // Color prompt
       System.out.print("What is the color of the headphones?  ");
       color = scannerIn.next();

        // Volume prompt
       System.out.print("What volume are the headphones set to [1,2, 3]?  ");
       volume = scannerIn.nextInt();

        // Manufacturer prompt
       System.out.print("Enter the manufacturer of the headphones:  ");
       manufacturer = scannerIn.next();

        // Model prompt
       System.out.print("Enter the model of the headphones:  ");
       model = scannerIn.next();

     } //End inputValues method

    // Verification method - printing input values
    public void dataVerification()
    {
        System.out.println("\n\t\u266F \u2669 \u266D \u266F \u2669 \u266D \u266F 
\u2669 \u266D \u266F \u2669 \u266D \u266F \u2669 \u266D \u266F \u2669 \u266D \n \t 
Headphones plugged in:  " + pluggedIn + "\n \t Wireless Bluetooth:     " + 
wirelessBluetooth + "\n \t Color of headphones:    " + color
            + "\n \t Volume:                " + volume + "\n \t Manufacturer:          " + manufacturer + "\n \t Model:                 " + model + " \n \t\u266D \u2669 \u266F \u266D \u2669 \u266F \u266D \u2669 \u266F \u266D \u2669 \u266F \u266D \u2669 \u266F \u266D \u2669 \u266F");
    } //End dataVerification method

    // Change volume of headphones
    public void changeVolume()
    {
        int volumeChange;

        // Prompting user with option to change volume
        System.out.print("\nWould you like to change the volume"
                + "[y or n]?  ");
        volumeChange = scannerIn.next().charAt(0);

        // if yes, prompt for volume change level
        if (volumeChange == 'y') {
            System.out.print("\nWhat volume would you like to set the headphones [1, 2, or 3]"
                + " to?  ");
            volume = scannerIn.nextInt();

            System.out.println("\nThe volume is now set to:  " + volume //confirmation of volume change message
                + " ");


        } //End volumeChange If
    } //End changeVolume method

    public static void main(String[] args)
    {
        Headphones headphone = new Headphones();

        // New scanner to receive user inputs
        Scanner check = new Scanner(System.in);

       //variable to exit program
        int endProgram = 4;

        while (true) {

            headphone.inputValues();
            headphone.dataVerification();
            headphone.changeVolume();

            //Prompt user whether to loop program or exit
            System.out.println("Do you wish to enter data for an additional pair of headphones? [Enter 4 to continue or enter 0 to exit program.]");
            endProgram = check.nextInt();


            if (endProgram == 0) {
                break;

            }//End if endProgram == 0
        }//End if
    }
}

【讨论】:

  • 动词是什么意思?你有这方面的最佳实践参考吗?
  • 使用动词get定义“getVolume”成员变量会导致与“volume”成员变量的getter方法混淆
【解决方案2】:

试试这个 changeVolume() 函数。

// Change volume of headphones
    public void changeVolume() {
        char volumeChange;

        // Prompting user with option to change volume
        System.out.print("\nWould you like to change the volume"
                + "[y or n]?  ");
        volumeChange = scannerIn.next().charAt(0);

        // if yes, prompt for volume change level
        if (volumeChange == 'y') {
            System.out.print("\nWhat volume would you like to set the headphones [1, 2, or 3]"
                    + " to?  ");
            volume = scannerIn.nextInt();

            System.out.println("\nThe volume is now set to:  " + getVolume //confirmation of volume change message
                    + " ");


        } //End volumeChange If
    } //End changeVolume method

【讨论】:

    猜你喜欢
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 2017-12-21
    相关资源
    最近更新 更多