這是控制檯。在用戶決定計算什麼後輸入數值。從類返回的值爲零。我有私人變量,訪問器和增變器。我不知道這是什麼原因。有任何想法嗎??請!!!當前代碼在運行時僅返回0值
public class Console {
public static final int USER_CHOIDE_VOLTAGE = 1;
public static final int USER_CHOIDE_AMPERAGE = 2;
public static final int USER_CHOIDE_RESISTANCE = 3;
public static void main(final String[] args) {
// Creates a Circuit Object
Circuit myCircuit = new Circuit();
// Creates a Scanner Object to get input from user
Scanner keyboard = new Scanner(System.in);
// Holds input from user
int userChoice;
System.out.println("\n");
System.out.println("This system will calculate the ");
System.out.println("\tVoltage, Amperage, or Resistance ");
System.out.println("\tgiven the other two values using Ohms Law.");
System.out.println("\n");
// Ask user what to calculate, if it is not one
// of the options, ask again(while-do loop)
do {
System.out.println("Which value would you like to calculate?");
System.out.println("\t1. Voltage");
System.out.println("\t2. Resistane");
System.out.println("\t3. Amperage");
System.out.println("\n");
System.out.println("Please select 1, 2, or 3");
userChoice = keyboard.nextInt();
//Switch follows cases for what the user would
// like to calculate
switch (userChoice) {
case USER_CHOIDE_VOLTAGE:
// Gets Amperage from User
System.out.println("Please enter the Amperage:");
// Sets Amperage value
myCircuit.setAmperage(keyboard.nextDouble());
// Gets Resistance from User
System.out.println("Please enter the Resistance:");
// Sets Resistance value
myCircuit.setResistance(keyboard.nextDouble());
// Returns Value for voltage from method
System.out.println("The value of Voltage is: "
+ myCircuit.getVoltage());
break;
case USER_CHOIDE_AMPERAGE:
// Gets Voltage from User
System.out.println("Please enter the Voltage:");
myCircuit.setVoltage(keyboard.nextDouble());
// Gets Resistance from User
System.out.println("Please enter the Resistance:");
// Sets Resistance value
myCircuit.setResistance(keyboard.nextDouble());
// Returns Value for Amperage from method
System.out.println("The value of Amperage is: "
+ myCircuit.getAmperage());
break;
case USER_CHOIDE_RESISTANCE:
// Gets Amperage from User
System.out.println("Please enter the Amperage:");
// Sets Amperage value
myCircuit.setAmperage(keyboard.nextDouble());
// Gets Voltage from User
System.out.println("Please enter the Voltage:");
myCircuit.setVoltage(keyboard.nextDouble());
// Returns Value for Resistance from method
System.out.println("The value of Resistance is: "
+ myCircuit.getResistance());
break;
// Do Nothing Since do while loop takes care of this option
default:
}
} while (userChoice != USER_CHOIDE_VOLTAGE
&& userChoice != USER_CHOIDE_AMPERAGE
&& userChoice != USER_CHOIDE_RESISTANCE);
System.exit(0);
keyboard.close();
}
}
這是類
public class Circuit {
private double voltage, resistance, amperage;
public double getVoltage() {
return voltage;
}
public double getResistance() {
return resistance;
}
public double getAmperage() {
return amperage;
}
public void setVoltage(double pVoltage) {
voltage = pVoltage;
}
public void setResistance(double pResistance) {
resistance = pResistance;
}
public void setAmperage(double pAmperage) {
amperage = pAmperage;
}
public void calcVoltage() {
voltage = amperage * resistance;
}
public void calcResistance() {
resistance = voltage/amperage;
}
public void calcAmperage() {
amperage = voltage/resistance;
}
}
請,使我們更容易幫助你,張貼[最小的,完整的,經過測試和讀示例](HTTP ://stackoverflow.com/help/mcve)。換句話說,我們可以運行代碼來測試它。請注意,我們可能沒有'bp.Circuit'庫。 – Christian
您能否刪除與您的問題無關的所有內容?這包括微不足道的getter/setter和評論。下面的bp.Circuit是 –
。對不起 – user2856759