黑马程序员技术交流社区
标题:
请各位大神帮我看看 下面代码那里出问题了
[打印本页]
作者:
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中慢慢调试呀~~ 下面代码是我一点点调试出来的,编译通过。
调试中发现,里面很多简单拼写错误,作为一名要去黑马的程序员,你得对自己要求高点!
package com.itheima;
class Exception extends Throwable {
public Exception() {
// TODO Auto-generated constructor stub
}
public Exception(String message) {
}
public static void main(String[] args) {
Teacher t = new Teacher("杨老师");
try {
t.jiangke();
} catch (NoPlanException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
t.jiangke();
} catch (NoPlanException e) {
System.out.println(e.getMessage());
System.out.println("更换电脑或老师");
}
}
}
class LanPinException extends Exception {
LanPinException(String message) {
super(message);
}
}
class MaoYanException extends Exception {
MaoYanException(String message) {
super(message);
}
}
class NoPlanException extends Exception {
NoPlanException(String massage) {
super(massage);
}
}
class Computer {
private int state = 3;
public void run() throws LanPinException, 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.getMessage());
}
System.out.println("讲课");
}
public void test() {
System.out.println("练习");
}
}
复制代码
作者:
xlunaer
时间:
2015-4-13 17:17
问题好多。所以给你几个建议:
1,不知道你是不是学过c语言,java的代码风格不是这样的,大括号不用另起一行。直接空格加大括号即可。
2.遇到好多单词拼写错误,大小写错误。写代码的时候注意一下。如果单词不好,平时花点时间背背单词,写代码会顺畅很多。
3,有几个括号多了或者少了。括号一定要左右两个括号写写好,再写里面的代码,否则容易遗忘。
代码如下,改的不好别介意。
class LanPinException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
LanPinException(String message) {
super(message);
}
}
class MaoYanException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
MaoYanException(String message) {
super(message);
}
}
class NoPlanException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
NoPlanException(String msg) {
// super(massage); //是msg,写代码别太慌
super(msg);
}
}
class Computer {
private int state = 3;
// public void run()throws LanPinExceptin,MaoYanException //是LanPinException
public void run() throws LanPinException, MaoYanException {
// if(state=2) //判断相等用== , = 是赋值
if (state == 2)
throw new LanPinException("电脑蓝屏");
// if(state=3) //同上
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();
// { //大括号放在catch语句后面
throw new NoPlanException("课时无法继续" + e.getMessage());
}
System.out.println("讲课");
}
public void test() {
System.out.println("练习");
}
}
// } //多了一个大括号
class Exception {
public static void main(String[] args) {
// Teather t = new Teather("杨老师"); //单词拼写错误
Teacher t = new Teacher("杨老师");
// t.jiangke(); //注释掉吧,下面一样的
try {
t.jiangke();
}
// catch (NoplanException e) //大写
catch (NoPlanException e) {
// System.out.println(e.getMassage); //单词拼写错误 & 调用方法别忘了后面的括号
System.out.println(e.getMessage());
System.out.println("更换电脑或老师");
}
}
// 少了一个打阔靠
}
复制代码
作者:
Searching
时间:
2015-4-13 17:18
你最好用IDE编辑一下,你这段代码问题太多,列出来要一页纸。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2