A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 崔自成 中级黑马   /  2013-5-4 18:05  /  1397 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

程序结果不对,帮忙调下。就是两个线程给一个Person一个加血~一个减血。
我想要的结果是Person 失血而亡,但是结果是满血复活...帮忙调下。
  1. class MyTest
  2. {       
  3.         public static void main(String[] args)
  4.         {
  5.                 Person p = new Person();
  6.                 new Thread(new AddLife(p)).start();
  7.                 new Thread(new HurtLife(p)).start();               
  8.         }

  9. }

  10. class Person
  11. {
  12.         private int life = 10000;
  13.         /*加血*/
  14.         public void addLife(){
  15.                 life+=20;
  16.                 System.out.println("Person加了"+20+"的血,现在Life值是:"+life);
  17.         }
  18.         /*减血*/
  19.         public void hurtLife(int x){
  20.                 if(x>life){
  21.                         /*伤害值如果大于life,
  22.                         则life值直接为0;
  23.                         */
  24.                         life=0;
  25.                 }else{
  26.                         life-=x;                       
  27.                 }
  28.                 System.out.println("Person受到"+x+"伤害,现在Life值是:"+life);
  29.         }
  30.        
  31.         public int getLife(){//get
  32.                 return life;
  33.         }
  34. }

  35. class AddLife implements Runnable
  36. {
  37.         private Person p;
  38.         AddLife(Person p){this.p=p;}
  39.        
  40.         public void run(){               
  41.                 while(true){
  42.                         synchronized(p){
  43.                                 if(p.getLife()<9000)//如果life值小于9000开始加血
  44.                                 {
  45.                                         p.addLife();
  46.                                 }
  47.                         }       
  48.                 }               
  49.         }
  50. }

  51. class HurtLife implements Runnable
  52. {
  53.         private Person p;
  54.         HurtLife(Person p){this.p=p;}

  55.         public void run(){               
  56.                 while(true){
  57.                         synchronized(p){
  58.                                 if(p.getLife()>0){//大于零开始减血
  59.                                         p.hurtLife(200);
  60.                                 }
  61.                                 else if (p.getLife()==0)//等于零表示挂了,程序退出。
  62.                                 {
  63.                                         System.out.println("Person挂了");
  64.                                         return;
  65.                                 }
  66.                         }                       
  67.                 }               
  68.         }
  69. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
张熙韬 + 1

查看全部评分

2 个回复

倒序浏览
  1. class MyTest
  2. {        
  3.         public static void main(String[] args)
  4.         {
  5.                 Person p = new Person();
  6.                 new Thread(new AddLife(p)).start();
  7.                 new Thread(new HurtLife(p)).start();               
  8.         }

  9. }

  10. class Person
  11. {
  12.         private int life = 10000;
  13.         /*加血*/
  14.         public void addLife(){
  15.                 life+=20;
  16.                 System.out.println("Person加了"+20+"的血,现在Life值是:"+life);
  17.         }
  18.         /*减血*/
  19.         public void hurtLife(int x){
  20.                 if(x>life){
  21.                         /*伤害值如果大于life,
  22.                         则life值直接为0;
  23.                         */
  24.                         life=0;
  25.                 }else{
  26.                         life-=x;                        
  27.                 }
  28.                 System.out.println("Person受到"+x+"伤害,现在Life值是:"+life);
  29.         }
  30.         
  31.         public int getLife(){//get
  32.                 return life;
  33.         }
  34. }

  35. class AddLife implements Runnable
  36. {
  37.         private Person p;
  38.         AddLife(Person p){this.p=p;}
  39.         
  40.         public void run(){               
  41.                 while(true){
  42.                         synchronized(p){
  43.                                 if(p.getLife()<9000)//如果life值小于9000开始加血
  44.                                 {
  45.                                         p.addLife();
  46.                                 }
  47.                         }        
  48.                 }               
  49.         }
  50. }

  51. class HurtLife implements Runnable
  52. {
  53.         private Person p;
  54.         HurtLife(Person p){this.p=p;}

  55.         public void run(){               
  56.                 while(true){
  57.                         synchronized(p){
  58.                                 if(p.getLife()>0){//大于零开始减血
  59.                                         p.hurtLife(200);
  60.                                 }
  61.                                 else if (p.getLife()==0)//等于零表示挂了,程序退出。
  62.                       {
  63.                                          System.out.println("Person挂了 GAME OVER!");
  64.                                         //return;  把return去掉  既然挂了 还return干啥  
  65.                            //直接退出就行 程序不退出 就一直会处于加血状态。。
  66.                            System.exit(0);// 当没血的时候退出程序 GAME OVER!                              
  67.                                    }
  68.                         }                        
  69.                 }               
  70.         }
  71. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马