View difference between Paste ID: 8wqkrq9c and VDuTMyxA
SHOW: | | - or go back to the newest paste.
1
import java.util.Scanner;
2
3
public class PromptInRange
4
{
5
    public static int promptIntInRange(int min, int max, String minError, String maxError)
6
    {
7
        Scanner user_input = new Scanner(System.in);
8
        int number;
9
10
        do
11
        {
12-
            System.out.print("Please enter a whole number: ");
12+
13
            number = user_input.nextInt();
14
15-
            if (number < 0)
15+
16
            {
17-
                System.out.print("That's a negative number. ");
17+
18
            }
19-
        } while (number < 0);
19+
20
            {
21
                System.out.print(maxError + " ");
22
            }
23
        } while (number < min || number > max);
24
25
        return number;
26
    }
27
28
    public static int promptIntInRange(int min, int max, String error)
29
    {
30
        return promptIntInRange(min, max, error + " ");
31
    }
32
33
    public static int promptIntInRange(int min, int max)
34-
            if (number < min || number > max)
34+
35
        return promptIntInRange(min, max, "That's out of range.");
36-
                System.out.print("That's out of range. ");
36+
37
38
    public static int promptWholeNumber()
39
    {
40
        return promptIntInRange(0, Integer.MAX_VALUE, "That's a negative number.");
41
    }
42
}