呆鸟问题求助!

link155 2004-02-19 11:25:25
原来提问的帖子又点乱,现在我来整理一下。
这是我编的一个小程序,COMPILE的时候没有问题,但运行的时候说我array out of bounds.看了几遍书,也没看出个所以来。现求助!

这个题大概的意图是要求输入候选人名及他得到的选票,在算出他的得票率。

要求输出:

candidant votes percentage
tom 256 **%
lee 254 **%
......
等,一直到你输入结束的信号。要求用数组,array, 谢谢各位牛人!!





import java.io.*;
import java.util.*;
import java.text.DecimalFormat;
public class votescaculation2
{
public static void main(String[] args) throws FileNotFoundException,IOException,ArrayIndexOutOfBoundsException

{
BufferedReader keyboard= new BufferedReader( new InputStreamReader(System.in));
DecimalFormat twodecimal= new DecimalFormat("0.00");
String[] candidatenames= new String[5];
int[] Votes=new int[5];
double[] percentage=new double[5];
int index=0;
double votes=0.00,sum=0.00;
String candidatename=null;
StringTokenizer tokenizer;
tokenizer= new StringTokenizer(keyboard.readLine());
candidatename = tokenizer.nextToken();
while (!candidatename.equals("9999"))
{

for(index=0;index<=4;index++);
{

candidatenames[index] = candidatename;
Votes[index] = Integer.parseInt(tokenizer.nextToken());

sum=sum+Votes[index];



}
tokenizer=new StringTokenizer(keyboard.readLine());
candidatename=tokenizer.nextToken();

}


for(index=0;index<=4;index++)
{
votes=Votes[index];
percentage[index]=100*votes/sum;


}
System.out.println("candidate name "+"votes received "+"% of Total Votes");
for(index=0;index<=4;index++)
System.out.println(candidatenames[index]+" "+Votes[index]+" "+twodecimal.format(percentage[index]));




}
}
...全文
19 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧