| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 602 人关注过本帖
标题:初调试程序经常遇到类似问题@
只看楼主 加入收藏
雨蒙珍珍
Rank: 1
等 级:新手上路
威 望:1
帖 子:169
专家分:0
注 册:2006-4-10
收藏
 问题点数:0 回复次数:0 
初调试程序经常遇到类似问题@

大家好.我是一个初学者.在调试程序的时候总回有这样的问题!我下面就举个简单的例子!请诸位学长帮帮我!在这里谢谢了!






这是一个书上的例子


/**
* this program shows multiple threads can safely access a data structure ,using
* sysnchsonized methods
*/
public class SynchBankTest2 {

public static void main(String[] args) {
Bank b = new Bank(100,1000);
for(int i = 0; i < 100; i++)
{
TransferRunnable r = new TransferRunnable(b,i,1000);
Thread t = new Thread(r);
t.start();
}

}
}

class Bank {

/**
* Construct the bank
*/
public Bank(int n, double initialBalance) {
accounts = new double[n];
for(int i = 0; i < accounts.length; i++)
{
accounts[i] = initialBalance;
}
}
/**
* Tranfers money from one account to another
*/
public synchronized void transfer(int from, int to, double amount) throws InterruptedException {
while(accounts[from] < amount)
wait();
System.out.print(Thread.currentThread());
accounts[from] -= amount;
System.out.printf(" %10.2 from %d to %d", amount,from, to);
accounts[to] += amount;
System.out.printf(" Total Balance: %10.2f%n",getTotalBalance());
notifyAll();
}
/**
* Gets the sum of all account balances
*/
public synchronized double getTotalBalance() {
double sum = 0;
for(double a : accounts)
{
sum += a;
}
return sum;
}
/**
* Gets the number of accounts int the bank
* return the number of accounts
*/
public int size() {
return accounts.length;
}

private final double[] accounts;
}

/**
* A runnable that tranfers money from an account to other
*/
class TransferRunnable implements Runnable {
/**
*Constuncts a transfer runnable
*/
public TransferRunnable(Bank b, int from, double max) {
bank = b;
fromAccount = from;
maxAmount = max;
}

public void run() {
try
{
while(true)
{
int toAccount = (int) (bank.size() * Math.random());
double amount = maxAmount * Math.random();
bank.transfer(fromAccount,toAccount,amount);
Thread.sleep((int) (DELAY * Math.random()));
}
}
catch (InterruptedException e)
{

}
}

private Bank bank;
private int fromAccount;
private double maxAmount;
private int repetitions;
private int DELAY = 10;

}





调试的时候出现这样的错误提示!

Thread[Thread-1,5,main]Exception in thread "Thread-1" java.util.UnknownFormatConversionException: Conversion = '1'
at java.util.Formatter.checkText(Formatter.java:2500)
at java.util.Formatter.parse(Formatter.java:2464)
at java.util.Formatter.format(Formatter.java:2411)
at java.io.PrintStream.format(PrintStream.java:899)
at java.io.PrintStream.printf(PrintStream.java:800)
at Bank.transfer(SynchBankTest2.java:39)
at TransferRunnable.run(SynchBankTest2.java:86)
at java.lang.Thread.run(Thread.java:595)



类似的问题出现了很多次
我们初学者遇到这样问题怎么样解决呢?




搜索更多相关主题的帖子: 调试 Bank public args 
2007-10-20 10:19
快速回复:初调试程序经常遇到类似问题@
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.064889 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved