r/learnjava • u/CouchResident • 17d ago
been learning on mooc.fi, is so far on part 2 of Java 1, is there a better way for this?
import java.util.Scanner;
class Main {
public static void main(String[] args) {
// System.out.println("Try clicking the Run button.");
Scanner scanner = new Scanner(System.in);
System.out.println("What is your name?");
String name = scanner.nextLine();
System.out.println("Hello " + name + " ,lets begin using this calculator ");
System.out.print("Enter your first number:");
int first = Integer.valueOf(scanner.nextLine());
System.out.print("Enter your second number:");
int second = Integer.valueOf(scanner.nextLine());
System.out.println("would you like to add, subtract, multiply or divide these numbers?");
String math = scanner.nextLine();
if (math.equals("add")) {
System.out.println("Result: " + (first + second));
}
if (math.equals("subtract")) {
System.out.println("Result: " + (first - second));
}
if (math.equals("multiply")) {
System.out.println("Result: " + (first * second));
}
if (math.equals("divide")) {
System.out.println("Result: " + (first / second));
}
}
}