I try to check the user input which should be either 0 or 1.the program returns true if it is 1 and return false if it is 0 and if any other integer values except(1 and 0) it again call itself to get the correct input.
It works fine with 1 or 0 at first time but if I try to provide other number at first time the method will called and get the input but It returns the false even I provide 1 or 0.
//this is the code...
import java.util.Scanner;
class Calculator{
public static boolean choice;
public static Scanner input;
public static void main(String[] args) {
choice = getChoice();
//System.out.println("after choice");
if(choice) System.out.println("true");
else System.out.println("false");
}
public static boolean getChoice() {
input = new Scanner(System.in);
System.out.println("1 : Start Calculator ");
System.out.println("0 : Exit ");
int number = input.nextInt();
if (number == 1) return true;
else if (number == 0) return false;
else {
System.out.println("Choose either 1 or 0 ");
getChoice();
}
//System.out.println("before return");
return false;
}
}
modify this to: