这个退出功能我需要帮助吗?

时间:2015-12-06 02:10:47

标签: java

当我输入"退出"时,我需要帮助使这个程序退出。这应该是一个分数计算器;现在我想创建一个驱动程序循环来继续执行输入功能,直到用户键入" quit"任何套管。

import java.util.*;

public class FractionCalculator {

   // useDelimiter or split method in string class
   public static void main(String[] args) {
      Greeting();
      produceAnswer();
   }

   static String value1Str = "";
   static String value2Str = "";
   static char operator = ' ';

   public static void Greeting() {
      Scanner kb = new Scanner (System.in);

      String userName;
      System.out.print("Hello Person, What is your first name: ");
      userName = kb.next();
      System.out.println("Hi " + userName + ", welcome to the great  mystical fraction calculator.");
   }   

   public static void produceAnswer() {
      Scanner kb = new Scanner (System.in);
      System.out.println("What is your input or type quit to leave?");
      String input = kb.nextLine();     
      boolean value1Done = false;
      boolean operatorDone = false;
      boolean value2Done = false;
      boolean correctFormat = false;

      for (int i = 0; i < input.length(); i++) {   
         System.out.println("The input of given string is: " +input.charAt(i));         

         if (input.charAt(i) != ' ' && value1Done == false) {
            value1Str += input.charAt(i);
         }
         else{
            value1Done = true;
         }  

         if (input.charAt(i) == '+' || input.charAt(i) == '-' ||   input.charAt(i) == '*' || input.charAt(i) == '/' && operatorDone == false && value1Done == true) {
            operator = input.charAt(i);
            operatorDone = true;
            i=i+1;
         } 

         if (input.charAt(i) != ' ' && value1Done == true && value2Done == false) {
            value2Str  += input.charAt(i);
         }      
         else {
            value2Done = false;
         }
         else if kb.next().equalsIgnoreCase("quit"){
            System.out.println("Why are you leaving? Comeback soon bby");
            quit = true;
            correctFormat = true;
        }
     }  

1 个答案:

答案 0 :(得分:0)

当你确定输入了“quit”时,在if语句块中放入一个break语句:

  else if (kb.next().equalsIgnoreCase("quit"){
      System.out.println("Why are you leaving? Comeback soon bye");
      break;
  }