想要为用户提供再次启动程序的选项

时间:2014-01-28 01:03:58

标签: java while-loop do-while

我试图让用户有机会输入另一个计算。问题是我之前也有一个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();
    }
}

2 个答案:

答案 0 :(得分:0)

在do while循环中包含整个事物,在最后提示用户是否要继续前进,如果他们同意,则将while条件设为true。

答案 1 :(得分:0)

我认为问题在于你的状况。

while (userChoice != USER_CHOICE_VOLTAGE
                && userChoice != USER_CHOICE_AMPERAGE
                && userChoice != USER_CHOICE_RESISTANCE
                && userChoice < 0);

使用此逻辑,如果用户选择了您想要的选项,则do-while循环将结束。但是由于userChoice < 0条件,如果输入负数,则循环。尝试运行程序并在第一个提示符处输入负数。它应该与你想要的相反。

有时do-while循环可能会让人感到困惑,因为如果某个条件为真,你希望它退出,但如果while条件的计算结果为true,则程序循环而不是退出。我之前做过这个。尝试更改逻辑,以便仅在用户输入的数字高于有效选项时才会循环。逻辑将更简单,你不必使用任何&amp;&amp;除非你想要输入0来循环。