开发者

Simple Java calculator

开发者 https://www.devze.com 2022-12-28 08:03 出处:网络
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 compiler is telling me error

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 compiler is telling me errors I don't know how to fix. Without changing much logic and code, could someone kindly point out where some of my errors are? Thanks

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();
   开发者_StackOverflow社区         break;
          case('-'):
            subtraction(operands);
            operands.next();
            break;
          case('*'):
            multiplication(operands);
            operands.next();
            break;
          case('/'):
            division(operands);
            operands.next();
            break;
       }
  }
}


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);


  } 
} 

This will work


operands and operators are out of scope for main. You declare local variables in the constructor, so when you exit the ctor they're eligible for GC and gone.

You have compilation errors - 10 of them.


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.


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


In addition to the other answers, your main() method must be static in order to be a program entry point. In main() you will need to construct your own Calculator object, and call methods on that.


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!


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().


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;
       }
  }
}


You are asking for the user to type in integers, but you put the statement operands.next(); as the input. Try to keep consistent with your variables and user input, so changing it to operands.nextInt() would help.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号