Question: In this lab, you will review the while loop that uses a sentinel value to control a loop in a Java program. A source code

In this lab, you will review the while loop that uses a sentinel value to control a loop in a Java program. A source code has already been provided with the necessary variable declarations, logic and output statements.

Each theater patron enters a value from 0 to 4 indicating the number of stars that the patron awards to the Guides featured movie of the week. The program executes continuously until the theater manager enters a negative number to quit. At the end of the program, you should display the average star rating for the movie.

  1. Open the source code file named MovieGuide.java using Notepad or an IDE of your choice.
  2. Review the code and save the source code file in a directory of your choice.
  3. Compile the source code file, MovieGuide.java.
  4. Execute the program. Input the following as star ratings:

0 3 4 4 1 1 2 -1

  1. Record the average star rating for the movie.
  2. Attach execution results to Moodle

import java.util.Scanner;

public class MovieGuide { public static void main(String args[]) { Scanner s = new Scanner(System.in); // Declare and initialize variables. double numStars; // star rating. String numStarsString; // string version of star rating double averageStars; // average star rating. double totalStars = 0; // total of star ratings. int numPatrons = 0; // keep track of number of patrons // This is the work done in the housekeeping() method // Get input. System.out.println("Enter rating for featured movie: "); numStarsString = s.nextLine(); // This is the work done in the detailLoop() method // Convert to integer. numStars = Double.parseDouble(numStarsString); while ( numStars >= 0 ) // Test for loop entry. { totalStars += numStars; // Accumulate total of star ratings. numPatrons++; // Add 1 to number of patrons. System.out.println("Enter rating for featured movie: "); numStarsString = s.nextLine(); numStars = Integer.parseInt(numStarsString); } // End of while loop. // This is the work done in the endOfJob() method averageStars = totalStars / numPatrons; System.out.println("Average Star Value: " + averageStars); System.exit(0); } // End of main() method.

} // End of MovieGuide class.

I know all I have to do is compile the code and run the numbers but for whatever reason when I run it the output box shows up but won't allow me to input any numbers

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!