当前位置: 技术问答>java相关
幫我看一下,為什麼會出錯?
来源: 互联网 发布时间:2015-10-03
本文导语: 幫我看一下,為什麼會出錯: ---------- 編譯java ---------- BankOperation.java:128: unreachable statement theBank.doTransaction(inTray); ^ 1 error Normal Termination Output completed (3 sec consumed). ////////////////// class ...
幫我看一下,為什麼會出錯:
---------- 編譯java ----------
BankOperation.java:128: unreachable statement
theBank.doTransaction(inTray);
^
1 error
Normal Termination
Output completed (3 sec consumed).
//////////////////
class Bank
{
public void doTransaction(Transaction transaction)
{
int balance = transaction.getAccount().getBalance();
switch(transaction.getTransactionType())
{
case Transaction.CREDIT:
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
System.out.println(e);
}
balance +=transaction.getAmount();
break;
case Transaction.DEBIT:
try
{
Thread.sleep(150);
}
catch (InterruptedException e)
{
System.out.println(e);
}
balance -=transaction.getAmount();
break;
default:
System.out.println("Invalid transaction");
System.exit(1);
}
transaction.getAccount().setBalance(balance);
}
}
class Transaction
{
public static final int DEBIT =0;
public static final int CREDIT = 1;
public static String[] types = {"Debit","Credit"};
//Constructor
public Transaction(Account account,int transactionType ,int amount)
{
this.account = account;
this.transactionType = transactionType;
this.amount = amount;
}
public Account getAccount()
{
return account;
}
public int getTransactionType()
{
return transactionType;
}
public int getAmount()
{
return amount;
}
public String toString()
{
return types[transactionType] + " A//C: " + ": $"+amount;
}
private Account account;
private int amount;
private int transactionType;
}
class Account
{
public Account(int accountNumber,int balance)
{
this.accountNumber = accountNumber;
this.balance = balance;
}
public void setBalance(int balance)
{
this.balance = balance;
}
public int getBalance()
{
return this.balance = balance;
}
public int getAccountNumber()
{
return accountNumber;
}
public String toString()
{
return "A//C NO. " + accountNumber + " : $" + balance;
}
private int balance;
private int accountNumber;
}
class Clerk implements Runnable
{
private Bank theBank;
private Transaction inTray;
public Clerk(Bank theBank)
{
this.theBank = theBank;
inTray = null;
}
public void doTransaction(Transaction transaction)
{
inTray = transaction;
}
public void run()
{
while (true)
{
while (inTray==null)
{
try
{
Thread.sleep(150);
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
這一句出錯 theBank.doTransaction(inTray);
inTray = null;
}
public boolean isBusy()
{
return inTray !=null;
}
}
public class BankOperation
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
---------- 編譯java ----------
BankOperation.java:128: unreachable statement
theBank.doTransaction(inTray);
^
1 error
Normal Termination
Output completed (3 sec consumed).
//////////////////
class Bank
{
public void doTransaction(Transaction transaction)
{
int balance = transaction.getAccount().getBalance();
switch(transaction.getTransactionType())
{
case Transaction.CREDIT:
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
System.out.println(e);
}
balance +=transaction.getAmount();
break;
case Transaction.DEBIT:
try
{
Thread.sleep(150);
}
catch (InterruptedException e)
{
System.out.println(e);
}
balance -=transaction.getAmount();
break;
default:
System.out.println("Invalid transaction");
System.exit(1);
}
transaction.getAccount().setBalance(balance);
}
}
class Transaction
{
public static final int DEBIT =0;
public static final int CREDIT = 1;
public static String[] types = {"Debit","Credit"};
//Constructor
public Transaction(Account account,int transactionType ,int amount)
{
this.account = account;
this.transactionType = transactionType;
this.amount = amount;
}
public Account getAccount()
{
return account;
}
public int getTransactionType()
{
return transactionType;
}
public int getAmount()
{
return amount;
}
public String toString()
{
return types[transactionType] + " A//C: " + ": $"+amount;
}
private Account account;
private int amount;
private int transactionType;
}
class Account
{
public Account(int accountNumber,int balance)
{
this.accountNumber = accountNumber;
this.balance = balance;
}
public void setBalance(int balance)
{
this.balance = balance;
}
public int getBalance()
{
return this.balance = balance;
}
public int getAccountNumber()
{
return accountNumber;
}
public String toString()
{
return "A//C NO. " + accountNumber + " : $" + balance;
}
private int balance;
private int accountNumber;
}
class Clerk implements Runnable
{
private Bank theBank;
private Transaction inTray;
public Clerk(Bank theBank)
{
this.theBank = theBank;
inTray = null;
}
public void doTransaction(Transaction transaction)
{
inTray = transaction;
}
public void run()
{
while (true)
{
while (inTray==null)
{
try
{
Thread.sleep(150);
}
catch (InterruptedException e)
{
System.out.println(e);
}
}
}
這一句出錯 theBank.doTransaction(inTray);
inTray = null;
}
public boolean isBusy()
{
return inTray !=null;
}
}
public class BankOperation
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
|
把他放到jb里面去编译,就很容易看到是那里有问题了。
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。