使用嵌套的控制语句
/* n o w j a v a . c o m - 时 代 Java */ import java.util.Scanner; public class Main { public static void main(String[] args) { // create Scanner to obtain input from command window Scanner input = new Scanner(System.in); // initializing variables in declarations int passes = 0; int failures = 0; int studentCounter = 1; // process 10 students using counter-controlled loop while (studentCounter <= 10) { /* from n o w j a v a . c o m - 时 代 Java */ // prompt user for input and obtain value from user System.out.print("Enter result (1 = pass, 2 = fail): "); int result = input.nextInt(); // if...else is nested in the while statement if (result == 1) passes = passes + 1; else failures = failures + 1; // increment studentCounter so loop eventually terminates studentCounter = studentCounter + 1; }