java程序制作!
Write a program named ClassifyScores that classifies a series of scores entered by the user into ranges (0-9, 10-19, and so forth). The scores will be numbers between 0 and 100.
Design and implement a program to prompt the user to enter each score separately.
? If the score is within the correct range, then increment the appropriate range
? If the score entered is greater than 100, display an error message, ignore the incorrect score, and prompt for the user to enter the next score.
? If the score entered is less than 0, the program should display the result and terminate.
For example, if the user enters 1 score between 20 and 29, 2 scores between 40 and 49, 3 scores between 50 and 59, etc. The program should display the following.
Range Number of scores
0-9 0
10-19 0
20-29 1
30-39 0
40-49 2
50-59 3
60-69 1
70-79 7
80-89 5
90-100 3
Hint: Create an array with 10 elements, one for each range. When each valid score is entered, increment the appropriate array element. The array index for each range can be calculated by (score / 10). Please note that when the score is 100, the appropriate array index cannot be calculated this way.
Note: The user of your program can enter as many scores as (s)he wishes. The user identifies the end by entering a negative number for score; your program then should display the tabulated scores and exit.
------解决方案--------------------
Java code
int[] number = {0,0,0,0,0,0,0,0,0,0} ; //定义长度为10的int型数组;其中number[0]表示0-9,number[1]表示10-19,依次类推
public void add(int value){ //value为录入的值
if(value==100){
......
}else if(value<0){
......
}else{
int n = value/10 ; //整除,取商,正好是value值对应的number数组的位置
number[n] += 1 ;
display() ;
}
}
public void display(){
//按规定格式打印number数组 ;
}