Java – use 𞓜 to compress the basic code and operators with strings
I am a novice in Java programming I can't find any information about using | operators and strings I wonder if there is a more efficient way to execute this code, especially if it is still easy to read I try to use a simple calculator to familiarize myself with if then else statements
import java.util.Scanner; public class SimpleCalculator { public static void main(String[] args){ Scanner input=new Scanner(system.in); double first; double second; String option; while(true){ System.out.println("What function would you like to calculate?"); option=input.next(); if(option.equals("add") || option.equals("+")){ System.out.println("First number"); first=input.nextDouble(); System.out.println("Second number"); second=input.nextDouble(); double add=first+second; System.out.println(add); } else if(option.equals("subtract") || option.equals("-")) { System.out.println("First number"); first=input.nextDouble(); System.out.println("Second number"); second=input.nextDouble(); double subtract=first-second; System.out.println(subtract); } else if(option.equals("multiply") ||option.equals("*")) { System.out.println("First number"); first=input.nextDouble(); System.out.println("Second number"); second=input.nextDouble(); double multiply=first*second; System.out.println(multiply); } else if(option.equals("divide") || option.equals("/")) { System.out.println("First number"); first=input.nextDouble(); System.out.println("Second number"); second=input.nextDouble(); double divide=first/second; System.out.println(divide); } else if(option.equals("end")){ System.exit(0); } } } }
In most cases, I wonder if I've tested them if required, but they seem a little clumsy to me However, any criticism will be highly appreciated
Solution
Switch / case statements are a good choice for a series of IFS, and as of Java 7 can use switch statements with strings Pay attention to the grammatical differences between the two Each case ends with a break statement instead of being grouped in curly braces
switch (option) { case "add": case "+": System.out.println("First number"); first=input.nextDouble(); System.out.println("Second number"); second=input.nextDouble(); double add=first+second; System.out.println(add); break; case "subtract": case "-": System.out.println("First number"); first=input.nextDouble(); System.out.println("Second number"); second=input.nextDouble(); double subtract=first-second; System.out.println(subtract); break; case "multiply": case "*": System.out.println("First number"); first=input.nextDouble(); System.out.println("Second number"); second=input.nextDouble(); double multiply=first*second; System.out.println(multiply); break; case "divide": case "/": System.out.println("First number"); first=input.nextDouble(); System.out.println("Second number"); second=input.nextDouble(); double divide=first/second; System.out.println(divide); break; case "end": System.exit(0); }
Then I'll suggest combining duplicate prompt codes If you find yourself copying and pasting code, it's usually a good idea to step back and find out how to avoid repetition Repeated code indicates that you should do some refactoring
if (option.equals("end")) { System.exit(0); } System.out.println("First number"); first=input.nextDouble(); System.out.println("Second number"); second=input.nextDouble(); switch (option) { case "add": case "+": double add=first+second; System.out.println(add); break; case "subtract": case "-": double subtract=first-second; System.out.println(subtract); break; case "multiply": case "*": double multiply=first*second; System.out.println(multiply); break; case "divide": case "/": double divide=first/second; System.out.println(divide); break; }
In addition, you can eliminate duplicate printouts by using a single result variable for all calculations
if (option.equals("end")) { System.exit(0); } System.out.println("First number"); first=input.nextDouble(); System.out.println("Second number"); second=input.nextDouble(); double result; switch (option) { case "add": case "+": result = first + second; break; case "subtract": case "-": result = first - second; break; case "multiply": case "*": result = first * second; break; case "divide": case "/": result = first / second; break; } System.out.println(result);