0
Will anyone please tell me how to make calculator on java?
Java calculator
4 Answers
0
import java.util.Scanner;
public class Main{
private static Scanner sc;
static int SumAdd(int Number1, int Number2){
return Number1 + Number2;
}
static int SumSub(int Number1, int Number2){
return Number1 - Number2;
}
static int SumMulti(int Number1, int Number2){
return Number1 * Number2;
}
static int SumDiv(int Number1, int Number2){
return Number1 / Number2;
}
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
String UseOpti;
int x = 3;
int UserNum1;
int UserNum2;
System.out.println("Enter your first number: ");
UserNum1 = sc.nextInt();
System.out.println("Enter your second number: ");
UserNum2 = sc.nextInt();
System.out.println("What do you want to do?(addition, substraction...");
UseOpti = sc.next();
switch(UseOpti){
case "addition":
System.out.println(SumAdd(UserNum1, UserNum2));
break;
case "multiplifation":
System.out.println(SumMulti(UserNum1, UserNum2));
break;
case "substraction":
System.out.println(SumSub(UserNum1, UserNum2));
break;
case "Division":
System.out.println(SumDiv(UserNum1, UserNum2));
break;
}
}
}
0
Here my codes a simple calculator, Like and follow.
0
explain these things to me-
UseOpti
switch
0
I made each operation into a seperate class to make it easier to understand and read. Placing each on into diffeerent class allows me to give the code oe opration for each class
static int SumAdd(int Number1, int Number2){ return Number1 + Number2; }
I used the Switch metthod as there was more than 1 outcome of the userinput. this alowes you to make multiple exception or if statment with different consicuences.
switch(UseOpti){ case "addition": System.out.println(SumAdd(UserNum1, UserNum2)); break;
This switch code is saying that if the user option(UserOpti == User Option on what he want to do with their numbers e,g, add, subbtract....) is addition then go to the SumAdd classs with the Users Given inputed Numbers. (UserOpti was jist short for User Option.)