AccountTest.java class BankAccount //定義銀行賬戶類BankAccount{private static int amount =2000; //賬戶余額最初為2000public void despoit(int m) //定義存款的方法{amount=amount+m;System.out.println("曉明存入["+m+"元]");}public void withdraw(int m) //定義取款的方法{amount=amount-m;System.out.println("張新取走["+m+"元]");if(amount0)System.out.println("***余額不足!***);public int balance() //定義得到賬戶余額的方法{return amount;}}classicCustomerextendsThread {String name;BankAccount bs; //定義一個(gè)具體的賬戶對(duì)象public Customer(BankAccount b,String s){name=s;bs=b;}public static void cus(String name,BankAccount bs) //具體的賬戶操作方法{if(name.equals("小明")) //判斷用戶是不是小明{try{for(int i=0;i6;i++) //用戶曉明則向銀行存款6次,每次1000元 {Thread.currentThread().sleep((int)(Math.random()*300));bs.despoit(1000);}}catch(InterruptedException e){}}else{try{for(int i=0;i6;i++) //用戶不是小明則從銀行取款6次,每次1000元{Thread.currentThread().sleep((int)(Math.random()*300));bs.withdraw(1000); }}catch(InterruptedException e){} }}public void run() //定義run方法}cus(name,bs); }}public classAccountTest{public static void main(String [] args) throws InterruptedException{BankAccount bs=new BankAccount();Customer customer1=new Customer(bs,"小明");Customer customer2=new Customer(bs,"張新");Thread t1=new Thread(customer1);Thread t2=new Thread(customer2);t1.Start();t2.start();Thread.currentThread().sleep(500);}}

創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),廣安企業(yè)網(wǎng)站建設(shè),廣安品牌網(wǎng)站建設(shè),網(wǎng)站定制,廣安網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,廣安網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
你的代碼問題很多!
幫你修改后:
class Account {
private static float Balance = 200f;//線程共享的錢
public synchronized static void deposit(float amt) {
Balance = Balance + amt;
try {
Thread.sleep(10);// 模擬其它處理所需要的時(shí)間,比如刷新數(shù)據(jù)庫等
}
catch (InterruptedException e) {
}
}
public static float getBalance() {
return Balance;
}
public static void setBalance(float balance) {
Balance = balance;
}
public synchronized static void withdraw(float bmt) {
Balance -= bmt;
try {
Thread.sleep(10);// 模擬其它處理所需要的時(shí)間,比如刷新數(shù)據(jù)庫等
}
catch (InterruptedException e) {
}
}
String name;
public Account(String name) {
this.name = name;
}
}
public class AccountTest {
public static void main(String[] args) {
Customer customerA = new Customer("A");
Customer customerB = new Customer("B");
customerA.start();
customerB.start();
System.out.println();
try {
customerA.join();//等待A線程執(zhí)行完畢
customerB.join();//等待B線程執(zhí)行完畢
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("最后總錢數(shù):" + Account.getBalance());
}
}
class Customer extends Thread {
public Customer(String string) {
super(string);
}
@Override
public void run() {
System.out.println(currentThread().getName());
if (currentThread().getName().equals("A"))
Account.deposit(100.0f);//A線程加100
else if (currentThread().getName().equals("B"))
Account.withdraw(50.0f);//B線程減50
}
}
private?int?balance?=?0;
private??String?username?=?"A";
private??String?password?=?"B";
public?void?bank()?{
Scanner?scan?=?new?Scanner(System.in);
String??temp;
while?(true)?{
System.out.println("輸入賬號(hào):");
if?(scan.hasNext())?{
temp?=?scan.next();
if?(temp.equals(username))?{
break;
}?else?{
System.out.println("輸入錯(cuò)誤");
}
}
}
while?(true)?{
System.out.println("輸入密碼:");
if?(scan.hasNext())?{
temp?=?scan.next();
if?(temp.equals(password))?{
break;
}?else?{
System.out.println("輸入錯(cuò)誤");
}
}
}
System.out.println("登錄成功");
while?(true)?{
System.out.println("輸入操作:");
if?(scan.hasNext())?{
temp?=?scan.next();
switch?(temp)?{
case?"存款":
int?x?=?0;
while?(true)?{
System.out.println("輸入存款金額:");
if?(scan.hasNextInt())?{
x?=?scan.nextInt();
break;
}?else?{
System.out.println("輸入錯(cuò)誤");
scan.next();
}
}
balance?+=?x;
break;
case?"取款":
int?y?=?0;
while?(true)?{
System.out.println("輸入取款金額:");
if?(scan.hasNextInt())?{
y?=?scan.nextInt();
if?(balance??y)?{
System.out.println("余額不足");
continue;
}
break;
}?else?{
System.out.println("輸入錯(cuò)誤");
scan.next();
}
}
balance?-=?y;
break;
case?"余額":
System.out.println("余額:"?+?balance);
break;
case?"終止":
System.exit(0);
default:
System.out.println("未知操作");
}
}
}
分享名稱:java代碼活期存款 活期存期代碼
網(wǎng)站路徑:http://chinadenli.net/article2/dodjjic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、App開發(fā)、網(wǎng)站制作、虛擬主機(jī)、微信公眾號(hào)、手機(jī)網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)