2014-01-28 21 views
0

我想讓用戶有機會輸入另一個計算。問題是,在此之前我也有一個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(); 
    } 
} 
+1

爲'退出'創建選項4並保留在主循環中,直到用戶選擇它。您將擁有單個用戶輸入點,無需其他問題。 –

回答

0

包住整個事情在while循環一做,促使在最後的用戶和如果他們說是的話,這個條件就成立了。

0

I bel即使問題在於你的條件。

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

有了這個邏輯,如果用戶選擇這些選項,do-while循環就會結束,這就是你想要的。但是由於userChoice < 0的條件,如果輸入的是負數,則會出現循環。嘗試運行您的程序並在第一次提示時輸入負數。它應該與你想要的相反。

有時,do-while循環可能會讓人困惑,因爲如果某個條件成立,您希望它退出,但是如果while條件評估爲true,程序將循環而不是退出。我以前做過這個。嘗試改變您的邏輯,以便只有在用戶輸入的數字高於有效選項時纔會循環。邏輯將會更簡單,除非您希望輸入0也循環,否則您不必使用任何& &。

相關問題