如果否则只在java中执行else

时间:2016-06-16 14:16:05

标签: java if-statement

所以我正在努力解决这个问题,这似乎是我无法理解的,我在java中是全新的,我需要帮助..所以我正在制作一个简单的程序,它将决定你是否通过了主题传递了主题等级,它似乎只决定了ELSE声明..这里的代码..它真的会帮助我很多..提前谢谢

    String studID=" ",course=" ", name=" ", dept=" ", subj=" ";
    double exam1=0, exam2=0, exam3=0, exam4=0;
    double avg, pgrade;
    String msg=" ";

studID =JOptionPane.showInputDialog("Please fill-out the following fields:"
        + "\nStudent ID: ");
name = JOptionPane.showInputDialog("Name: ");
dept = JOptionPane.showInputDialog("Department: ");
course = JOptionPane.showInputDialog("Course: ");
subj = JOptionPane.showInputDialog("Subject: ");
exam1 = Double.parseDouble(JOptionPane.showInputDialog("First Examination: "));
exam2 = Double.parseDouble(JOptionPane.showInputDialog("Second Examination: "));
exam3 = Double.parseDouble(JOptionPane.showInputDialog("Third Examination: "));
exam4 = Double.parseDouble(JOptionPane.showInputDialog("Final Examination: "));
avg = (exam1 + exam2 + exam3 + exam4)/4;
pgrade =(100 - avg + 10) / 10;

if (avg==100)
{
    msg = "passed - Excellent!";
}else if (avg>100 && avg<89)
{
    msg = "Passed - Very Good!";
}else if (avg>90 && avg<84)
{
    msg = "Passed - Average";
}else if (avg>85 && avg<79)
{
    msg = "Passed - Good";
}else if (avg>80 && avg<74)
{
    msg = "Passed - Satisfactory";
}else if (avg>75 && avg<49)
{
    msg = "Failed";
}else if (avg>50 && avg<0.00)
{   
    msg = "Dropped";
}else if (avg==0.00 && avg<0.00)
{
    msg = "No such Grade";
}else
{
    msg = "Out of Range or Invalid.";
}


JOptionPane.showMessageDialog(null, new JTextArea (
        "|======Student Details=======|"
    + "\n|StudentID:\t" + studID +"\t |"
    + "\n|Name:\t" + name + "\t |"
    + "\n|Department:\t" +dept+ "\t |"
    + "\n|Course:\t"+course+"\t |"
    + "\n|Subject:\t"+subj + "\t |"
    + "\n|=======Grade Details======= |"
    + "\n|First     Second     Third     Fourth  |"
    + "\n|"+exam1+"     "+exam2+"     "+exam3+"     "+exam4+"\t     |"
    + "\n|Average:\t" +avg + "\t|"
    + "\n|Point Grade\t:" +pgrade+"\t|"
    + "\n|Remarks:"+msg+"\t|"
    + "\n|=============================|"));





}

}

2 个答案:

答案 0 :(得分:3)

条件错误,因为要在您必须设置的范围中包含变量

else if(avg>89 && avg<100)

答案 1 :(得分:0)

你的比较开始时是错误的,所以尝试一下我所纠正的内容,它不是最好的,所以你可以改进它。

  String studID=" ",course=" ", name=" ", dept=" ", subj=" ";
    double exam1=0, exam2=0, exam3=0, exam4=0;
    double avg, pgrade;
    String msg=" ";

studID =JOptionPane.showInputDialog("Please fill-out the following fields:"
        + "\nStudent ID: ");
name = JOptionPane.showInputDialog("Name: ");
dept = JOptionPane.showInputDialog("Department: ");
course = JOptionPane.showInputDialog("Course: ");
subj = JOptionPane.showInputDialog("Subject: ");
exam1 = Double.parseDouble(JOptionPane.showInputDialog("First Examination: "));
exam2 = Double.parseDouble(JOptionPane.showInputDialog("Second Examination: "));
exam3 = Double.parseDouble(JOptionPane.showInputDialog("Third Examination: "));
exam4 = Double.parseDouble(JOptionPane.showInputDialog("Final Examination: "));
avg = (exam1 + exam2 + exam3 + exam4)/4;
pgrade =(100 - avg + 10) / 10;

if (avg==100)
{
    msg = "passed - Excellent!";
}else if (avg<100 && avg>89)
{
    msg = "Passed - Very Good!";
}else if (avg<90 && avg>84)
{
    msg = "Passed - Average";
}else if (avg<85 && avg>79)
{
    msg = "Passed - Good";
}else if (avg<80 && avg>74)
{
    msg = "Passed - Satisfactory";
}else if (avg<75 && avg>49)
{
    msg = "Failed";
}else if (avg<50 && avg>0.00)
{   
    msg = "Dropped";
}else if (avg==0.00)//no one gets below zero in  a fair system
{
    msg = "No such Grade";
}else
{
    msg = "Out of Range or Invalid.";
}


JOptionPane.showMessageDialog(null, new JTextArea (
        "|======Student Details=======|"
    + "\n|StudentID:\t" + studID +"\t |"
    + "\n|Name:\t" + name + "\t |"
    + "\n|Department:\t" +dept+ "\t |"
    + "\n|Course:\t"+course+"\t |"
    + "\n|Subject:\t"+subj + "\t |"
    + "\n|=======Grade Details======= |"
    + "\n|First     Second     Third     Fourth  |"
    + "\n|"+exam1+"     "+exam2+"     "+exam3+"     "+exam4+"\t     |"
    + "\n|Average:\t" +avg + "\t|"
    + "\n|Point Grade\t:" +pgrade+"\t|"
    + "\n|Remarks:"+msg+"\t|"
    + "\n|=============================|"));





}}