GPA计算器打印不起作用,有什么不对?

时间:2014-03-16 19:54:27

标签: java string parsing methods integer

我想知道为什么我的方法无法使我的java"正确打印" 我知道它可能需要重新安排变量和解析......

帮助我完成这项工作!

包分配5;

导入javax.swing.JOptionPane;

公共课作业5 {

        /**
         * @param args
         */
public static void main(String[] args) 
{
      String name,advisor,major,more_data = "yes", value, output="", grade;
    int year = 0,count1=0,count2=0,count3=0,count4=0;
      int johnson_count=0,smith_count=0,mathcount=0,ciscount=0;
      double mathtotalgpa=0,cistotalgpa=0;
      double GPA=0,high_GPA=0,cis_average,math_average;
      String high_advisor="",high_name="",high_major="",advisor_most="";

    while(more_data.equals("yes"))
    {
    // READ THE DATA FIRST
        name=get_name();    
        grade=get_year();
        major=get_major();
        value=get_value();
        advisor=get_advisor();  

        //COUNT THE YEARS
        if(year==1)
           count1=count1+1;
        else
            if(year==2)
              count2=count2+1;
            else
              if(year==3)
                 count3=count3+1;
              else
                if (year==4)
                 count4=count4+1;

    //COUNT THE AMOUNT OF STUDENTS PER ADVISOR
    if(advisor.equals("Johnson"))
          johnson_count=johnson_count+1;
    else
        if(advisor.equals("Smith"))
          smith_count=smith_count+1;

    //CHECK IF CIS OR MATH
    if(major.equals("Math"))
       {
       mathtotalgpa =  mathtotalgpa+GPA;
       mathcount = mathcount + 1;
       }
    if (major.equals("CIS"))
       {
       cistotalgpa = cistotalgpa + GPA;
       ciscount = ciscount + 1;
       }

    //NAME OF STUDENT WITH HIGHEST GPA, SAVE THE NAME, MAJOR AND ADVISOR
    if(year==4)
        {
            if(GPA>high_GPA)
            {
              high_GPA=GPA;
              high_name=name;
              high_advisor=advisor; 
              high_major=major;
            }
        }
    //Ask if more data
    more_data=JOptionPane.showInputDialog(null,"More data? yes/no",
            "Input Data",JOptionPane.QUESTION_MESSAGE); 
    }//END OF WHILE LOOP

    //NAME OF ADVISOR HAVING MOST ADVISEES
           if(johnson_count>smith_count) 
               advisor_most="Johnson";
           else 
               advisor_most="Smith";

    //MAJOR HAVING THE MOST STUDENTS
           if(mathcount>ciscount)
               high_major="Math";
           else
               high_major="CIS";

    //AVERAGE GPA CALCULATION - OUTSIDE OF THE LOOP
    cis_average= calc_avg_cis (cistotalgpa, ciscount);
    math_average=calc_avg_math (mathtotalgpa, mathcount);

    //Output data
    printing(count1, count2, count3, count4,
            johnson_count,smith_count,
            high_major,
            math_average, cis_average, 
            high_name,high_GPA, high_advisor);
    System.exit(0);
    }

    public static String get_name()
    {
    String name=JOptionPane.showInputDialog(null,"Enter student name ",
            "Input Data",JOptionPane.QUESTION_MESSAGE);
    return name;
    }

    public static String get_year()
    {
    String grade=JOptionPane.showInputDialog(null,"Enter the year, 1,2,3,or 4  ",
            "Input Data",JOptionPane.QUESTION_MESSAGE); 
    int year = Integer.parseInt(grade);
    return grade;
    }

    public static String get_major()
    {
    String major=JOptionPane.showInputDialog(null,"Enter the major, Math or CIS  ",
            "Input Data",JOptionPane.QUESTION_MESSAGE); 
    return major;
    }

    public static String get_value()
    {
    String value=JOptionPane.showInputDialog(null,"Enter the GPA  ",
            "Input Data",JOptionPane.QUESTION_MESSAGE); 
    double GPA=Double.parseDouble(value);
    return value;
    }

    public static String get_advisor()
    {
    String advisor=JOptionPane.showInputDialog(null,"Enter Advisor ",
            "Input Data",JOptionPane.QUESTION_MESSAGE);
    return advisor;
    }

    public static double calc_avg_cis (double cistotalgpa, int ciscount)
    {
        double cis_average=cistotalgpa/ciscount;
        return cis_average;
    }

    public static double calc_avg_math (double mathtotalgpa, int mathcount)
    {
        double math_average=mathtotalgpa/mathcount;
        return math_average;
    }

    private static void printing(int count1, int count2, int count3, int count4, 
        int johnson_count, int smith_count,
        String high_major, double math_average, double cis_average,
        String high_name, double high_GPA, String high_advisor) {

    //OUTPUT
            String output="";
    output=output+"The total number of freshman = "+count1+"\n";
    output=output+"The total number of sophomores = "+count2+"\n";
    output=output+"The total number of juniors = "+count3+"\n";
    output=output+"The total number of seniors = "+count4+"\n"+"\n";

    output=output+"The total number of advisees for Dr. Johnson = "+johnson_count+"\n";
    output=output+"The total number of advisees for Dr. Smith = "+smith_count+"\n"+"\n";

    output=output+"The major having the most students = "+high_major+"\n";

    output=output+"The average GPA for Math majors = "+math_average+"\n";
    output=output+"The average GPA for CIS majors = "+cis_average+"\n"+"\n";

    output=output+"The Senior, "+high_name+" had the highest GPA of "+high_GPA+"\n";
    output=output+"The Student is a "+high_major+" major, and the advisor is "+high_advisor;                

    JOptionPane.showMessageDialog(null, output, "Output:", JOptionPane.INFORMATION_MESSAGE);
    }
    }

1 个答案:

答案 0 :(得分:1)

我可以确切地看到你做了什么。这只是因为我有一些空闲时间并通过了所有你的代码并使其可读。你是java新手吗?你的代码真的很混乱。

无论如何,你的问题在于代码的这一部分:

// READ THE DATA FIRST
name=get_name();    
grade=get_year();
major=get_major();
value=get_value();
advisor=get_advisor();  

,并且,通过扩展,使用get_year()和get_value()方法(为了清晰和良好的做法,我将其重命名为getYear()和getGPA())。

以getYear()为例:

    public static String getYear()
{
String grade=JOptionPane.showInputDialog(null,"Enter the year, 1,2,3,or 4  ",
        "Input Data",JOptionPane.QUESTION_MESSAGE); 
int year = Integer.parseInt(grade);
return grade;
}

您正在解析整数,将其保存为年份,然后对其执行任何操作。 它应该是这样的:

public static int getYear()
    {
        String grade = JOptionPane.showInputDialog(null,"Enter the year, 1,2,3,or 4  ",
                "Input Data",JOptionPane.QUESTION_MESSAGE); 
        int year = Integer.parseInt(grade);
        return year;
    }

顺便说一下,学习如何使用" + ="。

相关问题