首页 > 代码库 > 解题.for; block scope; while; "2 to the " + i + " power is " + result

解题.for; block scope; while; "2 to the " + i + " power is " + result

 1 package com.java7; 2  3 public class WhileDemo { 4     public static void main(String[] args) { 5         int e; 6         int result; 7         for(int i = 0; i < 10; i++){ 8             result = 1; 9             e = i;10             while(e > 0){11                 result *= 2;12                 e--;13             }14             15         System.out.println("2 to the " + i + " power is " + result);16         }17     }18 }

 执行过程:

1: i = 0; result = 1; e = 0;

2: i = 1; result = 1; e = 1; result = result(1)*2 = 2; e = 0; end loop;

3: i = 2; result = 1; e = 2; result = result(1)*2 = 2; e = 1; result = result(2)*2 = 4; e = 0; end loop;

4: // ...

解题.for; block scope; while; "2 to the " + i + " power is " + result