tailieunhanh - Lecter Java: Program design - Chapter 6: Iteration

In this chapter, we consider the while, for, and do-while looping constructs that control how many times a statement list is iterated (executed). Loops make programs much more powerful and capable of solving significant problems. Mastering the design and implementation of loops is an important part of a programmer's education. | Iteration Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Java looping Options while do-while for Allow programs to control how many times a statement list is executed Averaging Problem Extract a list of positive numbers from standard input and produce their average Numbers are one per line A negative number acts as a sentinel to indicate that there are no more numbers to process Observations Cannot supply sufficient code using just assignments and conditional constructs to solve the problem Don’t how big of a list to process Need ability to repeat code as needed Averaging Problem Extract a list of positive numbers from standard input and produce their average Numbers are one per line A negative number acts as a sentinel to indicate that there are no more numbers to process Algorithm Prepare for processing Get first input While there is an input to process do { Process current input Get the next input } Perform final processing Averaging . | Iteration Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Java looping Options while do-while for Allow programs to control how many times a statement list is executed Averaging Problem Extract a list of positive numbers from standard input and produce their average Numbers are one per line A negative number acts as a sentinel to indicate that there are no more numbers to process Observations Cannot supply sufficient code using just assignments and conditional constructs to solve the problem Don’t how big of a list to process Need ability to repeat code as needed Averaging Problem Extract a list of positive numbers from standard input and produce their average Numbers are one per line A negative number acts as a sentinel to indicate that there are no more numbers to process Algorithm Prepare for processing Get first input While there is an input to process do { Process current input Get the next input } Perform final processing Averaging Problem Extract a list of positive numbers from standard input and produce their average Numbers are one per line A negative number acts as a sentinel to indicate that there are no more numbers to process Sample run Enter positive numbers one per line. Indicate end of list with a negative number. -1 Average public class NumberAverage { // main(): application entry point public static void main(String[] args) throws IOException { // set up the list processing // prompt user for values // get first value // process values one-by-one while (value >= 0) { // add value to running total // processed another value // prepare next iteration - get next value } // display result if (valuesProcessed > 0) // compute and display average else // indicate no average to display } } Roadmap for program ("Enter positive numbers 1 per line.\n" + "Indicate end of the list with a negative number."); Scanner stdin = new Scanner(); int valuesProcessed = 0; double .

TỪ KHÓA LIÊN QUAN
crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.