使用户友好的基本计算器java

时间:2016-05-04 22:43:29

标签: java

我差不多完成了我的程序,但我需要让我的代码更加用户友好。

我怎样才能让它只要求用户输入操作,只需要一个数字来说明日志,cos,sin,tan和square root?到目前为止,它要求用户输入两位数字和操作,但第一位数字无关紧要。

package calculator2;
import java.util.Scanner;
public class Calculator2 {
    public static void main(String[] args) {
        System.out.println("Addition");
        System.out.println("Subtraction");
        System.out.println("Division");
        System.out.println("Multiplication");
        System.out.println("Natural Log");
        System.out.println("Exponent");
        System.out.println("cos");
        System.out.println("sin");
        System.out.println("tan");
        System.out.println("root");
        System.out.println("pactorial");
        System.out.println("compitation");
        System.out.println("-------------------------");

        Scanner scan = new Scanner(System.in);
        String userInput ;

       do{
           calculator();
           System.out.println("Would you like to do another calculation. Enter Y for yes or N for no");
            userInput = scan.nextLine();
       }while (userInput.equalsIgnoreCase("y"));
        System.out.println("Goodbye");

        }

    public static void calculator(){

        Scanner user = new Scanner(System.in);

        System.out.println("Enter a calculation. With a space in between digit and opertation. ");
        System.out.println("Like 1 + 1 . For log, first press 0, then operator 'log' follow by digit you wish to calculate.");

        double value1 = user.nextDouble();
        String op = user.next();
        double value2 = user.nextDouble();

         if (op.equals("+")){
             Addition(value1,value2);

         }if (op.equals("-")){
             Subtraction(value1,value2);

         }if(op.equals("/")){
             Division(value1,value2);

         }if(op.equals("*")){
             Multiplication(value1,value2);

         }if(op.equals("^")){
             Exponent(value1,value2);

         }if(op.equals("log")){
             NaturalLog(value1);

         }if(op.equals("cos")){
             cos(value2);

         }if(op.equals("sin")){
             sin(value2);
         }if(op.equals("tan")){
             tan(value2);

         }if(op.equals("root")){
             root(value2);
         }if(op.equals("patory")){
             patory(value1, value2);
         }if(op.equals("comb")){
             comb(value1 , value2);
         }else{
         }
    }
    public static void Addition (double value1, double value2){
        double Sum= value1 + value2;
        System.out.println(Sum);
    }
    public static void Subtraction (double value1 , double value2){
        double diff = value1 - value2;
        System.out.println(diff);
    }
    public static void Division(double value1 , double value2){
        double div = value1 / value2;
        System.out.println(div);
    }
    public static void Multiplication(double value1, double value2){
       double mult =  value1 * value2;
        System.out.println(mult);
    }
    public static void Exponent(double value1 , double value2){
        double exp =  Math.pow(value1, value2);
        System.out.println(exp);
    }
    public static void NaturalLog(double value2){
        double logs = Math.log(value2);
        System.out.println(logs);
    }
      public static void cos(double value2){
          double cas = Math.cos(value2);
          System.out.println(cas);
      }
      public static void sin(double value2){
          double sins = Math.sin(value2);
          System.out.println(sins);

      }
      public static void tan(double value2){
          double tans = Math.tan(value2);
          System.out.println(tans);
      }
     public static void root(double value2){
         double sqroot = Math.sqrt(value2);
         System.out.println(sqroot);

     } 
     public static void patory(double value1 , double value2){
         double denominator =1;
         double numarator = 1;
         for(double i = value1; i>=1; i--){
             numarator *= i;

         }
         for(double j = value1 - value2; j>= 1; j--){
             denominator *= j;
         }
         System.out.println(numarator/denominator);

     }
     public static void comb( double value1, double value2){
         double denominator1 = 1;
         double denominator2 = 1;
         double numarator = 1;

         for(double i =value1; i >=1; i--){
             numarator *= i;

         }
         for( double j = value2; j>=1; j--){
             denominator1 *= j;
         }
         for(double j = value1 - value2; j>=1; j--){
             denominator2 *= j;

         }
         System.out.println(numarator/(denominator1*denominator2));
     }
}

2 个答案:

答案 0 :(得分:0)

需要添加一个简单的条件。

if(function ==log||function == sin||etc.) 
//Something like this in conditional
{
 value2 = user.nextDouble(); 
 //get user input only once for special cases that satisfy conditional
}
else
{
  value1 = user.nextDouble(); //get user input normally.
  String op = user.next();
  value2 = user.nextDouble();
}

<强> LOGIC 如果函数不是那些特殊函数之一,那么请求2个数字并获得操作...否则,如果满足条件,因为用户想要正弦切线等...然后只扫描一个数字。

答案 1 :(得分:0)

您应该拆分代码以支持两种情况:一元函数(如root / log / cos / etc)和双函数(如:+ - / *)。

所以我会将calculator()的代码修改为:

public static void calculator() {

    Scanner user = new Scanner(System.in);

    System.out.println("Enter a calculation. With a space in between digit and opertation. ");
    System.out.println("Like 1 + 1 . For log, first press 0, then operator 'log' follow by digit you wish to calculate.");

    String command = user.nextLine();
    String[] ops = command.split("\\s+");
    if (ops.length == 2) {
        biFunction(ops);
    } else {
        unaryFunction(ops);
    }
}

分别实施biFunction(ops)unaryFunction(ops)