Answers for "making calculator in java"

1

how to make a calculator in java

// this was all coded by me, to use it get Eclipse, or IntelliJ and make a new
// Java Project

package javaCode;

import java.util.Scanner;

public class testCode {

	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Would you like to Add, Subtract, Divide, or Multiply?");
		
		String mathType = scan.next();
		
		if (mathType.equalsIgnoreCase("add")) {
			System.out.println("Whats the first number you want to add?");
			
			int firstNumber = scan.nextInt();
			
			System.out.println(firstNumber + " + ");
			System.out.println("Enter your second number");
			
			int secondNumber = scan.nextInt();
			
			System.out.println(firstNumber + " + " + secondNumber + " =");
			System.out.println(firstNumber + secondNumber);
			
		} else if (mathType.equalsIgnoreCase("subtract")) {
			System.out.println("Whats the first number you want to subtract?");
			
			int firstNumber = scan.nextInt();
			
			System.out.println(firstNumber + " - ");
			System.out.println("Enter your second number");
			
			int secondNumber = scan.nextInt();
			
			System.out.println(firstNumber + " - " + secondNumber + " =");
			System.out.println(firstNumber - secondNumber);
		} else if (mathType.equalsIgnoreCase("divide")) {
			System.out.println("Whats the first number you want to divide?");
			
			int firstNumber = scan.nextInt();
			
			System.out.println(firstNumber + " % ");
			System.out.println("Enter your second number");
			
			int secondNumber = scan.nextInt();
			
			System.out.println(firstNumber + " % " + secondNumber + " =");
			System.out.println(firstNumber / secondNumber);
		} else if (mathType.equalsIgnoreCase("multiply")){
			System.out.println("Whats the first number you want to multiply?");
			
			int firstNumber = scan.nextInt();
			
			System.out.println(firstNumber + " x ");
			System.out.println("Enter your second number");
			
			int secondNumber = scan.nextInt();
			
			System.out.println(firstNumber + " x " + secondNumber + " =");
			System.out.println(firstNumber * secondNumber);
		}
		
	}

}
Posted by: Guest on August-13-2020
0

calculator in java using methods

import java.util.Scanner;


public class Calculator {


    public static void main(String[] args) {

        Scanner kb = new Scanner(System.in);

        System.out.println("Simple Calculator");

        System.out.println("\nHere are your options:");
        System.out.println("\n1. Addition");
        System.out.println("2. Subtraction");
        System.out.println("3. Division");
        System.out.println("4. Multiplication");

        System.out.print("\nWhat would you like to do?: ");
        int choice = kb.nextInt();
        System.out.println();


        if (choice == 1){
            addition();
        }
        else if (choice == 2){
            subtraction();
        }
        else if (choice == 3){
            division();
        }
        else if (choice == 4){
            multiplication();
        }

        System.out.println();
        kb.close();
    }

    public static void addition(){

        int nOne, nTwo;
        Scanner kb = new Scanner(System.in);

        System.out.println("Addition");

        System.out.print("\nFirst Number: ");
        nOne = kb.nextInt();

        System.out.print("\nSecond Number: ");
        nTwo = kb.nextInt();

        kb.close();
        System.out.println("\nSum: " + nOne + " + " + nTwo + " = " + (nOne + nTwo));
    }

    public static void subtraction(){
        int nOne, nTwo;
        Scanner kb = new Scanner(System.in);

        System.out.println("Subtraction");

        System.out.print("\nFirst Number: ");
        nOne = kb.nextInt();

        System.out.print("\nSecond Number: ");
        nTwo = kb.nextInt();

        kb.close();
        System.out.println("\nSum: " + nOne + " - " + nTwo + " = " + (nOne - nTwo));
    }

    public static void division(){
        int nOne, nTwo;
        Scanner kb = new Scanner(System.in);

        System.out.println("Division");

        System.out.print("\nFirst Number: ");
        nOne = kb.nextInt();

        System.out.print("\nSecond Number: ");
        nTwo = kb.nextInt();

        kb.close();
        System.out.println("\nSum: " + nOne + " / " + nTwo + " = " + (nOne / nTwo));
    }

    public static void multiplication(){
        int nOne, nTwo;
        Scanner kb = new Scanner(System.in);

        System.out.println("Multiplication");

        System.out.print("\nFirst Number: ");
        nOne = kb.nextInt();

        System.out.print("\nSecond Number: ");
        nTwo = kb.nextInt();

        kb.close();
        System.out.println("\nSum: " + nOne + " x " + nTwo + " = " + (nOne * nTwo));
    }
}
Posted by: Guest on August-04-2021

Code answers related to "making calculator in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language