如果用户键入"退出"?如何使用while循环连续询问输入并停止程序?

时间:2014-11-10 01:49:16

标签: java expression calculator

我目前正在我的学校参加AP计算机科学课程,我的一个项目遇到了一些麻烦!该项目要求我创建一个计算器,可以评估表达式然后解决它。我已经完成了大部分工作,但我遇到了一点麻烦,因为我的老师让我使用while循环来不断询问输入并显示答案,我坚持这一点。要结束程序,用户必须输入"退出",是否有人有任何提示?

import java.util.*;
public class Calculator {
   public static void main(String[] args) {
      System.out.println("Welcome to the AP Computer Science calculator!!");
      System.out.println();
      System.out.println("Please use the following format in your expressions: (double)(space)(+,-,*,/...)(space)(double)");
      System.out.println("or: (symbol)(space)(double)");
      System.out.println();
      next();
   }

   public static void next() {
      Scanner kb = new Scanner(System.in);
      System.out.print("Enter an expression, or quit to exit: ");
      String expression = kb.nextLine();
      next3(expression);  
   }
   public static void next3(String expression) {
         while (QuitFunction(expression)) {
            next2(expression);
            next();
         }   
   }

   public static void next2(String expression) {
      if (OperatorFor2OperandExpressions(expression).equals("+")) {
         System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) + SecondOperandFor2OperandExpressions(expression)));          
      }
      else if (OperatorFor2OperandExpressions(expression).equals("*")) {
         System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " "  + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) * SecondOperandFor2OperandExpressions(expression)));
      }
      else if (OperatorFor2OperandExpressions(expression).equals("-")) {
         System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) - SecondOperandFor2OperandExpressions(expression)));       
      }
      else if (OperatorFor2OperandExpressions(expression).equals("/")) {
         System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " "  + SecondOperandFor2OperandExpressions(expression) + " = " + (FirstOperandFor2OperandExpressions(expression) / SecondOperandFor2OperandExpressions(expression)));
      }
      else if (OperatorFor2OperandExpressions(expression).equals("^")) {
         System.out.println(FirstOperandFor2OperandExpressions(expression) + " " + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression) + " = " + Math.pow(FirstOperandFor2OperandExpressions(expression),SecondOperandFor2OperandExpressions(expression)));
      }
      else if (OperatorFor1OperandExpressions(expression).equals("|")) {
         System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.abs(OperandFor1OperatorExpressions(expression)));
      }
      else if (OperatorFor1OperandExpressions(expression).equals("v")) {
         System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.sqrt(OperandFor1OperatorExpressions(expression)));
      }
      else if (OperatorFor1OperandExpressions(expression).equals("~")) {
         double x = 0.0;
         System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + (Math.round(OperandFor1OperatorExpressions(expression))+ x));
      }
      else if (OperatorFor1OperandExpressions(expression).equals("s")) {
         System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.sin(OperandFor1OperatorExpressions(expression)));
      }
      else if (OperatorFor1OperandExpressions(expression).equals("c")) {
         System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.cos(OperandFor1OperatorExpressions(expression)));
      }
      else if (OperatorFor1OperandExpressions(expression).equals("t")) {
         System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression) + " = " + Math.tan(OperandFor1OperatorExpressions(expression))); 
      } 
   }

      public static double FirstOperandFor2OperandExpressions(String expression) {
         String[] tokens = expression.split(" ");
         String OperandOrOperator = tokens[0];
         double y = Double.parseDouble(OperandOrOperator);
         return y;         
   }
      public static double SecondOperandFor2OperandExpressions(String expression) {
         String[] tokens = expression.split(" ");
         String OperandOrOperator = tokens[2];
         double y = Double.parseDouble(OperandOrOperator);
         return y;
   }
       public static String OperatorFor2OperandExpressions(String expression) {
         String[] tokens = expression.split(" ");
         String OperandOrOperator = tokens[1];
         return OperandOrOperator;
   }
      public static String OperatorFor1OperandExpressions(String expression) {
         String[] tokens = expression.split(" ");
         String OperandOrOperator = tokens[0];
         return OperandOrOperator; 
   }
      public static double OperandFor1OperatorExpressions(String expression) {
         String[] tokens = expression.split(" ");
         String OperandOrOperator = tokens[1];
         double y = Double.parseDouble(OperandOrOperator);
         return y;  
   }
      public static boolean QuitFunction(String expression) {
         if (expression.equalsIgnoreCase("quit")) {
            System.out.println("Goodbye!");
            return false;
         }
         else {
            return true;
         }
      }           
}

4 个答案:

答案 0 :(得分:1)

从你的方法中删除链接逻辑......

例如。 (伪代码,因为你负责做家庭作业)

method A { ....  B(); }

method B { ..... C(); }

method C { .... D(); }

method D ....

而是这样做:

while( boolean_condition )
{
    call method A();
    call method B();
    call method C();
    call method D();

    set boolean_condition to false if we are "done";

}

答案 1 :(得分:0)

next函数中,您可以使用以下内容来代替调用next3(顺便提一下,非常富有表现力的名字),而不是:

while (!expression.equals("quit"))
    next3();

答案 2 :(得分:0)

您可以使用始终为true的while来重复读取输入。为此用途

while(true) {
   .......
}

每次阅读输入后,您都可以检查输入是否为quit,如果它已退出,您可以退出程序。为此用途

if(expression.equals("quit")) { // to check whether the input is quit.
    System.exit(0); // for exiting the program
}

CODE:

    public static void main(String[] args) {
        System.out.println("Welcome to the AP Computer Science calculator!!");
        System.out.println();
        System.out
                .println("Please use the following format in your expressions: (double)(space)(+,-,*,/...)(space)(double)");
        System.out.println("or: (symbol)(space)(double)");
        System.out.println();
        while (true) {
            next();
        }
    }

    public static void next() {
        Scanner kb = new Scanner(System.in);
        System.out.print("Enter an expression, or quit to exit: ");
        String expression = kb.nextLine();
        if (expression.equals("quit")) {
            System.exit(0);
        } else {
            next3(expression);
        }
    }

在你的代码中,这些方法的名称确实令人困惑。

  
      
  • 方法名称应该描述方法的目的,制作你的   代码自我记录并减少了对额外评论的需求。
  •   
  • 方法应该是动词,在第一个字母的大小写混合   小写,每个内部单词的首字母大写。 (naming conventions)
  •   
  • 通过   遵循命名约定,它可以使您的代码易于阅读   另一个人。
  •   

答案 3 :(得分:0)

稍微更改一下,到当前代码。

next()next3()方法中完成的更改。

 public static void main(String[] args) {
        System.out.println("Welcome to the AP Computer Science calculator!!");
        System.out.println();
        System.out
                .println("Please use the following format in your expressions: (double)(space)(+,-,*,/...)(space)(double)");
        System.out.println("or: (symbol)(space)(double)");
        System.out.println();
        next();
    }

    public static void next() {
        Scanner kb = new Scanner(System.in);
        System.out.print("Enter an expression, or quit to exit: ");
        while (kb.hasNext()) {
            String expression = kb.nextLine();
            next3(expression);
            System.out.print("Enter an expression, or quit to exit: ");
        }
    }

    public static void next3(String expression) {
        if (QuitFunction(expression)) {
            next2(expression);
            next();
        } else {
            System.exit(0);
        }
    }

    public static void next2(String expression) {
        if (OperatorFor2OperandExpressions(expression).equals("+")) {
            System.out.println(FirstOperandFor2OperandExpressions(expression) + " "
                    + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression)
                    + " = "
                    + (FirstOperandFor2OperandExpressions(expression) + SecondOperandFor2OperandExpressions(expression)));
        } else if (OperatorFor2OperandExpressions(expression).equals("*")) {
            System.out.println(FirstOperandFor2OperandExpressions(expression) + " "
                    + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression)
                    + " = "
                    + (FirstOperandFor2OperandExpressions(expression) * SecondOperandFor2OperandExpressions(expression)));
        } else if (OperatorFor2OperandExpressions(expression).equals("-")) {
            System.out.println(FirstOperandFor2OperandExpressions(expression) + " "
                    + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression)
                    + " = "
                    + (FirstOperandFor2OperandExpressions(expression) - SecondOperandFor2OperandExpressions(expression)));
        } else if (OperatorFor2OperandExpressions(expression).equals("/")) {
            System.out.println(FirstOperandFor2OperandExpressions(expression) + " "
                    + OperatorFor2OperandExpressions(expression) + " " + SecondOperandFor2OperandExpressions(expression)
                    + " = "
                    + (FirstOperandFor2OperandExpressions(expression) / SecondOperandFor2OperandExpressions(expression)));
        } else if (OperatorFor2OperandExpressions(expression).equals("^")) {
            System.out.println(FirstOperandFor2OperandExpressions(expression)
                    + " "
                    + OperatorFor2OperandExpressions(expression)
                    + " "
                    + SecondOperandFor2OperandExpressions(expression)
                    + " = "
                    + Math.pow(FirstOperandFor2OperandExpressions(expression),
                            SecondOperandFor2OperandExpressions(expression)));
        } else if (OperatorFor1OperandExpressions(expression).equals("|")) {
            System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression)
                    + " = " + Math.abs(OperandFor1OperatorExpressions(expression)));
        } else if (OperatorFor1OperandExpressions(expression).equals("v")) {
            System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression)
                    + " = " + Math.sqrt(OperandFor1OperatorExpressions(expression)));
        } else if (OperatorFor1OperandExpressions(expression).equals("~")) {
            double x = 0.0;
            System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression)
                    + " = " + (Math.round(OperandFor1OperatorExpressions(expression)) + x));
        } else if (OperatorFor1OperandExpressions(expression).equals("s")) {
            System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression)
                    + " = " + Math.sin(OperandFor1OperatorExpressions(expression)));
        } else if (OperatorFor1OperandExpressions(expression).equals("c")) {
            System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression)
                    + " = " + Math.cos(OperandFor1OperatorExpressions(expression)));
        } else if (OperatorFor1OperandExpressions(expression).equals("t")) {
            System.out.println(OperatorFor1OperandExpressions(expression) + " " + OperandFor1OperatorExpressions(expression)
                    + " = " + Math.tan(OperandFor1OperatorExpressions(expression)));
        }
    }

    public static double FirstOperandFor2OperandExpressions(String expression) {
        String[] tokens = expression.split(" ");
        String OperandOrOperator = tokens[0];
        double y = Double.parseDouble(OperandOrOperator);
        return y;
    }

    public static double SecondOperandFor2OperandExpressions(String expression) {
        String[] tokens = expression.split(" ");
        String OperandOrOperator = tokens[2];
        double y = Double.parseDouble(OperandOrOperator);
        return y;
    }

    public static String OperatorFor2OperandExpressions(String expression) {
        String[] tokens = expression.split(" ");
        String OperandOrOperator = tokens[1];
        return OperandOrOperator;
    }

    public static String OperatorFor1OperandExpressions(String expression) {
        String[] tokens = expression.split(" ");
        String OperandOrOperator = tokens[0];
        return OperandOrOperator;
    }

    public static double OperandFor1OperatorExpressions(String expression) {
        String[] tokens = expression.split(" ");
        String OperandOrOperator = tokens[1];
        double y = Double.parseDouble(OperandOrOperator);
        return y;
    }

    public static boolean QuitFunction(String expression) {
        if (expression.equalsIgnoreCase("quit")) {
            System.out.println("Goodbye!");
            return false;
        } else {
            return true;
        }
    }

<强>输出

Welcome to the AP Computer Science calculator!!

Please use the following format in your expressions: (double)(space)(+,-,*,/...)(space)(double)
or: (symbol)(space)(double)

Enter an expression, or quit to exit: 3 - 2
3.0 - 2.0 = 1.0
Enter an expression, or quit to exit: quit
Goodbye!