- Java code
import java.io.*;import java.math.*;public class Puncher { static int i,j; static void hole(int n) { int i; i = n+j; j= i+j; System.out.println("\n inside: i = "+i+" j = "+j); } public static void main(String argv[]) { System.out.println("\n before: i = "+i+" j = " +j); hole(5); System.out.println("\n after: i = "+i+" j = " +j); }}
我觉得输出应该是:
before: i = 0 j = 0
inside: i = 5 j = 5
after: i = 5 j = 5
然而跑了一下这个程序输出:
before: i = 0 j = 0
inside: i = 5 j = 5
after: i = 0 j = 5
------解决方案--------------------
你在hole()内部定义了一个局部变量i,改变的是局部变量,不是外面的那个i