黑马程序员技术交流社区

标题: 请各位大神帮我看看 下面代码那里出问题了 [打印本页]

作者: yearn    时间: 2015-4-13 16:43
标题: 请各位大神帮我看看 下面代码那里出问题了
编译不通过,解决不了!还请各位大神帮忙看看。

class LanPinException extends Exception
{
  LanPinException(String message)
  {
   super(message);
  }
}
class MaoYanException extends Exception
{
  MaoYanException(String message)
  {
   super(message);
  }
}
class NoPlanException extends Exception
{
  NoPlanException(String msg)
  {
   super(massage);
  }
}

class Computer
{ private int state=3;
public void run()throws LanPinExceptin,MaoYanException
{   
  if(state=2)
  throw new LanPinException("电脑蓝屏");
  if(state=3)
  throw new MaoYanException("电脑冒烟");
  
  System.out.println("电脑运行");
}
public void reset()
{   
  state=1;
  System.out.println("电脑重启");
}
}
class Teacher
{
private String name;
private Computer cop;
Teacher(String name)
{
  this.name=name;
  cop=new Computer();
  public void jiangke()throws NoPlanException
  {
   try
   {
    cop.run();
   }
   catch (LanPinException e)
   {
    cop.reset();
   }
   catch (MaoYanException e)
    test();
   {
    throw new NoPlanException("课时无法继续"+e.getMassage());
   }
  System.out.println("讲课");
   
  }
public void test()
{
  System.out.println("练习");
}

}
}
class  Exception
{
public static void main(String[] args)
{
  Teather t = new Teather("杨老师");
  t.jiangke();
  try
  {
   t.jiangke();
  }
  catch (NoplanException e)
  {
   System.out.println(e.getMassage);
   System.out.println("更换电脑或老师");
  }
}


作者: 2015黑马之路    时间: 2015-4-13 17:17
这位同学,首先你这样贴一大段代码上来,说一声编译不过,请帮忙的做法实在不可取。
然后,代码编译不过,要学会自己在myeclipse中慢慢调试呀~~ 下面代码是我一点点调试出来的,编译通过。
调试中发现,里面很多简单拼写错误,作为一名要去黑马的程序员,你得对自己要求高点!
  1. package com.itheima;

  2. class Exception extends Throwable {
  3.         public Exception() {
  4.                 // TODO Auto-generated constructor stub
  5.         }

  6.         public Exception(String message) {

  7.         }

  8.         public static void main(String[] args) {
  9.                 Teacher t = new Teacher("杨老师");
  10.                 try {
  11.                         t.jiangke();
  12.                 } catch (NoPlanException e1) {
  13.                         // TODO Auto-generated catch block
  14.                         e1.printStackTrace();
  15.                 }

  16.                 try {
  17.                         t.jiangke();
  18.                 } catch (NoPlanException e) {
  19.                         System.out.println(e.getMessage());
  20.                         System.out.println("更换电脑或老师");
  21.                 }
  22.         }
  23. }

  24. class LanPinException extends Exception {
  25.         LanPinException(String message) {
  26.                 super(message);
  27.         }
  28. }

  29. class MaoYanException extends Exception {
  30.         MaoYanException(String message) {
  31.                 super(message);
  32.         }
  33. }

  34. class NoPlanException extends Exception {
  35.         NoPlanException(String massage) {
  36.                 super(massage);
  37.         }
  38. }

  39. class Computer {
  40.         private int state = 3;

  41.         public void run() throws LanPinException, MaoYanException {
  42.                 if (state == 2)
  43.                         throw new LanPinException("电脑蓝屏");
  44.                 if (state == 3)
  45.                         throw new MaoYanException("电脑冒烟");

  46.                 System.out.println("电脑运行");
  47.         }

  48.         public void reset() {
  49.                 state = 1;
  50.                 System.out.println("电脑重启");
  51.         }
  52. }

  53. class Teacher {
  54.         private String name;
  55.         private Computer cop;

  56.         Teacher(String name) {
  57.                 this.name = name;
  58.                 cop = new Computer();
  59.         }

  60.         public void jiangke() throws NoPlanException {
  61.                 try {
  62.                         cop.run();
  63.                 } catch (LanPinException e) {
  64.                         cop.reset();

  65.                 } catch (MaoYanException e) {
  66.                         test();
  67.                         throw new NoPlanException("课时无法继续" + e.getMessage());
  68.                 }
  69.                 System.out.println("讲课");
  70.         }

  71.         public void test() {
  72.                 System.out.println("练习");
  73.         }
  74. }
复制代码

作者: xlunaer    时间: 2015-4-13 17:17
问题好多。所以给你几个建议:
1,不知道你是不是学过c语言,java的代码风格不是这样的,大括号不用另起一行。直接空格加大括号即可。
2.遇到好多单词拼写错误,大小写错误。写代码的时候注意一下。如果单词不好,平时花点时间背背单词,写代码会顺畅很多。
3,有几个括号多了或者少了。括号一定要左右两个括号写写好,再写里面的代码,否则容易遗忘。
代码如下,改的不好别介意。
  1. class LanPinException extends Exception {
  2.         /**
  3.          *
  4.          */
  5.         private static final long serialVersionUID = 1L;

  6.         LanPinException(String message) {
  7.                 super(message);
  8.         }
  9. }

  10. class MaoYanException extends Exception {
  11.         /**
  12.          *
  13.          */
  14.         private static final long serialVersionUID = 1L;

  15.         MaoYanException(String message) {
  16.                 super(message);
  17.         }
  18. }

  19. class NoPlanException extends Exception {
  20.         /**
  21.          *
  22.          */
  23.         private static final long serialVersionUID = 1L;

  24.         NoPlanException(String msg) {
  25.                 // super(massage); //是msg,写代码别太慌
  26.                 super(msg);
  27.         }
  28. }

  29. class Computer {
  30.         private int state = 3;

  31.         // public void run()throws LanPinExceptin,MaoYanException //是LanPinException
  32.         public void run() throws LanPinException, MaoYanException {
  33.                 // if(state=2) //判断相等用== , = 是赋值
  34.                 if (state == 2)
  35.                         throw new LanPinException("电脑蓝屏");
  36.                 // if(state=3) //同上
  37.                 if (state == 3)
  38.                         throw new MaoYanException("电脑冒烟");

  39.                 System.out.println("电脑运行");
  40.         }

  41.         public void reset() {
  42.                 state = 1;
  43.                 System.out.println("电脑重启");
  44.         }
  45. }

  46. class Teacher {
  47.         private String name;
  48.         private Computer cop;

  49.         Teacher(String name) {
  50.                 this.name = name;
  51.                 cop = new Computer(); // 构造方法少了一个大括号
  52.         }

  53.         public void jiangke() throws NoPlanException {
  54.                 try {
  55.                         cop.run();
  56.                 } catch (LanPinException e) {
  57.                         cop.reset();
  58.                 } catch (MaoYanException e) {
  59.                         test();
  60.                         // { //大括号放在catch语句后面
  61.                         throw new NoPlanException("课时无法继续" + e.getMessage());
  62.                 }
  63.                 System.out.println("讲课");

  64.         }

  65.         public void test() {
  66.                 System.out.println("练习");
  67.         }

  68. }

  69. // } //多了一个大括号
  70. class Exception {
  71.         public static void main(String[] args) {
  72.                 // Teather t = new Teather("杨老师"); //单词拼写错误
  73.                 Teacher t = new Teacher("杨老师");
  74.                 // t.jiangke(); //注释掉吧,下面一样的
  75.                 try {
  76.                         t.jiangke();
  77.                 }
  78.                 // catch (NoplanException e) //大写
  79.                 catch (NoPlanException e) {
  80.                         // System.out.println(e.getMassage); //单词拼写错误 & 调用方法别忘了后面的括号
  81.                         System.out.println(e.getMessage());
  82.                         System.out.println("更换电脑或老师");
  83.                 }
  84.         }
  85.         // 少了一个打阔靠
  86. }
复制代码

作者: Searching    时间: 2015-4-13 17:18
你最好用IDE编辑一下,你这段代码问题太多,列出来要一页纸。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2