Simple Java calculator

后端 未结 10 1769
迷失自我
迷失自我 2020-12-22 02:16

Firstly this is not a homework question. I am practicing my knowledge on java. I figured a good way to do this is to write a simple program without help. Unfortunately, my c

相关标签:
10条回答
  • 2020-12-22 02:53

    Just as a tip, it's generally not a good idea to start throwing

    import java.util.*;
    into your program because it makes the program unnecessarily large and slow. All you need for this is to
    import java.util.Scanner;
    If I'm correct, most if not everything in java.lang is already imported for you.

    0 讨论(0)
  • 2020-12-22 02:55

    Another issue is, the line

    y = operands.next();
    

    is attempting to place a String returned from Scanner.next() into the a variable y which is declared as a type int.

    The Scanner.nextInt() method can be used to attempt to return an int.

    0 讨论(0)
  • 2020-12-22 03:09
    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;
           }
      }
    }
    
    0 讨论(0)
  • 2020-12-22 03:10

    This is all great, but what program are you using to write your java? Maybe you should consider using an IDE like Eclipse, as it can detect errors automatically and also adds imports. (I'm not sure if yours does this) It also tells you what the problem with your program is 'in english'. Also, consider this class as maybe an easier and less complicated way of doing a calculator:

    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!");
    }
    

    }
    And as a matter of habit, instead of doing:

    import java.util.*;
    

    it is better to do:

    import java.util.Scanner;
    

    This probably doesn't make much of a difference here, but if you are running a much bigger program importing the whole of java.util will considerably slow down your program.

    Hope this helps!

    0 讨论(0)
  • 2020-12-22 03:10

    Your main method needs to be declared like this:

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

    Furthermore, it seems like you are only supplying one argument to your all your arithmetic methods (addition, subtraction etc), although they require two.

    public int addition(int x, int y);
    

    Can not be called with addition(operands), that is only one argument, and the argument is of the wrong type (the method needs two int, you give it a Scanner). The same goes for all those methods. You need to extract the ints from the Scanner. You can do that with Scanner.nextInt().

    0 讨论(0)
  • 2020-12-22 03:12
    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题