swoly
swoly

Reputation: 19

gives user a 2nd chance to enter the right number of "99"

//Want the user to have a repeating chance to enter the correct number im looking for which is 99

package importingclasscode; import java.util.Scanner;

public class Firstclass {

public static void main(String[] args) {

    Scanner myScan = new Scanner (System.in);
    System.out.println("What is you rs level ? ");
    int level = myScan.nextInt();

    System.out.println("What is your range level? ");

    int rngLvl = myScan.nextInt();

    if (rngLvl!=99)
         rngLvl = myScan.nextInt();

        System.out.println("Range level is too low");
    else 
        myScan.hasNextInt();

        System.out.println(" You comabt level is " + level + " and your range level is " + rngLvl ); 
    }
        }

Upvotes: 0

Views: 68

Answers (5)

parthlr
parthlr

Reputation: 426

For this, you want to use a while loop that loops when rngLevel is not 99. For example:

while (rngLevel != 99) {
   // Ask for user input again
}

If you want to give them a certain number of tries before they fail, you could have a counter that keeps track of how many times the user has tried an input. Example:

int numberOfTries = 0;
while (rngLevel != 99 && numberOfTries < 2) {
   // Ask for user input again
   numberOfTries++;
}

In this case, the user is given two tries before they are no longer prompted for input again.

If you're confused about how while loops work, GeeksforGeeks has a great resource.

Upvotes: 0

Midoen
Midoen

Reputation: 46

If you are looking for 2 tries only, then you can use if statements only, no need for loops:

int rngLvl = myScan.nextInt();
if (rngLvl != 99) {
   rngLvl = myScan.nextInt();
   if (rngLvl == 99) {
      correct();
   } else {
      // The error
   }
} else { correct(); }

Outside the main function:

public void correct() {
    // Write your success code
}

You are simply asking the user to type an Integer value, check the result if it's 99, you go to correct if it's wrong, then you ask the user to type it again if it's 99 you go to correct, otherwise, you'll do your error code.

Upvotes: 0

user13500072
user13500072

Reputation:

enmmm, first you need a thing looks like this {} for if statement since if you don't, the if statement is only going to include the next line so: System.out.println("Range level is too low"); will always be printed then...

    public static void main(String[] args) {

        Scanner myScan = new Scanner (System.in);
        System.out.println("What is you rs level ? ");
        int level = myScan.nextInt();

        System.out.println("What is your range level? ");

        int rngLvl = myScan.nextInt();

        if (rngLvl!=99){
             rngLvl = myScan.nextInt();

            System.out.println("Range level is too low");
}
System.out.println("Please enter again");
rngLvl = myScan.nextInt();
             if (rngLvl!=99){
             rngLvl = myScan.nextInt();

            System.out.println("Range level is too low");
}
        else {
            myScan.hasNextInt();

            System.out.println(" You comabt level is " + level + " and your range level is " + rngLvl ); 
}
 }

No while needed, since you just said a second chance....

Upvotes: 0

VHS
VHS

Reputation: 10194

If you want to offer your user multiple chances to enter the correct input of "99", you need to use a loop such as while instead of an if statement.

while (rngLvl!=99) {
     System.out.println("Range level is too low");
     if(myScan.hasNextInt()) {
            rngLvl = myScan.nextInt();
     }
     else {
          break;
     }
  }
  System.out.println(" You comabt level is " + level + " and your range level is " + rngLvl ); 
}

Upvotes: 1

infamed
infamed

Reputation: 404

You will want to utilize a do while loop for this. In the while loop parameter, make the argument check to see if the value is not 99. In the while loop, prompt the user for their answer. If it isn’t correct, will continue looping and prompting the user. If it is 99, the loop will end.

Upvotes: 0

Related Questions