java中关于!符号的使用问题

1.源码

package charactor;

import java.io.Serializable;
 
public class Hero{
    public String name; 
    public float hp;
    
    public int damage;
    
    public void attackHero(Hero h) {
    	try {
    		//为了表示攻击需要时间,每次攻击暂停1000毫秒
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	h.hp-=damage;
    	System.out.format("%s 正在攻击 %s, %s的血变成了 %.0f%n",name,h.name,h.name,h.hp);
    	
    	if(h.isDead())
    		System.out.println(h.name +"死了!");
    }

	public boolean isDead() {
		return 0>=hp?true:false;
	}

}

package multiplethread;

import charactor.Hero;

public class TestThread {

	public static void main(String[] args) {
		
		Hero gareen = new Hero();
		gareen.name = "盖伦";
		gareen.hp = 616;
		gareen.damage = 50;

		Hero teemo = new Hero();
		teemo.name = "提莫";
		teemo.hp = 300;
		teemo.damage = 30;
		
		Hero bh = new Hero();
		bh.name = "赏金猎人";
		bh.hp = 500;
		bh.damage = 65;
		
		Hero leesin = new Hero();
		leesin.name = "盲僧";
		leesin.hp = 455;
		leesin.damage = 80;
		
		//盖伦攻击提莫
		while(!teemo.isDead()){
			gareen.attackHero(teemo);
		}

		//赏金猎人攻击盲僧
		while(!leesin.isDead()){
			bh.attackHero(leesin);
		}
	}
	
}

2.探讨代码段

while(!leesin.isDead()){
			bh.attackHero(leesin);
		}
public boolean isDead() {
		return 0>=hp?true:false;
	}

2.1正确推导

graph TD A[while循环执行 A] -->B(!lessin.isDead 为true B) A-->C(lessin.hp>0 C) B --> D{lessin.isDead 为false D} D-->E(lessin.hp>0时,lessin.isDead为false E) C-->E E-->|相符合|F[0>=hp?true:false F]

2.2一开始的错误推导

graph TD A[while循环执行 A] -->B(!lessin.isDead 为true B) A-->C(lessin.hp>0 C) C-->D B --> D{lessin.hp>0时,!lessin.isDead为true D} D-->E(lessin.hp<=0时,lessin.isDead为false E) E-->|不符合|F[0>=hp?true:false F]

2.3问题

从D到E推导有问题,漏掉了!符号。

graph TD D{lessin.hp>0时,!lessin.isDead为true D}-->E(lessin.hp<=0时,!lessin.isDead为false E) E-->F(lessin.hp<=0时,lessin.isDeaad为true F) F-->|相符合|G[0>=hp?true:false G]

2.4疑惑

以上是根据最后结果的反向推导,从E到F的推导是否有问题?

猜你喜欢

转载自www.cnblogs.com/zuoshouyoushou/p/12331996.html
今日推荐