2017-02-18 64 views
-2
static public charchar box1 = ' ', box2 = ' ', box3 = ' ', box4 = ' ', box5 = ' ', box6 = ' ', box7 = ' ', box8 = ' ', box9 = ' ', input2; 

static public void reset() 
{ 
    char box1 = ' ', box2 = ' ', box3 = ' ', box4 = ' ', box5 = ' ', box6 = ' ', box7 = ' ', box8 = ' ', box9 = ' ', input2; 
    bool isWin = false; 
    int line = 1, nrJogada = 1; 
    ciclo(); 
} 

嗯,我在做什么显然不工作,我已经寻找了很长时间对如何做到这一点的价值,但我不能(抱歉我不能不要让代码段工作) 如何更改公共/私有变量的值? (它会被用来重置游戏我怎样才能改变静态公共变量

+2

什么是“charchar”?你得到什么错误信息?错误消息是否可能表示解决方案? –

+0

在您的重置中,您声明的局部变量的名称与您的静态名称相同。 – Rob

回答

2

的问题是,你的reset方法是宣布新的局部变量隐藏公共领域。取出char并更换,;

static public void reset() 
{ 
    box1 = ' '; 
    box2 = ' '; 
    box3 = ' '; 
    box4 = ' '; 
    box5 = ' '; 
    box6 = ' '; 
    box7 = ' '; 
    box8 = ' '; 
    box9 = ' '; 
    bool isWin = false; 
    int line = 1, nrJogada = 1; 
    ciclo(); 
} 

其他局部变量isWinlinenrJogada将丢失一旦reset方法终止,如@Rob指出。

同样在这里,阵列将是更加便利。

public static char[] box = new char[8] {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}; 
public static char input2; 

private static bool isWin = false; 
private static int line = 1, nrJogada = 1; 

static public void reset() 
{ 
    for (int i = 0; i < box.Length; i++) { 
     box[i] = ' '; 
    } 
    isWin = false; 
    line = 1; nrJogada = 1; 
    ciclo(); 
} 

注意,在变量名前写入类型名称,声明一个新变量。

+1

那么isWin,line和nrJogada呢? – Rob

+1

也可能在静态类的构造函数中初始化数组,所以避免在代码中分配两次。我可能会将'reset'重命名为'Initialize'并从静态构造函数中调用它。 – Andrew