Search code examples
javawhile-loopdo-whilestatements

Do-while statements beginner java max-min prompt


Hey guys I new to Java and on do-while statements, the question asks me to create a prompt that asks for a max and a min value, then it asks for another value between my max and min values. "The user should be continually be prompted until a number within the range is entered. Im having a hard time wrapping my head around using a do-while statement so some help would be nice thanks! Also try to keep it simple!

package Chapter6Java;
import java.util.Scanner;

public class Chapter6Prompter {

    public static void main(String [] args){
        int max, min, between;

        Scanner input = new Scanner(System.in);

        System.out.print("Enter a min value: ");
        min = input.nextInt(); 

        System.out.print("Enter a max value: ");
        max = input.nextInt();

        do {
            System.out.print("Enter a value between your min and max values:");
            between = input.nextInt();
        } while (between != max && between != min);

    }

}

Solution

  • Modify your condition in the while as:

    while (between >= max || between <= min);
    

    Since you are checking that between is in range you should check if its greater than or equal to minimum value and less than or equal to maximum value.