简单的Java计算器

时间:2010-04-29 01:28:01

标签: java calculator

首先,这不是一个家庭作业问题。我正在练习我对java的知识。我认为这样做的一个好方法是在没有帮助的情况下编写一个简单的程序。不幸的是,我的编译器告诉我错误,我不知道如何解决。在不改变逻辑和代码的情况下,有人可以指出我的错误在哪里吗?感谢

import java.lang.*;
import java.util.*;

public class Calculator
{
    private int solution;
    private int x;
    private int y;
    private char operators;

    public Calculator()
    {
        solution = 0;
        Scanner operators = new Scanner(System.in);
        Scanner operands = new Scanner(System.in);
    }

    public int addition(int x, int y)
    {
       return x + y;
    }
    public int subtraction(int x, int y)
    {
       return x - y;
    }
    public int multiplication(int x, int y)
    {    
       return x * y;
    }
    public int division(int x, int y)
    {
       solution = x / y;
       return solution;
    }
    public void main (String[] args)
    {
      System.out.println("What operation? ('+', '-', '*', '/')"); 

      System.out.println("Insert 2 numbers to be subtracted");
       System.out.println("operand 1: ");
       x = operands;
       System.out.println("operand 2: ");
       y = operands.next();
      switch(operators)
      {
          case('+'):
            addition(operands);
            operands.next();
            break;
          case('-'):
            subtraction(operands);
            operands.next();
            break;
          case('*'):
            multiplication(operands);
            operands.next();
            break;
          case('/'):
            division(operands);
            operands.next();
            break;
       }
  }
}

10 个答案:

答案 0 :(得分:4)

operandsoperators超出了主要范围。您在构造函数中声明了局部变量,因此当您退出ctor时,它们就有资格使用GC并且已经消失了。

您有编译错误 - 其中10个。

答案 1 :(得分:3)

package org.com;

import java.lang.*; 
import java.util.*; 

public class Calculator 
{ 
    private int solution; 
    private static int x; 
    private static int y; 
    private char operators; 

    public Calculator() 
    { 
        solution = 0; 
        Scanner operators = new Scanner(System.in); 
        Scanner operands = new Scanner(System.in); 
    } 

    public int addition(int x, int y) 
    { 
       return x + y; 
    } 
    public int subtraction(int x, int y) 
    { 
       return x - y; 
    } 
    public int multiplication(int x, int y) 
    {     
       return x * y; 
    } 
    public int division(int x, int y) 
    { 
       solution = x / y; 
       return solution; 
    } 

    public void calc(int ops){
         x = 4; 
         System.out.println("operand 2: "); 
         y = 5; 

         switch(ops) 
         { 
             case(1): 
               System.out.println(addition(x, y)); 

           //    operands.next(); 
               break; 
             case(2): 
                 System.out.println(subtraction(x, y)); 
              // operands.next(); 
               break; 
             case(3): 
                 System.out.println(multiplication(x, y)); 
             //  operands.next(); 
               break; 
             case(4): 
                 System.out.println(division(x, y));
             //  operands.next(); 
               break; 
          } 
    }
    public static void main (String[] args) 
    { 
      System.out.println("What operation? ('+', '-', '*', '/')");  
      System.out.println(" Enter 1 for Addition");
      System.out.println(" Enter 2 for Subtraction");
      System.out.println(" Enter 3 for Multiplication");
      System.out.println(" Enter 4 for Division");

       Calculator calc = new Calculator();
       calc.calc(1);


  } 
} 

这将有效

答案 2 :(得分:2)

另一个问题是,行

y = operands.next();

正在尝试将StringScanner.next()返回到声明为y类型的变量int

Scanner.nextInt()方法可用于尝试返回int

答案 3 :(得分:2)

package com.abc;

import java.util.Scanner;

public class Calculator {
    private static final String pos = "+";
    private static final String neg = "-";
    private static final String mult = "*";
    private static final String div = "/";

    private enum operation {
        pos, neg, mult, div
    };
    private int solution;
    private int x;
    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    private int y;



    static Scanner operators;

    public Calculator() {
        solution = 0;
        operators = new Scanner(System.in);

    }

    public int addition(int x, int y) {
        return x + y;
    }

    public int subtraction(int x, int y) {
        return x - y;
    }

    public int multiplication(int x, int y) {
        return x * y;
    }

    public int division(int x, int y) {
        solution = x / y;
        return solution;
    }

    public static void main(String[] args) {
        Calculator calc = new Calculator();

        System.out.println("Insert 2 numbers");

        System.out.println("operand 1: ");

        calc.setX(Integer.parseInt(operators.next()));

        System.out.println("operand 2: ");
        calc.setY(Integer.parseInt(operators.next()));

        System.out.println("What operation? ('pos', 'neg', 'mult', 'div')");
        operation ttt = operation.valueOf(operators.next());
        int output = 0 ;
        switch(ttt){
        case pos:
            output = calc.addition(calc.getX(), calc.getY());

            break;
          case neg:
              output = calc.subtraction(calc.getX(), calc.getY());

            break;
          case mult:
              output = calc.multiplication(calc.getX(), calc.getY());

            break;
          case div:
              output = calc.division(calc.getX(), calc.getY());

            break;
        }
        System.out.println("output ="+output);
    }
}

答案 4 :(得分:1)

除了其他答案之外,main()方法必须是静态的才能成为程序入口点。在main()中,您需要构建自己的Calculator对象,并在其上调用方法。

答案 5 :(得分:1)

这一切都很棒,但你用什么程序来编写java? 也许您应该考虑使用像Eclipse这样的IDE,因为它可以自动检测错误并添加导入。 (我不确定你的是否这样做)它还告诉你你的程序的问题是“英语”。 另外,考虑这个类可能是一个更简单,更简单的计算器方法:

public class Calculator {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter an Operator: ");
    String in = sc.next();
    char oper = in.charAt(0);

    System.out.print("Enter a number: ");
    in = sc.next();
    double num1 = Double.parseDouble(in);

    System.out.print("Enter another number: ");
    in = sc.next();
    double num2 = Double.parseDouble(in);

    if(oper == '+') {
        double result = num1 + num2;
        System.out.println(result);
    } else if(oper == '-') {
        double result = num1 - num2;
        System.out.println(result);
    } else if(oper == 'x') {
        double result = num1 * num2;
        System.out.println(result);
    } else if(oper == '/') {
        double result = num1 / num2;
        System.out.println(result);
    } else {
        double result = num1 % num2;
        System.out.println(result);
    }
        System.out.println("Hope this helped your mathmatical troubles!");
}

}
而且作为习惯问题,而不是做:

import java.util.*;

最好这样做:

import java.util.Scanner;

这可能在这里没有多大区别,但是如果你运行一个更大的程序导入整个java.util会大大减慢你的程序。

希望这有帮助!

答案 6 :(得分:0)

您的主要方法需要像这样声明:

public static void main(String[] args) {..}

此外,您似乎只为您的所有算术方法(加法,减法等)提供一个参数,尽管它们需要两个。

public int addition(int x, int y);

无法使用addition(operands)调用,这只是一个参数,且参数类型错误(该方法需要两个int,您给它一个Scanner)。所有这些方法都是如此。您需要从int中提取Scanner s。您可以使用Scanner.nextInt()

执行此操作

答案 7 :(得分:0)

import java.lang.*;

import java.util.*;


public class Calculator
{
    private int solution;
    private int x;
    private int y;
 private char operators;

    public Calculator()
    {
        solution = 0;
        Scanner operators = new Scanner(System.in);
        Scanner operands = new Scanner(System.in);
    }

    public int addition(int x, int y)
    {
       return x + y;
    }
    public int subtraction(int x, int y)
    {
       return x - y;
    }
    public int multiplication(int x, int y)
    {    
       return x * y;
    }
    public int division(int x, int y)
    {
       solution = x / y;
       return solution;
    }
    public void main (String[] args)
    {
      System.out.println("What operation? ('+', '-', '*', '/')"); 

      System.out.println("Insert 2 numbers to be subtracted");
       System.out.println("operand 1: ");
       x = operands;
       System.out.println("operand 2: ");
       y = operands.next();
      switch(operators)
      {
          case('+'):
            addition(operands);
            operands.next();
            break;
          case('-'):
            subtraction(operands);
            operands.next();
            break;
          case('*'):
            multiplication(operands);
            operands.next();
            break;
          case('/'):
            division(operands);
            operands.next();
            break;
       }
  }
}

答案 8 :(得分:-1)

您要求用户输入整数,但您将语句operands.next();作为输入。尝试与变量和用户输入保持一致,因此将其更改为operands.nextInt()会有所帮助。

答案 9 :(得分:-2)

就像提示一样,开始将

import java.util.*;
投入到程序中通常不是一个好主意,因为它会使程序不必要地变大和变慢。你需要的只是
import java.util.Scanner;
如果我是正确的,那么java.lang中的大部分内容都已经为你导入了。