How to limit input integers from 1 to 9

I have a Java program to play the playoffs, I want to limit b1, b2, b3 and s1 to any number only from 1-9. If the input is not 1-9, try again.

 for (int i=0;i<=5;i++)

        {

            int b1, b2, b3, s1;
            int j=i+1;
            System.out.println("Enter the Numbers which you will block.... Please remember that it should be from 1-9 and nothingelse, 1 2 3 4 5 6 7 8 9");


            b1=Integer.parseInt(br.readLine()); // how to restrict this to numbers between 1 to 9
            b2=Integer.parseInt(br.readLine()); //this also
            b3=Integer.parseInt(br.readLine()); //this also

            System.out.flush();
            System.out.println("Enter The Number where you will score");
            s1=Integer.parseInt(br.readLine()); //this also

      

+3


source to share


5 answers


while(b1 > 9 || b1 < 1) {
    System.out.println("Wrong number. Try again."); 
    b1=Integer.parseInt(br.readLine());
}

      



0


source


pseudocode:

if ((input > 9) || (input < 1)) {
    tryAgain()
}

      



I think that's all you need (you have to check every variable though).

0


source


Try this dynamic way,

public class testJava {
   public static void main(String[] args) {
       testJava j = new testJava();
       System.out.println(j.isBetween(1,1,9));

   }
    public boolean isNumeric(int value,int startValue,int endValue) {
        if(value >= startValue || value <= endValue)
        {
            return true;
        }else{
           return false;
        }
    }
}

      

0


source


Just check your input.

If (number >= 1 && number <= 9)
{
 progressFurther();
}
else
{
 enterAgain();
 checkAgain();
}

      

0


source


Just use a simple condition like:

if (x >= 1 && x <= 9) {
    // handle the correct case
} else {
    // handle the case where the input is not in the range 1-9
}

      

0


source







All Articles