I have a method, public static int payMethod
, with parameter input
for Scanner
to get, well, input. Obviously, I could remove the method entirely and put everything in the main
method, but I'm trying to learn about using different types of methods so that isn't really an option.
My goal is for the user to input 1, 2, or 3. If they input something else, then the program asks again, until the user inputs 1, 2, or 3.
import java.util.*;
public class test {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
//User input on selection
System.out.println("Choose 1, 2, or 3.");
double choice;
//Repeat until payment selection is valid choice
choice = choiceMethod(input);
input.close(); //Close Scanner
}
public static int choiceMethod (Scanner input) {
do {
System.out.print("Which choice would you like? ");
if (input.nextDouble() < 1 || input.nextDouble() > 3 || input.nextDouble() != (int)input.nextDouble()) {
System.out.println("This is not a valid option. Please choose either option 1, 2, or 3.");
}
} while (input.nextDouble() < 1 || input.nextDouble() > 3 || input.nextDouble() != (int)input.nextDouble());
return (int)input.nextDouble();
}
}
Here is my wonky input/output.
Choose 1, 2, or 3.
Which choice would you like? 1
2
3
4
This is not a valid option. Please choose either option 1, 2, or 3.
5
1
1
2
Which choice would you like?
2
3
4
5
This is not a valid option. Please choose either option 1, 2, or 3.
I've tried changing parameters to no avail, so I'm sure the problem is something within the method. Help is appreciated, TIA.