Can someone help me with this question, even thought it is in chegg answers... I
ID: 3759724 • Letter: C
Question
Can someone help me with this question, even thought it is in chegg answers... I tried the solution step and keep get the error message saying cannot find the symbol countNUM, this chapeter has to do with loops.
Here is the file:
// DebugSix4.java
// Displays 5 random numbers between
// (and including) user-specified values
import java.util.Scanner;
public class FixDebugSix4
{
public static void main(String[] args)
{
int high, low, count;
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();
}
if (count < NUM)
countNUM = count;
else
countNUM = NUM;
/*repeat the loop to generate and print 5 random integers*/
while(count < NUM)
{
/*generate a random value between 0 and 1 (inclusive)*/
double result = Math.random();
// find the answer using the random value
// random() returns value between 0 and 1
int answer = (int) (result * 10 + low);
/* verify whether the answer is less than or equal to high*/
// multiply by 10 and add low -- random is at least the value of low
// only use answer if it is low enough
if(answer <= high)
{
//display the answer
System.out.print(answer + " ");
//increase the count value by 1
++count;
}//end if
}//end while
//goes to the next line
System.out.println();
}
}
Explanation / Answer
Solution:
Please find the running code...
package com.chegg.nancy.solution;
import java.util.Scanner;
public class FixDebugSix4 {
public static void main(String[] args) {
int high;
int low;
int 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();
}
/*if (count < NUM)
countNUM = count;
else
countNUM = NUM;*/
/* repeat the loop to generate and print 5 random integers */
while (count < NUM) {
/* generate a random value between 0 and 1 (inclusive) */
double result = Math.random();
// find the answer using the random value
// random() returns value between 0 and 1
int answer = (int) (result * 10 + low);
/* verify whether the answer is less than or equal to high */
// multiply by 10 and add low -- random is at least the value of low
// only use answer if it is low enough
if (answer <= high) {
// display the answer
System.out.print(answer + " ");
// increase the count value by 1
++count;
} // end if
} // end while
// goes to the next line
System.out.println();
}
}