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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 赵喜平 中级黑马   /  2013-3-31 10:22  /  1483 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 赵喜平 于 2013-3-31 18:32 编辑

昨天看到的贴子,今天找不到了,又不会解决,,求助
此段代码一运行就会出现死循环
  1. class LanPingException extends Exception{
  2.     LanPingException(String msg)
  3.     {
  4.             super(msg);
  5.     }
  6. }
  7. class MaoYanException extends Exception{
  8.        MaoYanException(String msg)
  9.        {
  10.              super(msg);
  11.        }
  12. }
  13. class TeachStopException extends Exception{
  14.                TeachStopException(String msg)
  15.               {
  16.                     super(msg);
  17.               
  18.               }
  19. }
  20. class FixComputer{
  21.       public void Fix()
  22.       {
  23.             System.out.println("把电脑修好了");
  24.       }
  25. }
  26. class Teacher{
  27.     private String name;
  28.     Teacher(String name)
  29.     {
  30.        this.name=name;
  31.     }
  32.     Computer co=new Computer();
  33.     FixComputer fi=new FixComputer();

  34.     public void teachxue()throws TeachStopException
  35.     {         
  36.             try
  37.             {
  38.                co.run();
  39.               System.out.println(name+"老师在上课");
  40.             }
  41.             catch (LanPingException e)
  42.             {
  43.                     System.out.println(e.getMessage());
  44.                     co.result();
  45.                     teachxue();
  46.                   
  47.             }
  48.             catch(MaoYanException e)
  49.             {         System.out.println(e.getMessage());
  50.                      this.function();
  51.             }
  52.       
  53.     }
  54.     public void function()
  55.     {
  56.             System.out.println("大家做练习吧");
  57.             fi.Fix();

  58.     }
  59. }
  60. class Computer{
  61.     private int i=1;         
  62.      public void run()throws LanPingException,MaoYanException
  63.     {
  64.         if (i==1)
  65.                  throw new LanPingException("电脑蓝屏了");
  66.             if(i==2)
  67.                throw new MaoYanException("电脑冒烟了");
  68.      System.out.println("电脑正常开机运行");
  69.     }
  70.     public void result()
  71.     {
  72.             System.out.println("电脑重启");
  73.       
  74.     }
  75. }

  76. class ComputerTest{
  77.       public static void main(String[] args)
  78.       {                 Teacher te=new Teacher("石松老师");
  79.                        try
  80.                        {
  81.                               te.teachxue();
  82.                        }
  83.                        catch (TeachStopException e)
  84.                        {
  85.                               te.function();
  86.                        }
  87.             }
  88. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

6 个回复

倒序浏览
看晕了··自己可以一点一点的写好了,慢慢测试!
回复 使用道具 举报
问题主要出在这段代码上,抛出蓝屏异常后,被catch到,然后调用result方法。

但是调用玩result方法后再次“通知老师”调用teachxue时,你定义的“i”还是等于1,所以再次抛异常,造成一直循环

我在result中加入了i = 0,代表电脑恢复正常
  1. ……………………
  2. public void teachxue() throws TeachStopException {
  3.                 try {
  4.                         co.run();
  5.                         System.out.println(name + "老师在上课");
  6.                 } catch (LanPingException e) {
  7.                         System.out.println(e.getMessage());
  8.                         co.result();//处理电脑问题
  9.                         teachxue();//处理后再次调用老师

  10.                 } catch (MaoYanException e) {
  11.                         System.out.println(e.getMessage());
  12.                         this.function();
  13.                 }

  14.         }

  15.         public void function() {
  16.                 System.out.println("大家做练习吧");
  17.                 fi.Fix();

  18.         }
  19. }

  20. class Computer {
  21.         private int i = 1;//作为错误代码而存在

  22.         public void run() throws LanPingException, MaoYanException {
  23.                 if (i == 1)
  24.                         throw new LanPingException("电脑蓝屏了");
  25.                 if (i == 2)
  26.                         throw new MaoYanException("电脑冒烟了");
  27.                 System.out.println("电脑正常开机运行");
  28.         }

  29.         public void result() {
  30.                 System.out.println("电脑重启");
  31.                 i = 0;//重置错误代码

  32.         }
  33. }

  34. ……………………
复制代码

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
本帖最后由 熊永标 于 2013-3-31 11:14 编辑

问题出现在这个类:
  1. 62.class Computer{

  2. 63.    private int i=1;         

  3. 64.     public void run()throws LanPingException,MaoYanException

  4. 65.    {

  5. 66.        if (i==1)

  6. 67.                 throw new LanPingException("电脑蓝屏了");

  7. 68.            if(i==2)

  8. 69.               throw new MaoYanException("电脑冒烟了");

  9. 70.     System.out.println("电脑正常开机运行");

  10. 71.    }

  11. 72.    public void result()

  12. 73.    {

  13. 74.            System.out.println("电脑重启");

  14. 75.      

  15. 76.    }

  16. 77.}

复制代码
分析得出:
在入口函数中调用teachxue(),而这个方法调用computer的run方法,因为 i 的值始终等于1,所以始终抛出 LanPingException("电脑蓝屏了")异常,而在teacher中的teachxue()处理完异常后又递归调用此方法,而 i的值并没有任何改变,所以就一直循环下去了,循环的根本原因就是递归调用函数.i 必需在正确的时刻动态的给i赋除1和2以外的值就可以了.

代码改正如下:
  1. 62.class Computer{

  2. 63.    private int i=1;         

  3. 64.     public void run()throws LanPingException,MaoYanException

  4. 65.    {
  5.            i=new Random().nextInt(2)+1;//产生一个一到三的随机数
  6. 66.        if (i==1)

  7. 67.                 throw new LanPingException("电脑蓝屏了");

  8. 68.            if(i==2)

  9. 69.               throw new MaoYanException("电脑冒烟了");
  10. 70.     System.out.println("电脑正常开机运行");

  11. 71.    }

  12. 72.    public void result()

  13. 73.    {

  14. 74.            System.out.println("电脑重启");

  15. 75.      

  16. 76.    }

  17. 77.}

复制代码

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
真心看晕了,全代码没一个注释。看了一半就不想看了。。
LZ 希望你i以后能加些注释。{:soso_e128:}
回复 使用道具 举报
陈腾跃_2013 发表于 2013-3-31 10:53
问题主要出在这段代码上,抛出蓝屏异常后,被catch到,然后调用result方法。

但是调用玩result方法后再次 ...

:handshake
回复 使用道具 举报
在teacher中 catch (LanPingException e)
            {
                    System.out.println(e.getMessage());
                    co.result();
                    teachxue();
                  
            }
在异常处理中请不要调用teachxue();
在co.result();后应该做的是co.i=0;你的程序中设置了private关键字,请提供改变i值得函数,

希望对你有帮助

评分

参与人数 1技术分 +1 收起 理由
陈丽莉 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马