【发布时间】: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成员,并且您将它们混淆了。删除其中一个,您的代码应该可以更好地工作