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.
- Open the source code file named MovieGuide.java using Notepad or an IDE of your choice.
- Review the code and save the source code file in a directory of your choice.
- Compile the source code file, MovieGuide.java.
- Execute the program. Input the following as star ratings:
0 3 4 4 1 1 2 -1
- Record the average star rating for the movie.
- 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started