package com; class Account { String name; double balance; public Account(String name) { this.name=name; this.balance=0; } public synchronized void put(double value) { if(value>0) this.balance+=value; System.out.println("您的存款金额为"+value+"存款成功,余额为"+this.balance); } public synchronized double get( double value) { if(value<=0) return 0; if(value<=this.balance) this.balance-=value; else { value=this.balance; this.balance=0; } System.out.println("您的取款金额为"+value+"取款成功,余额为"+this.balance); return value; } } class SaveThread extends Thread{ private Account account; private double value; public SaveThread(Account account,double value) { this.account=account; this.value=value; } public void run() { try {Thread.sleep(1);} catch(InterruptedException ex) {} this.account.put(this.value); } } class FetchThread extends Thread{ private Account account; private double value; public FetchThread(Account account,double value){ this.account = account; this.value = value; } public void run(){ try{ Thread.sleep(1); } catch(InterruptedException ex){} this.account.get(this.value); } public static void main(String[] args) { Account wang =new Account("Wang"); (new SaveThread(wang,100)).start(); (new SaveThread(wang,200)).start(); (new SaveThread(wang,300)).start(); (new FetchThread(wang,100)).start(); (new FetchThread(wang,200)).start(); } }代码查错
时间: 2024-03-20 14:41:03 浏览: 67
在这段代码中,存在一些语法错误和逻辑问题,下面是修正后的代码:
```java
package com;
class Account {
String name;
double balance;
public Account(String name) {
this.name = name;
this.balance = 0;
}
public synchronized void put(double value) {
if (value > 0) {
this.balance += value;
System.out.println("您的存款金额为" + value + "存款成功,余额为" + this.balance);
}
}
public synchronized double get(double value) {
if (value <= 0)
return 0;
if (value <= this.balance) {
this.balance -= value;
System.out.println("您的取款金额为" + value + "取款成功,余额为" + this.balance);
return value;
} else {
System.out.println("您的取款金额为" + value + "取款失败,余额为" + this.balance);
return 0;
}
}
}
class SaveThread extends Thread {
private Account account;
private double value;
public SaveThread(Account account, double value) {
this.account = account;
this.value = value;
}
public void run() {
try {
Thread.sleep(1);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
this.account.put(this.value);
}
}
class FetchThread extends Thread {
private Account account;
private double value;
public FetchThread(Account account, double value) {
this.account = account;
this.value = value;
}
public void run() {
try {
Thread.sleep(1);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
this.account.get(this.value);
}
}
public class Main {
public static void main(String[] args) {
Account wang = new Account("Wang");
(new SaveThread(wang, 100)).start();
(new SaveThread(wang, 200)).start();
(new SaveThread(wang, 300)).start();
(new FetchThread(wang, 100)).start();
(new FetchThread(wang, 200)).start();
}
}
```
修正后的代码中,主要修改了以下几个问题:
1. 在Account类的put方法和get方法中,需要用大括号括起来表示if语句的范围。
2. 在FetchThread类的get方法中,需要在取款失败的情况下打印错误信息。
3. 在SaveThread和FetchThread的run方法中,需要捕获InterruptedException异常并打印它的堆栈信息。
4. 在主函数中,需要将Main类声明为public,才能被其他类所访问。
经过以上修正,代码应该可以正确运行了。
阅读全文