2

Main Class

import java.util.Scanner;

class Calculator
{
    public static void main(String args[])
    {
     Scanner input = new Scanner(System.in);
     Addition objecta = new Addition ();
     Multiplacation objectmu = new Multiplacation();
     Division objectd = new Division();
     Minus objectmi = new Minus();

     System.out.println("Enter operation");
     System.out.println("1.Addition");
     System.out.println("2.Multiplacation");
     System.out.println("3.Division");
     System.out.println("4.Subtraction");

     input.nextLine();
     int test1 = 1;
     int test2 = 2;
     int test3 = 3;
     int test4 = 4;
     if (test1 == 1)
    {
     Addition plus = new Addition();
     plus.add();
    } 
    if (test2 == 2)
    {
     Multiplacation multi = new Multiplacation();
     multi.multiply();
    }
    if (test3 == 3)
    {
     Division div = new Division();
     div.divide();
    }
    if (test4 == 4)
    {
     Minus mi = new Minus();
     mi.subtract();
    }
    }
}

Classes

import java.util.Scanner;

class Addition
{
    public static void add()
    {
        Scanner bob = new Scanner(System.in);
        double fnum, snum, answer;
        System.out.println("Eneter First Number");
        fnum = bob.nextDouble();
        System.out.println("Eneter Second Number");
        snum = bob.nextDouble();
        answer = fnum + snum;
        System.out.println(answer);
    }
}

import java.util.Scanner;

class Multiplacation
{
        public static void multiply()
    {
        Scanner bob = new Scanner(System.in);
        double fnum, snum, answer;
        System.out.println("Eneter First Number");
        fnum = bob.nextDouble();
        System.out.println("Eneter Second Number");
        snum = bob.nextDouble();
        answer = fnum * snum;
        System.out.println(answer);
    }
}

import java.util.Scanner;

class Division
{
        public static void divide()
    {
        Scanner bob = new Scanner(System.in);
        double fnum, snum, answer;
        System.out.println("Eneter First Number");
        fnum = bob.nextDouble();
        System.out.println("Eneter Second Number");
        snum = bob.nextDouble();
        answer = fnum / snum;
        System.out.println(answer);
    }
}

import java.util.Scanner;

class Minus
{
        public static void subtract()
    {
        Scanner bob = new Scanner(System.in);
        double fnum, snum, answer;
        System.out.println("Eneter First Number");
        fnum = bob.nextDouble();
        System.out.println("Eneter Second Number");
        snum = bob.nextDouble();
        answer = fnum - snum;
        System.out.println(answer);
    }
}

Question

I am making a calculator and I am new to computer programming. I got the program to work but the only problem is that I cannot get the program to stop. It answers one problem then starts another one continuously. Any ideas how to stop it please comment.

Markaroni
  • 21
  • 2
  • 1
    Note: You shouldn't create multiple `Scanners` using `System.in`, you should instead pass the `Scanner` as a parameter to your classes functions. [Here is why.](http://stackoverflow.com/questions/19766566/java-multiple-scanners) – Jonny Henly Apr 21 '16 at 04:47
  • change `input.nextLine();` to `int test = input.nextInt();` and change every condition to `if(test == 1)`, `if(test == 2)`, etc, instead of using multiple variables like `test1, test2, test3 and test4` in `if`, `if(test1 == 1)`. – SatyaTNV Apr 21 '16 at 04:47
  • why are you creating 4 different variable `test1,test2,test3,test4` ? Its not required – Rahman Apr 21 '16 at 04:48

4 Answers4

3

I think you need to understand how to accept input from the user.

In your code, this is wrong:

 input.nextLine();
 int test1 = 1;
 int test2 = 2;
 int test3 = 3;
 int test4 = 4;

Because later on, you check whether test1 is 1, test2 is 2 etc. Since you did not change the values of these variables, your conditions will always evaluate to true.

The correct way to do it is this:

int userInput = input.nextInt();

and then check the userInput variable:

if (userInput == 1) {
    ...
}

if (userInput == 2) {
    ...
}

etc

Tip: in this situation you should use else ifs:

if (userInput == 1) {
    ...
} else if (userInput == 2) {
    ...
} else if (userInput == 3) {
    ...
} else if (userInput == 4) {
    ...
}

Other improvements:

You should not create multiple scanners, one is enough.

You can declare a scanner in the Calculator class:

class Calculator {
    public static Scanner input = new Scanner(System.in);

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

In your other classes, use Calculator.input instead of creating a new scanner. I'll give you an example:

class Addition
{
    public static void add()
    {
        double fnum, snum, answer;
        System.out.println("Eneter First Number");
        fnum = Calculator.input.nextDouble();
        System.out.println("Eneter Second Number");
        snum = Calculator.input.nextDouble();
        answer = fnum + snum;
        System.out.println(answer);
    }
}
Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

This logic

if (test1 == 1)

and

if (test2 == 2)

is always going to be true.

You should be comparing

int test = input.nextInt();

if (test == test1) {  // etc
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

You need to check the input given by the user, and since that is only one
if/if-else is the best approach to do...

if (userInput == 1) {
     Addition plus = new Addition();
     plus.add();
} else if (userInput == 2) {
    ...your code 
} else if (userInput == 3) {
    ......your code 
} else if (userInput == 4) {
    ......your code 
}

here when math is done, you should ask again the user for another input... and one scanner object is more than ok, you dont need to define multiple ones....

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You can change your calculator class like this mentioned below so that it asks for an option when it completes execution. In similar situations you can use switch case instead of if conditions. Add an exit option to list of options to terminate program when ever user required it.

    class Calculator
{
    public static void main(String args[])
    {
     Scanner input = new Scanner(System.in);
     Addition objecta = new Addition ();
     Multiplacation objectmu = new Multiplacation();
     Division objectd = new Division();
     Minus objectmi = new Minus();

     while(true){

     System.out.println("Enter operation");
     System.out.println("1.Addition");
     System.out.println("2.Multiplacation");
     System.out.println("3.Division");
     System.out.println("4.Subtraction");
        System.out.println("5.Exit");


     switch(input.nextLine()){
         case "1":
             Addition plus = new Addition();
             plus.add();
             break;
         case "2":
             Multiplacation multi = new Multiplacation();
             multi.multiply();
             break;
         case "3":
             Division div = new Division();
             div.divide();
             break;
         case "4":
             Minus mi = new Minus();
             mi.subtract();
             break;
         case "5":
             System.exit(0);
         default:
             System.out.println("Please enter valid option");

     }
     }
     /*input.nextLine();
     int test1 = 1;
     int test2 = 2;
     int test3 = 3;
     int test4 = 4;
     if (test1 == 1)
    {
     Addition plus = new Addition();
     plus.add();
    } 
    if (test2 == 2)
    {
     Multiplacation multi = new Multiplacation();
     multi.multiply();
    }
    if (test3 == 3)
    {
     Division div = new Division();
     div.divide();
    }
    if (test4 == 4)
    {
     Minus mi = new Minus();
     mi.subtract();
    }*/
    }
}
BhanuReddy
  • 74
  • 10