SuperUser
SuperUser

Reputation: 45

How to loop this simple program?

How do i loop this WHOLE program?

public class Experiments {
    public static void main(String[] args) {
        int n;

        System.out.print("Please enter a number:");
        n = TextIO.getlnInt();

        while (n<3) {
            System.out.print("Please enter another number:");
            n = TextIO.getlnInt();
        }
        while (n<=5) {
            System.out.println("Number entered should be more than 5.");
            System.out.println("Please re-enter:");
            n=TextIO.getlnInt();
        }
        if (n%2==0) {
            n=n*2+1;
        }
        else {
            n=n*2;
        }
        System.out.println("The result is "+n +".");
    }
}

When it prints out the result:

The result is 14.

I want it to say something like:

Had fun? Lets do it again.
Please enter a number:

or

Had fun? Want do it again. Press y/n.
Please enter a number:

Upvotes: 1

Views: 122

Answers (5)

Kenny Tai Huynh
Kenny Tai Huynh

Reputation: 1599

You can use recursive. Sample like this:

public class Experiments {
    public static void main(String[] args) {
        guess();
    }

    public static void guess() {
        int n;

        System.out.print("Please enter a number:");
        n = TextIO.getlnInt();

        while (n<3) {
            System.out.print("Please enter another number:");
            n = TextIO.getlnInt();
        }
        while (n<=5) {
            System.out.println("Number entered should be more than 5.");
            System.out.println("Please re-enter:");
            n=TextIO.getlnInt();
        }
        if (n%2==0) {
            n=n*2+1;
        }
        else {
            n=n*2;
        }
        System.out.println("The result is "+n +".");
        System.out.println("Had fun? Lets do it again.");
        guess();
    }
}

Upvotes: 0

Piyush Mittal
Piyush Mittal

Reputation: 1890

public class Experiments {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
char c;
    int n;
do{
    System.out.print("Please enter a number:");
    n = TextIO.getlnInt();

    while (n<3) {
        System.out.print("Please enter another number:");
        n = TextIO.getlnInt();
    }
    while (n<=5) {
        System.out.println("Number entered should be more than 5.");
        System.out.println("Please re-enter:");
        n=TextIO.getlnInt();
    }
    if (n%2==0) {
        n=n*2+1;
    }
    else {
        n=n*2;
    }
    System.out.println("The result is "+n +".");
    System.out.println("Had fun? press y do it again n to exit");
     c = s.next().charAt(0);
    } while(c == 'y');

}

credit goes to @wawek

Upvotes: 1

Nagappan
Nagappan

Reputation: 356

Just read user next line character for yes and then run the whole sequence as shwon below.

public class Experiments {

    public static void main(String[] args) {
        int n;

        while(againFlag=='y'){ 
        System.out.print("Please enter a number:");
        n = TextIO.getlnInt();

        while (n<3) {
            System.out.print("Please enter another number:");
            n = TextIO.getlnInt();
        }
        while (n<=5) {
            System.out.println("Number entered should be more than 5.");
            System.out.println("Please re-enter:");
            n=TextIO.getlnInt();
        }
        if (n%2==0) {
            n=n*2+1;
        }
        else {
            n=n*2;
        }
        System.out.println("The result is "+n +".");
        System.out.println("Had fun. Want do it again?(Press 'y')");
        char againFlag = TextIO.getlnChar();
        }

    }

Upvotes: 0

MrT
MrT

Reputation: 594

To combine your code with the example from wawek:

 public class Experiments {
    public static void main(String[] args) {

    Scanner s = new Scanner(System.in);
    do {
       play();
    System.out.println("Again ( press y ) else press Anykey");
       c = s.next().charAt(0);
    } while(c == 'y');

    }

    public static play(){
        int n;

        System.out.print("Please enter a number:");
        n = TextIO.getlnInt();

        while (n<3) {
            System.out.print("Please enter another number:");
            n = TextIO.getlnInt();
        }
        while (n<=5) {
            System.out.println("Number entered should be more than 5.");
            System.out.println("Please re-enter:");
            n=TextIO.getlnInt();
        }
        if (n%2==0) {
            n=n*2+1;
        }
        else {
            n=n*2;
        }
        System.out.println("The result is "+n +".");
    }
    }

Upvotes: 1

wawek
wawek

Reputation: 1597

You have to do something like this:

Scanner s = new Scanner(System.in);
char c;
do {
   your_code
   c = s.next().charAt(0);
} while(c == 'y');

And thanks to this your program will ask you everytime if you want to continue and when you enter 'y' then it will loop again. Otherwise it will exit from the loop.

Upvotes: 1

Related Questions