Before marking this all duplicate please look at the code as it is done in a different way than in other questions and I would appreciate a fix relating to this code. It is pretty much a calculator that takes two numbers and an operator then prints the final number (and, if applicable, a remainder). I get the errors:
The local variable num3 may not have been initialized
The local variable rem may not have been initialized
Here is the code:
import java.util.Scanner;
public class JCalc {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
int num1;
int num2;
int num3;
int rem;
System.out.println("Welcome to JCalc! The best calculator ever!");
System.out.print("Please enter the first number: ");
num1 = myScanner.nextInt();
System.out.print("Please enter the second number: ");
num2 = myScanner.nextInt();
System.out.print("Please enter an operator (+, -, %, *): ");
String op = myScanner.next();
if (op == "+") {
num3 = num1 + num2;
}
if (op == "-") {
num3 = num1 - num2;
}
if (op == "%") {
num3 = num1 - num2;
rem = num1 % num2;
}
if (op == "*") {
num3 = num1 * num2;
}
System.out.print("The answer is: ");
System.out.print(num3); //error
if (op == "%") {
System.out.print(" with a remainder of ");
System.out.println(rem); //error
}
}
}
The last 2 brackets got a little messed up when I copy pasted them (sorry). Appreciate all the help I can get!