Chapter 5 : Control Structures 2 : Repetition

 import java.util.*;
import java.io.*; public class Loop { static Scanner console = new Scanner(System.in); public static void main(String[] args) { int i = 0;
int sum = 0; while ( console.hasNext() ) {
i = console.nextInt();
System.out.println(i);
sum = sum + i; } System.out.println("sum = " + sum); } }

这个代码,本来实验while和console.hasNext(),不知错在哪里

需要两次舒服EOF,  why????

The break and continue statements are an effective way to avoid extra variables to control
a loop and produce an elegant code. However, these statements must be used very sparingly
within a loop. An excessive use of these statements in a loop will produce a spaghetti-code
(loops with many exit conditions) and could be very hard to understand and manage.

As stated earlier, all three loops have their place in Java and one loop can often replace
another. The execution of a continue statement, however, is where a while structure
differs from a for structure. In a while loop, when the continue statement is executed,
if the update statement appears after the continue statement, the update
statement is not executed. In a for loop, the update statement always executes.

上一篇:逻辑与(&&、&)和逻辑或(||、|)


下一篇:从Wordpress迁移到Jekyll