0

I am trying to have an input asking me to select a number 1-99, I want too have this system.close(0) after 2 failed attempts, if anyone could help me it would be appreciated.

package joey;

import java.util.Scanner;

public class Joey {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        while (true)   { 
            System.out.println("Please enter an number between 0-99: ");
            int n = input.nextInt();
            if ((n > 99) || (n < 1))
                System.out.println("Invalid number");
            else
                break;          
       }
    }
}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Big B
  • 3
  • 1
  • Take a look at this: https://stackoverflow.com/questions/2912817/how-to-use-scanner-to-accept-only-valid-int-as-input – clinomaniac Feb 20 '18 at 22:20
  • Have you tried implementing the feature? What problem are you experiencing? – 4castle Feb 20 '18 at 22:21
  • I am not having any issues, I am just not too good with java and don't know how I would go about it. – Big B Feb 20 '18 at 22:22
  • So, how do you think such code should work? Can you describe general steps? What happens when you try implementing such solution? Are you facing any *specific* problem? – Pshemo Feb 20 '18 at 22:23
  • Basically I just want it to ask me to select a number 1-99 and if I chose a number that isn't between 1-99 two times in a row, it will system.exit(0) – Big B Feb 20 '18 at 22:24

3 Answers3

1

In order to have ''this'' system.close, you need just to finish running the program manually - aka exit the loop. So A. Check you number. B. Set a Boolean flag and make the while loop itirate on that flag. When it'll change state the loop will over and the computer will back to it's state.

Vitali Pom
  • 602
  • 1
  • 8
  • 29
1

You need to create a flag variable... something like:

int counter = 0;

Then change your while (true) { loop to:

do {
    //Do whatever you want here
    counter++; //Don't forget to increase the counter or you'll end up with an infinite loop
    //If number is in range 1-99 then incorrect = false otherwise it becomes true
} while (counter < 2 && incorrect);

if (incorrect) {
    System.out.println("Good bye! You failed!");
} else {
    System.out.println("Good bye! You approved!");
}

There is no need for a System.exit(0); call

Or perhaps you could use a do-while loop but I'm leaving that up to you

Frakcool
  • 10,915
  • 9
  • 50
  • 89
1

Use a counter for the failed attempts (code updated):

import java.util.Scanner;
class Main {
    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int failed = 0;

        while (failed < 2) {
            System.out.println("Please enter an number between 0-99: ");
            int n = input.nextInt();
            if ((n > 99) || (n < 1)) {
                System.out.println("Invalid number");
                failed += 1;
            } else {
                System.out.println("Correct! Closing now");
                return;
            }
        }        
        System.out.println("Finished after two failed attempts");
    }
}

See it working here

Victor M Perez
  • 2,185
  • 3
  • 19
  • 22
  • Thank you, this has worked for me, Is their anyway to have it so when I do type a number that IS 1-99 it'll output something like "Correct! Closing now" and closes. – Big B Feb 20 '18 at 22:34