日期:2014-05-20  浏览次数:20768 次

编程问题
public class tongjifenshu {
public static void main(String[] s)
{
int numberstudents=0;
int[] scores;
  int i=0;
  int best=0;
char grade='0';
String numberstudentsString=JOptionPane.showInputDialog(null,"Please enter number of students:","软件班

  级",JOptionPane.QUESTION_MESSAGE);
numberstudents=Integer.parseInt(numberstudentsString);
scores=new int[numberstudents];
for( i=0;i<scores.length;i++)
{ String scoreString=JOptionPane.showInputDialog(null,"Please enter a score","软件班级",JOptionPane.QUESTION_MESSAGE);
scores[i]=Integer.parseInt(scoreString);
if(scores[i]>best)
best=scores[i];
}
  String output=" ";
   
for( i=0;i<scores.length;i++)
{  
  if(scores[i]>=best-10)
grade='A';
   
  else if(scores[i]>=best-20)
grade='B';
   
  else if(scores[i]>=best-30)
grade='C'
   
  else if(scores[i]>=best-40)
grade='D';
  else
grade='E';  
   
   
  output +="Studets "+i+" score is "+scores[i]+" and grade is "+grade+ "\n";
   
   
  }
   
JOptionPane.showMessageDialog(null,output,"软件班级",JOptionPane.INFORMATION_MESSAGE);
  }
}




统计分数后我像分别计数分每个grade的个数。例如:‘A’有几个~~~~~~~~~。在另一个方框中输出。  


------解决方案--------------------
Java code
import javax.swing.JOptionPane;

public class Tangjifengshu {
    public static void main(String[] s)
    {
        int numberstudents=0;
        int[] scores;
        int i=0;
        int best=0;
        char grade='0';
        String numberstudentsString=JOptionPane.showInputDialog(null,
                "Please enter number of students:","软件班级",
                JOptionPane.QUESTION_MESSAGE);
        numberstudents=Integer.parseInt(numberstudentsString);
        scores=new int[numberstudents];
        for( i=0;i <scores.length;i++)
        { 
            String scoreString=JOptionPane.showInputDialog(null,"Please enter a score","软件班级",JOptionPane.QUESTION_MESSAGE);
            scores[i]=Integer.parseInt(scoreString);
            if(scores[i]>best)
                    best=scores[i];
        }
        
        String output=" ";
        int []count;
        count =new int[5];
                                 
        for( i=0;i <scores.length;i++)
        {        
                if(scores[i]>=best-10){
                    grade='A';
                    count[0]++;
                }                                                                  
                else if(scores[i]>=best-20){
                    grade='B';
                    count[1]++;
                }                                              
                else if(scores[i]>=best-30){
                    grade='C';
                    count[2]++;
                }                                                
                else if(scores[i]>=best-40){
                    grade='D';
                    count[3]++;
                }
                else{
                    grade='E';
                    count[4]++;
                    }                     
                output +="Studets  "+(i+1)+"  score is   "+scores[i]+"  and grade is   "+grade+  "\n";
                                         
                                                 
         }
        char c='A';
        for (int j = 0; j < count.length; j++) {
            output+="\n"+(c++)+":  "+count[j];
        }
        JOptionPane.showMessageDialog(null,output,"软件班级",
                JOptionPane.INFORMATION_MESSAGE);
        
          }
    }