public class 投票统计 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int x;
int[] numbers = new int[10];//创建数组
x = in.nextInt();
while(x!=-1){
if(x>=0&&x<=9){
numbers[x] = numbers[x] + 1;//数组参与运算
}
x = in.nextInt();
}
for(int i=0;i<numbers.length;i++){
System.out.println(i+":"+numbers[i]);//遍历数组输出
}
}
}
上面这个例子看不懂唉,求大神点拨
Please read the comments. In fact, you will understand what is going on if you run this program.
The voter enters the number to vote from the console. If he votes for No. 3, then x = 3 in the program, and then adds one to the number of votes for No. 3
numbers[x] = numbers[x] + 1;
Finally print out the number of votes each person got.