我想讓用戶有機會輸入另一個計算。問題是,在此之前我也有一個do-while循環(這個循環負責確保用戶只輸入選項1,2或3.例如,我運行我的程序並決定計算電壓(選項1),但我不小心輸入4,程序一直運行到最後問我是否要再次運行程序,而不是給我選擇1,2,3的選項,這是我的方式節目我以前工作的決定,如果他們想繼續玩下去這個選項添加到程序(再次運行程序)想給用戶再次啓動程序的選項
/*Pseoudocode
*THIS PROGRAM WILL CALCULATE VOLTAGE, RESISTANCE, AMPERAGE
* ASK USER TO CALCULATE WHAT TO CALCULATE
* IF VOLTS
* GET AMPS
* GET RESISTANCE
* RETURN AMPS * RESISTANCE
* IF RESISTANCE
* GET VOLTS
* GET AMPERAGE
* RETURN VOLTS/AMPERAGE
* IF AMPERAGE
* GET VOLTS
* GET AMPS
* RETURN VOLTS/RESISTANCE
*/
package gui;
import java.text.DecimalFormat;
import java.util.Scanner;
import bp.Circuit;
/**
* This program calculates the voltage, resistance, or amperage depending on the
* input of the user according to the Ohms law.
*
* @author
*/
public final class Console {
/**
* Class is Final and Contructor is private.
*/
private Console() {
// Not called
}
/**
* Makes a Constant for Voltage.
*/
public static final int USER_CHOICE_VOLTAGE = 1;
/**
* Makes a Constant for Amperage.
*/
public static final int USER_CHOICE_RESISTANCE = 2;
/**
* Makes a Constant for Resistance.
*/
public static final int USER_CHOICE_AMPERAGE = 3;
public static final int RE_RUN = 10;
public static final int STOP = 11;
/**
* Makes the Body of the Program.
*
* @param args
* Accepts String arguments.
*/
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;
// Format the answer to 2 decimals
DecimalFormat f = new DecimalFormat("##.00");
int continueRunning = RE_RUN;
while (continueRunning != STOP) {
// Statement shows intructions to user
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. Resistance");
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_CHOICE_VOLTAGE:
break;
case USER_CHOICE_RESISTANCE:
break;
case USER_CHOICE_AMPERAGE:
break;
default: // Do Nothing Since do while loop takes care of this option
}
} while (userChoice != USER_CHOICE_VOLTAGE
&& userChoice != USER_CHOICE_AMPERAGE
&& userChoice != USER_CHOICE_RESISTANCE
&& userChoice < 0);
//Enter a Space
System.out.println();
//Ask User if he has another calculation
System.out.println("Would you like to run this program"
+ " again? Type:");
System.out.println("\t(10) for Yes");
System.out.println("\t(11) to Finish the program");
//Gets user answer
continueRunning = keyboard.nextInt();
}
System.exit(0);
keyboard.close();
}
}
爲'退出'創建選項4並保留在主循環中,直到用戶選擇它。您將擁有單個用戶輸入點,無需其他問題。 –