I would do it like this:
import java.util.Scanner;
public class VoteCount {
public static void main(String[] args) {
//create empty array
int[] votes = new int[5];
//initialise with 0
for (int i=0; i<5; i++){
votes[i] = 0;
}
//input data
input(votes);
}
public static void input(int[] votes)
{
System.out.println("Enter vote number of the candidate results: ");
Scanner kybd = new Scanner(System.in);
int votecount = kybd.nextInt();
while (votecount !=-1) {
votes[votecount]++;
System.out.println("Candidate " + votecount +" Has " +votes[votecount] + " votes");
System.out.println("Enter vote number of the candidate results: ");
votecount = kybd.nextInt();
}
}
}
Now you have to add the functionality that the user is not allowed to enter anything above 4 and below -1, otherwise you will get an exception. Good luck!