在Java分配中遇到问题从循环中查找最大值

时间:2012-10-11 23:09:13

标签: java loops while-loop

我在另一个作业之后对代码进行建模,因此可能有一部分或两部分不能很好地保留。基本上我无法得到GPA最高的老人的输出数据正确显示;它并不总能获得两个专业的正确平均GPA。

package studentdata;

import javax.swing.JOptionPane;

public class studentdata 
{

    public static void main(String[] args) 
{
String lastname = null,major = null,advisor_name = null,studentyear = null,studentgpa,output="";
int year,count_math=0,count_CIS=0,year1=0,year2=0,year3=0,year4=0,Johnson_count=0,Atkins_count=0,Smith_count=0;
double gpa=0,CIS_average=0,Math_average=0,high_gpa=0,Math_total=0,CIS_total=0;
String high_advisor="";
String high_student="";
String more_data = "yes";


while(more_data.equals("yes"))
{
    //Read in 5 sets of data

        lastname=JOptionPane.showInputDialog(null,"Enter Student's Last Name",
                "Input Data", JOptionPane.QUESTION_MESSAGE);


        studentyear=JOptionPane.showInputDialog(null,"Enter Student's Year (1,2,3,4)",
                "Input Data", JOptionPane.QUESTION_MESSAGE);
        year=Integer.parseInt(studentyear);


        major=JOptionPane.showInputDialog(null,"Enter Student's Major (Math or CIS)",
                "Input Data", JOptionPane.QUESTION_MESSAGE);

        studentgpa=JOptionPane.showInputDialog(null,"Enter Student's GPA",
                "Input Data", JOptionPane.QUESTION_MESSAGE);
        gpa=Double.parseDouble(studentgpa);

        advisor_name=JOptionPane.showInputDialog(null,"Enter Advisor Name (Johnson, Atkins, or Smith)",
                "Input Data", JOptionPane.QUESTION_MESSAGE);

        //Count the student's major
        if(major.equals("Math"))
            count_math=count_math+1;
        else
            count_CIS=count_CIS+1;

        //Count the student's year
        if(studentyear.equals("1"))
            year1=year1+1;
        if(studentyear.equals("2"))
            year2=year2+1;
        if(studentyear.equals("3"))
            year3=year3+1;
        if(studentyear.equals("4"))
            year4=year4+1;

        //Calculate total CIS and Math GPA for averaging
        if(major.equals("CIS"))
        CIS_total=gpa+gpa;
        if(major.equals("Math"))
        Math_total=gpa+gpa;

        //Calculate average CIS GPA
        if(major.equals("CIS"))
        CIS_average=CIS_total/count_CIS;

        //Calculate average Math GPA
        if(major.equals("Math"))
        Math_average=Math_total/count_math;

        //Count the advisees
        if(advisor_name.equals("Johnson"))
            Johnson_count=Johnson_count+1;
        else
        if(advisor_name.equals("Atkins"))
            Atkins_count=Atkins_count+1;
        else
            Smith_count=Smith_count+1;

        //Ask if more data
        more_data=JOptionPane.showInputDialog(null,"More Info? Enter yes or no",
                "Input Dialog",JOptionPane.QUESTION_MESSAGE);


}//End of while loop


//Output number of students at each year
output=output+"There are "+year1+" freshmen\n";
output=output+"There are "+year2+" sophomores\n";
output=output+"There are "+year3+" juniors\n";
output=output+"There are "+year4+" seniors\n";


//Find major with the most students
if(count_CIS>count_math)
output=output+"The CIS major has the most students\n";
else
output=output+"The Math major has the most students\n";

//Output advisee data
output=output+"Johnson has "+Johnson_count+" advisees\n";
output=output+"Atkins has "+Atkins_count+" advisees\n";
output=output+"Smith has "+Smith_count+" advisees\n";

//Output averages
output=output+"The average GPA for all CIS majors is "+CIS_average+"\n";
output=output+"The average GPA for all Math majors is "+Math_average+"\n";

//Output Senior Data
if(studentyear.equals("4"))
output=output+"The senior with the highest GPA is "+high_student+"\n";
output=output+"Their major is "+major+"\n";
output=output+"Their GPA is "+high_gpa+"\n";
output=output+"Their advisor is "+high_advisor+"\n";


//Output Advisor for Highest GPA senior
if (gpa>high_gpa)
{
    high_gpa=gpa;
    high_student=lastname;
    high_advisor=advisor_name;
}

//Display the output box
JOptionPane.showMessageDialog(null,output,"Output",JOptionPane.INFORMATION_MESSAGE) ;

System.exit(0);
} //End class
} //End method

1 个答案:

答案 0 :(得分:0)

if(major.equals("CIS"))
CIS_total=gpa+gpa;
if(major.equals("Math"))
Math_total=gpa+gpa;

应该是

if(major.equals("CIS"))
    CIS_total += gpa;
if(major.equals("Math"))
    Math_total += gpa;

并且不计算循环中的平均值。在循环之后做。