嵌套的if-else语句

今天写一个程序,不知道哪错了,如下:

		if (flag) {
			if (this.balance > 0) {
				this.balance -= money;
				System.out.println("取钱成功" + this.balance);
				flag = false;
				notifyAll();
			} else {
				try {
					wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

后来把第二个嵌套的if去掉,程序结果正常。我怀疑是嵌套的if-else语句造成的问题。嵌套的if-else循环是个容易出错的地方,以后写到嵌套的if-else语句时一定要多加注意!!!

改正,按照程序逻辑,else语句应该和第一个if相配套,改正如下:

		if (flag) {
			if (this.balance > 0) {
				this.balance -= money;
				System.out.println("取钱成功" + this.balance);
				flag = false;
				notifyAll();
			} 
		}else {
				try {
					wait();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}


你可能感兴趣的:(Java语法)