Simple Caulcotr With Java Code Example

Snippet 1

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

} 

Snippet 2

  import java.util.Scanner;
public class Calculator {
   public static void main(String[] args) {
      double num1;
      double num2;
      double ans;
      char op;
      Scanner reader = new Scanner(System.in);
      System.out.print("Enter two numbers: ");
      num1 = reader.nextDouble();
      num2 = reader.nextDouble();
      System.out.print("nEnter an operator (+, -, *, /): ");
      op = reader.next().charAt(0);
      switch(op) {
         case '+': ans = num1 + num2;
            break;
         case '-': ans = num1 - num2;
            break;
         case '*': ans = num1 * num2;
            break;
         case '/': ans = num1 / num2;
            break;
      default: System.out.printf("Error! Enter correct operator");
         return;
      }
      System.out.print("nThe result is given as follows:n");
      System.out.printf(num1 + " " + op + " " + num2 + " = " + ans);
   }
} 

Similar Snippets


Creating Java Main Method Code Example – java

Find Duplicates In Arraylist Java Code Example – java

Java Creat A Folder Code Example – java

Firestore Find Doc And Set Data Code Example – java

Create Copy Of Array From Another Array Code Example – java

Copyright © Code Fetcher 2020

 

 

Published

Leave a comment

Your email address will not be published. Required fields are marked *