Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

3) A do-while loop is most appropriate when the condition for repeating the loop statements is not known in advance, i.e., it is determined in

image text in transcribed

3) A do-while loop is most appropriate when the condition for repeating the loop statements is not known in advance, i.e., it is determined in the loop. In the following program, each iteration of the loop prompts the user to enter a number - positive, negative, or zero in order to keep a running total of the numbers the entered and also keep a count of the number of entries the user makes. The program should stop whenever the user enters "Q" to quit. When the user has finished, it prints the grand total and the number of entries the user typed For simplicity, we begin by simply prompting the user to enter a number. Then we want him to indicate whether or not to continue Because any number can be entered, we need to have another indication of whether or not the user has entered a number or a "Q". There are two ways to accomplish this task: 1) read the input using the Scanner next) method checking to see if it is a number or a "Q", or 2) check to see if the user has entered a number with the Scanner hasNextDouble) method. Choose one approach and complete the code shown below. (Note that if you choose approach 2, any response other than a number will cause the loop to quit.) import java.util.Scanner; public class DoWhile public static void main(String[] args) double total-0; int count = 0; double inputValue; Scanner in = new scanner(System.in); inputValue in.nextDouble() total = total + inputvalue; count count + 1; System.out.println("Enter a number or type 'Q'to uit"); while (i System.out.println("The total was "total "."); System.out.println ("You typedcount + "values.")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Accidental Data Scientist

Authors: Amy Affelt

1st Edition

1573877077, 9781573877077

More Books

Students also viewed these Databases questions

Question

what is the most common cause of preterm birth in twin pregnancies?

Answered: 1 week ago

Question

Which diagnostic test is most commonly used to confirm PROM?

Answered: 1 week ago

Question

What is the hallmark clinical feature of a molar pregnancy?

Answered: 1 week ago