简体   繁体   中英

How to use while loop to ask the program to try again at the end?

I am trying to use the while loop combine this two separate program into one and I have been trying this for a week now but still couldn't figure out where to put the "Do you want to try again (Y/N)?" loop. Any help or tips will do..

First separate WORKING program:

It will ask the user to input three integers. A Starting number, an End number that is greater than the Starting number, and a Step number.

import java.util.Scanner;
public class sample {

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

    String input;
        int first , second , third ;

        System.out.print("First number: ");
        input = in.nextLine();
        first = Integer.parseInt(input);
        System.out.print("Second number:: ");
        input = in.nextLine();
        second = Integer.parseInt(input);
        System.out.print("Third number: ");
        input = in.nextLine();
        third = Integer.parseInt(input);

        while(first <= second)
        {
            first = second + third;
            System.out.println(first);

        }
    }
}

Example output:

Start: 1

End: 10

Step: 2

1

3

5

7

9

Second Separate WORKING Program:

It will ask the user to try again.

import java.util.Scanner;

public class sample {

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

    char c = 'y';

    while (c == 'y')
       {
            System.out.println("C");
            System.out.print("Do you wish to continue? ");
            c = in.next().toLowerCase().charAt(0);

        }
    }
}

Example Output:

Do you want to try again (Y/N)? n



And I am trying to get an an output like this:


Start: 1

End: 10

Step: 2

1

3

5

7

9

Do you want to try again (Y/N)? n


If I entered 'y' it should get me back from the very start and ask me to input for the Start,End, and Step. And if I entered 'n' it should terminate the program. ... With only using the while loop to combine the two.

You need to simply wrap the first program inside the loop of the second. It will be a little clearer if you separate the operation of the first program into a separate method. The result might be something like this:

public class TheProgram {
    private static Scanner in = new Scanner(System.in);

    private static void interact() {

        String input;
        int first , second , third ;

        System.out.print("First number: ");
        input = in.nextLine();
        first = Integer.parseInt(input);
        System.out.print("Second number:: ");
        input = in.nextLine();
        second = Integer.parseInt(input);
        System.out.print("Third number: ");
        input = in.nextLine();
        third = Integer.parseInt(input);

        while(first <= second)
        {
            first = second + third;
            System.out.println(first);

        }
    }

    public static void main(String args[]) {
        char c = 'y';

        while (c == 'y') {
            interact();
            System.out.print("Do you wish to continue? ");
            c = in.next().toLowerCase().charAt(0);
        }
    }
}

Why do you have it as two separate programs? You should just do it in one program and use an if loop that has a bool set to true unless they type no, which typing no would cause the program to exit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM