java计算器中的用户定义方法

时间:2015-11-02 16:30:47

标签: java

public static void main(String[]args) {
           @SuppressWarnings("resource")
           Scanner input = new Scanner(System.in);
           double num1,num2,add,num3,difference,multiply,divide,square,cube;
           double power,sqrt,cbrt;
           double sin,cos,tan,abs,log,reciprocal,round,percentage;

           int op;


       System.out.println("MENU");     
       System.out.println(" Arithmetic/Power        Power/Roots         Tangent                  Rounding Off      Miscellaneous ");    
       System.out.println("1. Addition             5. Square            10. Sine            13. Round       16. Percent % ");   
       System.out.println("2. Subtraction          6. Cube       11. Cosine            14. Ceil           17. Absolute ");
       System.out.println("3. Multiplication    7. Power         12. Tanget          15. Floor      18. Log ");
       System.out.println("4. Division          8. Square root  \t                              19. Reciprocal");
       System.out.println("                        9. Cube Root ");
       System.out.println("     ");

      System.out.println(" Press 0 to Exit ");

    while (true ){
        System.out.println("select one option" );
        op =input.nextInt();
    switch (op) {
    case 1:
        System.out.println("enter num1: ");
        num1=input.nextDouble();
        System.out.println("enter num2: ");
        num2=input.nextDouble();
        return add(num1,num2);
        break;
    case 2:
        System.out.println("enter num1: ");
        num1=input.nextDouble();
        System.out.println("enter num2: ");
        num2=input.nextDouble();
        return subtract(num1,num2);
        break;
    case 3:
        System.out.println("enter num1: ");
        num1=input.nextDouble();
        System.out.println("enter num2: ");
        num2=input.nextDouble();
        return multiply(num1,num2);
        break;
    case 4:
        System.out.println("enter num1: ");
        num1=input.nextDouble();
        System.out.println("enter num2: ");
        num2=input.nextDouble();
        return divide(num1,num2);
        break;
    case 5:
     System.out.println("enter num1: ");
    num1=input.nextDouble();
    return square(num1);
    break;

    case 6: 
        System.out.println("enter num1: ");
     num1=input.nextDouble();
    return cube(num1);
    break;

    case 7:
     System.out.println("enter first num: " );
     num1=input.nextDouble();
        System.out.println("enter second num: " );
        num2=input.nextDouble();
      return power(num1,num2);

    case 8:
     System.out.println("enter num1: ");
     num1=input.nextDouble();
    return sqrt(num1);
    break;

    case 9:
     System.out.println("enter num1: ");
     num1=input.nextDouble();
      return cbrt(num1);
       break; 

    case 10:
     System.out.println("enter num1: ");
     num1=input.nextDouble();
      return sin(num1);
      break;

    case 11:
     System.out.println("enter num1: ");
     num1=input.nextDouble();
      return cos(num1);
      break;


    case 12:
     System.out.println("enter num1: ");
     num1=input.nextDouble();
      return tan(num1);
      break;

    case 13:
     System.out.println("enter num1: ");
     num1=input.nextDouble();
      return round(num1);
      break;

    case 14:
     System.out.println("enter num1: ");
     num2=input.nextDouble();
     return ceil(num1); 
     break;

    case 15:
      System.out.println("enter num1: ");
     num1=input.nextDouble();
      return floor(num1);
      break;

    case 16:
     System.out.println("enter first num: " );
        num1=input.nextDouble();
        System.out.println("enter second num: " );
     num2=input.nextDouble();
     return percentage(num1,num2);
        break;

    case 17:
     System.out.println("enter num1: " );
        num1=input.nextDouble();
     return abs(num1);
     break;

    case 18:
     System.out.println("enter num1: " );
        num1=input.nextDouble();
     return log(num1);
         break;
    case 19:
     System.out.println("enter num1: " );
        num1=input.nextDouble();
     return reciprocal;
        break;
    } 
    }

    public static double add(num1,num2){
        return num1+num2;
    }
    public static double subtract(num1,num2) {
        return num1-num2;
    }
    public static double multiply(num1,num2) {
        return num1*num2;
    }
    public static double divide(num1,num2) {
        return num1/num2;
    }
    public static double square(num1) {
        return num1*num1;
    }
    public static double cube(num1) {
        return num1*num1*num1;
    }
    public static double power(num1,num2) {
        return Math.pow(num1, num2);
    }
    public static double sqrt(num1) {
        return Math.sqrt(num1);
    }
    public static double cbrt(num1) {
        return Math.cbrt;
    }
    public static double sin(num1) {
        return Math.sin(num1);
    }
    public static double cos(num1) {
        return Math.cos(num1);
    }
    public static double tan(num1) {
        return Math.tan(num1);
    }
    public static double round(num1) {
        return Math.round(num1);
    }
    public static double ceil(num1) {
        return Math.ceil(num1);
    }
    public static double floor(num1) {
        return Math.floor(num1);
    }
    public static double percentage(num1,num2) {
        return (num1*100/num2);
    }
    public static double abs(num1) {
        return Math.abs(num1);
    }
    public static double log(num1) {
        return Math.log10(num1);
    }
    public static double reciprocal(num1) {
        return (1/num1);
    }


}
}

我收到以下错误:

The method add(double, double) is undefined for the type calculator
The method subtract(double, double) is undefined for the type calculator
The method multiply(double, double) is undefined for the type calculator
The method divide(double, double) is undefined for the type calculator
The method square(double) is undefined for the type calculator

有人可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:1)

  case 19:
     System.out.println("enter num1: " );
        num1=input.nextDouble();
     return reciprocal;
        break;
    } 
    }

上面代码中的最后一个结束括号似乎是关闭类声明。删除它并将其放在所有方法定义的末尾。这应该有用。