I looked at some other similar posts but was not able to figure out the solution, hence looking for your valuable input, thanks in advance.
What I want: You will be asked to enter a sign (+ , -, /) and the output will display "You entered minus" (if your input was -)
Class that takes the input
import java.util.Scanner;
public class TakeSign {
Scanner userInput = new Scanner(System.in); /*Create an object of scanner class*/
public char mySign() {
System.out.print("Enter a sign: ");
char input2 = userInput.next().charAt(0);
return input2;
}
}
Main Class
public class Main {
public static void main (String[]args) {
TakeSign ts = new TakeSign();
if (ts.mySign() == '+') {
System.out.println("You entered plus");
}
else if (ts.mySign() == '-') {
System.out.println("You entered minus");
}
else if (ts.mySign() == '/') {
System.out.println("You entered division");
}
}
}
Problem
If my 1st input is / (division) I get asked 3 times. I was expecting to be asked just 1 time.
Enter a sign: /
Enter a sign: /
Enter a sign: /
You entered division
I think the problem is in the loop which I probably did not write correctly. Can you please point me to right direction?