呆鸟问题求助!
原来提问的帖子又点乱,现在我来整理一下。
这是我编的一个小程序,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]));
}
}