calculator in java using methods

Solutions on MaxInterview for calculator in java using methods by the best coders in the world

showing results for - "calculator in java using methods"
Syrine
20 Jan 2020
1import java.util.Scanner;
2
3
4public class Calculator {
5
6
7    public static void main(String[] args) {
8
9        Scanner kb = new Scanner(System.in);
10
11        System.out.println("Simple Calculator");
12
13        System.out.println("\nHere are your options:");
14        System.out.println("\n1. Addition");
15        System.out.println("2. Subtraction");
16        System.out.println("3. Division");
17        System.out.println("4. Multiplication");
18
19        System.out.print("\nWhat would you like to do?: ");
20        int choice = kb.nextInt();
21        System.out.println();
22
23
24        if (choice == 1){
25            addition();
26        }
27        else if (choice == 2){
28            subtraction();
29        }
30        else if (choice == 3){
31            division();
32        }
33        else if (choice == 4){
34            multiplication();
35        }
36
37        System.out.println();
38        kb.close();
39    }
40
41    public static void addition(){
42
43        int nOne, nTwo;
44        Scanner kb = new Scanner(System.in);
45
46        System.out.println("Addition");
47
48        System.out.print("\nFirst Number: ");
49        nOne = kb.nextInt();
50
51        System.out.print("\nSecond Number: ");
52        nTwo = kb.nextInt();
53
54        kb.close();
55        System.out.println("\nSum: " + nOne + " + " + nTwo + " = " + (nOne + nTwo));
56    }
57
58    public static void subtraction(){
59        int nOne, nTwo;
60        Scanner kb = new Scanner(System.in);
61
62        System.out.println("Subtraction");
63
64        System.out.print("\nFirst Number: ");
65        nOne = kb.nextInt();
66
67        System.out.print("\nSecond Number: ");
68        nTwo = kb.nextInt();
69
70        kb.close();
71        System.out.println("\nSum: " + nOne + " - " + nTwo + " = " + (nOne - nTwo));
72    }
73
74    public static void division(){
75        int nOne, nTwo;
76        Scanner kb = new Scanner(System.in);
77
78        System.out.println("Division");
79
80        System.out.print("\nFirst Number: ");
81        nOne = kb.nextInt();
82
83        System.out.print("\nSecond Number: ");
84        nTwo = kb.nextInt();
85
86        kb.close();
87        System.out.println("\nSum: " + nOne + " / " + nTwo + " = " + (nOne / nTwo));
88    }
89
90    public static void multiplication(){
91        int nOne, nTwo;
92        Scanner kb = new Scanner(System.in);
93
94        System.out.println("Multiplication");
95
96        System.out.print("\nFirst Number: ");
97        nOne = kb.nextInt();
98
99        System.out.print("\nSecond Number: ");
100        nTwo = kb.nextInt();
101
102        kb.close();
103        System.out.println("\nSum: " + nOne + " x " + nTwo + " = " + (nOne * nTwo));
104    }
105}