首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想给用户重新启动程序的选项

我想给用户重新启动程序的选项
EN

Stack Overflow用户
提问于 2014-01-28 09:03:58
回答 2查看 1.2K关注 0票数 0

我正在尝试让用户有机会输入另一个计算。问题是在此之前我还有一个do-while循环(这个循环负责确保用户只输入选项1、2或3。例如。我运行我的程序,我决定计算电压(选项1),但我意外地输入了4,程序一直运行到最后,询问我是否想再次运行程序,而不是让我选择1,2,3,这是我在决定将此选项添加到程序中(再次运行程序)之前让程序工作的方式

代码语言:javascript
复制
/*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();
    }
}
EN

回答 2

Stack Overflow用户

发布于 2014-01-28 10:33:17

将整个过程封装在一个do while循环中,在最后提示用户是否要继续执行,如果用户说是,则使while条件为true。

票数 0
EN

Stack Overflow用户

发布于 2014-01-28 10:33:28

我相信问题出在你的while状态。

代码语言:javascript
复制
while (userChoice != USER_CHOICE_VOLTAGE
                && userChoice != USER_CHOICE_AMPERAGE
                && userChoice != USER_CHOICE_RESISTANCE
                && userChoice < 0);

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

有时do-while循环可能会令人困惑,因为您希望它在某个条件为true时退出,但如果while条件的计算结果为true,程序将循环而不是退出。我以前也这么做过。尝试更改您的逻辑,使其仅在用户输入的数字大于有效选项时才循环。逻辑将会更简单,并且您不需要使用任何&&,除非您也希望输入0循环。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21394978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档