读书人

编程有关问题

发布时间: 2011-12-18 22:54:38 作者: rapoo

编程问题
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);                  }    } 


[解决办法]

加个count[]数组,把统计的个数存入数组就行了

其实你的这个程序还有可改进的地方,比如,可一次性输入任意个成绩,成绩之间用空格隔开。代码如下:

Java code
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import javax.swing.JOptionPane;public class Tangjifengshu2 {    public static void main(String[] s) throws IOException    {        int i=0;        int best=0;        char grade='0';        System.out.println("Pls input your scores around spaces:");        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));        String st=(String) br.readLine();        System.out.println(st);                String []result=st.split("\\s+");    //将字符串中的数字字符转化为整数        int []scores=new int[result.length];                 for( i=0;i <scores.length;i++)        {             scores[i]=Integer.parseInt(result[i]);    //将转化成的整数存入数组中            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);                  }    } 

读书人网 >J2SE开发

热点推荐