Topic: 请大家帮我改一下这个程序好吗? |
Print this page |
1.请大家帮我改一下这个程序好吗? | Copy to clipboard |
Posted by: dumxz Posted on: 2006-02-17 13:26 这个程序不知为何,一直都无法编译运行,请大家帮我看看,应该如何更改。 public class Li904 implements Runnable { Account acc; public Li904() { } public void run() { acc.deposit(200.0f); acc.withdraw(100.0f); } private static int NUM_OF_THREAD = 10; static Thread[] threads = new Thread[NUM_OF_THREAD]; public static void main(String[] args){ final Account acc = new Account("王红", 1000.0f); for (int i = 0; i< NUM_OF_THREAD; i++) { threads[i] = new Thread(new Li904()); threads[i].start(); } for (int i=0; i<NUM_OF_THREAD; i++){ try { threads[i].join(); //等待所有线程运行结束 } catch (InterruptedException e) { } } System.out.println("完成,王红的帐户余额为:" + acc.getBalance()); } } class Account { String name; float amount; public Account(String name, float amount) { this.name = name; this.amount = amount; } public void deposit(float amt) { float tmp = amount; tmp += amt; try { Thread.sleep(1);//模拟其它处理所需要的时间,比如刷新数据库等 } catch (InterruptedException e) { } amount = tmp; } public void withdraw(float amt) { float tmp = amount; tmp -= amt; try { Thread.sleep(1);//模拟其它处理所需要的时间,比如刷新数据库等 } catch (InterruptedException e) { } amount = tmp; } public float getBalance() { return amount; } } |
2.Re:请大家帮我改一下这个程序好吗? [Re: dumxz] | Copy to clipboard |
Posted by: joelwx Posted on: 2006-02-17 16:04 我COPY下来,编译可以过的. 执行有问题... 帮你把错误贴上来!! (缩略图,点击图片链接看原图) |
3.Re:请大家帮我改一下这个程序好吗? [Re: dumxz] | Copy to clipboard |
Posted by: joelwx Posted on: 2006-02-17 16:11 你那个Li904里的Account acc好像没有new哦~~ 修改部份: public Li904(Account acc) { this.acc=acc; } ============== public static void main(String[] args){ final Account acc = new Account("王红", 1000.0f); for (int i = 0; i< NUM_OF_THREAD; i++) { threads[i] = new Thread(new Li904(acc)); threads[i].start(); } ============== 以下是执行结果: 完成,王红的帐户余额为:1100.0 试试看~~ |
4.Re:请大家帮我改一下这个程序好吗? [Re: joelwx] | Copy to clipboard |
Posted by: dumxz Posted on: 2006-02-17 23:19 我根据您说的修改过了,怎么还是出现错误呀。 public class Li904 implements Runnable { public Li904(Account acc) { this.acc = acc; } public void run() { acc.deposit(20.0f); acc.withdraw(10.0f); } private static int NUM_OF_THREAD = 100; static Thread[] threads = new Thread[NUM_OF_THREAD]; public static void main(String[] args){ final Account acc = new Account("王红", 1000.0f); for (int i = 0; i< NUM_OF_THREAD; i++) { Li904 my = new Li904(acc); threads[i] = new Thread(my); threads[i].start(); } for (int i=0; i<NUM_OF_THREAD; i++){ try { threads[i].join(); //等待所有线程运行结束 } catch (InterruptedException e) { } } System.out.println("完成,王红的帐户余额为:" + acc.getBalance()); } } class Account { String name; float amount; public Account(String name, float amount) { this.name = name; this.amount = amount; } public void deposit(float amt) { float tmp = amount; tmp += amt; try { Thread.sleep(1);//模拟其它处理所需要的时间,比如刷新数据库等 } catch (InterruptedException e) { } amount = tmp; } public void withdraw(float amt) { float tmp = amount; tmp -= amt; try { Thread.sleep(1);//模拟其它处理所需要的时间,比如刷新数据库等 } catch (InterruptedException e) { } amount = tmp; } public float getBalance() { return amount; } } |
5.Re:请大家帮我改一下这个程序好吗? [Re: dumxz] | Copy to clipboard |
Posted by: dumxz Posted on: 2006-02-17 23:26 多谢,已经修改好了。 |
6.请高手指教 [Re: dumxz] | Copy to clipboard |
Posted by: xingchao Posted on: 2006-02-25 19:48 //AbstractClass.java import java.awt.*; import java.applet.*; abstract class Shapes //抽象类 { public int x,y; public int width,height; public Shapes(int x,int y,int width,int height) //构造方法 { this.x=x; this.y=y; this.width=width; this.height=height; } abstract double getArea(); //抽象方法说明 abstract double getPerimeter(); //抽象方法说明 } class Square extends Shapes //定义Square类,它是从Shapes抽象类扩展的 { public double getArea() //抽象方法的实现 { return(width*height); //计算矩形面积 } public double getPerimeter() //抽象方法的实现 { return(2*width+2*height); } public Square(int x,int y,int width, int height) { super(x,y,width,height); //调用超类的方法 } } class Circle extends Shapes //定义Circle类,它是从Shapes抽象类扩展的 { public double r; public double getArea() //抽象方法的实现 { return(r*r*Math.PI); //计算圆面积 } public double getPerimeter() //抽象方法的实现 { return(2*Math.PI*r); //计算圆的周长 } public Circle(int x,int y,int width,int height) //构造方法 { super(x,y,width,height); //调用超类的方法 r=(float)width/2.0; } } public class AbstractClass extends Applet { Square Box = new Square(5,15,20,20); //创建Square类对象,并赋初值 Circle Oval = new Circle(5,50,20,20); //创建Circle类对象,并赋初值 public void paint(Graphics g) { g.drawRect(Box.x, Box.y, Box.width,Box.height); //画矩形 g.drawString("Area:"+Box.getArea(),50,35); //按指定位置显示矩形面积 g.drawOval(Oval.x, Oval.y, Oval.width, Oval.height); //画圆形 g.drawString("Area:"+(float)Oval.getArea(),50,70); //按指定位置显示圆面积 } } 这个程序编译时通过了 。但是,在运行时却出现了这样的状况 Exception in thread “main ” java.lang. NoSuchMethodError:main 请问这是怎么回事?? |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |