Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1 // DebugSix4.java 2 // Displays 5 random numbers between 3 // (and including)

ID: 3762937 • Letter: 1

Question

1 // DebugSix4.java
2 // Displays 5 random numbers between
3 // (and including) user-specified values
4 import java.util.Scanner;
5 public class DebugSix4
6 {
7 public static void main(String[] args)
8 {
9 int high, low, count;
10 final int NUM = 5;
11 Scanner input = new Scanner(System.in);
12 // Prompt user to enter high and low values
13 System.out.print("This application displays " + NUM +
14 " random numbers" +
15 " between the low and high values you enter" +
16 " Enter low value now... ");
17 low = input.nextInt();
18 System.out.print("Enter high value... ");
19 high = input.nextInt();
20 while(low == high)
21 {
22 System.out.println("The number you entered for a high number, " +
23 high + ", is not more than " + low);
24 System.out.print("Enter a number higher than " + low + "... ");
25 high = input.nextInt();
26 }
27
28 while(count < low)
29 {
30 double result = Math.random();
31 // random() returns value between 0 and 1
32 int answer = (int) (result * 10 + low);
33 // multiply by 10 and add low -- random is at least the value of low
34 // only use answer if it is low enough
35 if(answer <= low)
36 {
37 System.out.print(answer + " ");
38 ++count;
39 }
40 }
41 System.out.println();
42 }
43 }
44// book is JAVA programming 8th Edition, JoyceFarrell. Thank you

Explanation / Answer

import java.util.Random;
import java.util.Scanner;

public class DebugSix4 {
   public static void main(String[] args) {
       int high, low, count = 0;
       final int NUM = 5;
       Scanner input = new Scanner(System.in);
       // Prompt user to enter high and low values
       System.out.print("This application displays " + NUM + " random numbers"
               + " between the low and high values you enter"
               + " Enter low value now... ");
       low = input.nextInt();
       System.out.print("Enter high value... ");
       high = input.nextInt();
       while (low == high) {
           System.out.println("The number you entered for a high number, "
                   + high + ", is not more than " + low);
           System.out.print("Enter a number higher than " + low + "... ");
           high = input.nextInt();
       }

       while (count != 5) {

           Random r = new Random();
           int randomNum = r.nextInt((high - low) + 1) + low;

           System.out.print(randomNum + " ");
           ++count;

       }
       System.out.println();
   }
}

OUTPUT :

TEST1:

This application displays 5 random numbers
between the low and high values you enter
Enter low value now... 5
Enter high value... 5
The number you entered for a high number, 5, is not more than 5
Enter a number higher than 5... 10
8 10 10 5 10

TEST2:

This application displays 5 random numbers
between the low and high values you enter
Enter low value now... 10
Enter high value... 20
17 18 18 18 15