You are creating a basic calculator where the operator is the input. Your user will input an operator sign defined as a single character: either '+' or '-'. Your task will be to perform the operation on two fixed integers in your program, firstNum and secondNum, being their values 5 and 3 respectively. Name these variables appropriately.
Naming variables
Perform calculator operations on two fixed integers
Report a typo
Fill in the gaps with the relevant elements
::java17
::code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Declare two int variables for the calculation
int = 5;
int = 3;
// Use Scanner to read the operator input
Scanner sc = new Scanner(System.in);
char operator = sc.nextLine().charAt(0);
// Perform the operation based on the operator and print the result
switch (operator) {
case '+':
System.out.println( + );
break;
case '-':
System.out.println( - );
break;
default:
System.out.println("Invalid operator");
}
}
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.