0

帮助,我无法让我的程序成功运行。它给了我一个错误,其中有两个输入为期中分数和期末分数。我无法让它在适当的结束弹出时完全运行。我尝试编写一个程序,该程序接受用户的分数并为未知数量的学生显示适当的字母等级。我的代码如下。

        import javax.swing.*;
    public class StudentGrades_Hann{
        public static void main (String[] args) {
            String input ;
            input= JOptionPane.showInputDialog(" Enter Name, \n ID Number,\n Homework Mark, \n Assignment Mark, \n Midterm Mark, \n and Final Mark \n All Entrys Must Be Separated By Commas");

            int com;
            com= input.indexOf(",");

            String name ;
            name = input.substring(0,com);

            int com2;
    com2= input.indexOf(",", com+1);

    String idnumber ;
    idnumber = input.substring(com+1, com2);

    int com3;
    com3= input.indexOf(",", com2+1);

    String homeworkmark ; 
    homeworkmark = input.substring(com2+1, com3);

    int com4;
    com4= input.indexOf(",", com3+1);

        String assignmentmark ; 
        assignmentmark = input.substring(com3+1, com4);

        int com5;
        com5= input.indexOf(",", com4+1);

        String midtermmark ; 
        midtermmark = input.substring(com4+1, com5);

        int com6;
        com6= input.indexOf(",", com5+1);

        String finalmark ; 
        finalmark = input.substring(com4+1);

        double as ;
        as = Double.parseDouble (assignmentmark.trim());

        double hw ;
        hw = Double.parseDouble (homeworkmark.trim());

        double mm ;
        mm = Double.parseDouble (midtermmark.trim());



        double fm ;
        fm = Double.parseDouble (finalmark.trim());

        String grade ; 

        double totalmark ; 
        totalmark= 0.1*hw+0.1*as+0.3*mm+0.5*fm ;

        double mnf ;
        mnf= 0.5*fm+0.3*mm ;

        if(totalmark < 50 || mnf < 50) {
            JOptionPane.showMessageDialog(null,name + idnumber + "Fail") ;
        }
        else if (totalmark < 85) {
            JOptionPane.showMessageDialog(null, name + idnumber + "A") ;
        }

        else if (totalmark < 75) {
            JOptionPane.showMessageDialog(null,name + idnumber + "B") ;
        }

        else if (totalmark <65) {
            JOptionPane.showMessageDialog(null, name + idnumber + "C");
        }

        else if (totalmark <=50) {
            JOptionPane.showMessageDialog(null,name + idnumber + "D") ;
        }

        else {
            JOptionPane.showMessageDialog(null, "Invalid Entry") ;
        }

    }

    }   
4

2 回答 2

1

您没有更新,下一个值的索引。

对于第二个值,如果输入是name, 1, 1, 1, 1, 1,它将尝试解析 "1, 1"

使用String.split(","),检查数组中的元素数量是否符合您的预期,然后解析它们,就像这样的伪代码:

String[] strings = input.split(",");
if (string.length == 6) { //name and 5 values
    name = strings[0];
    id = strings[1];
    ...
} else {
       //provided values are incorrect
}

`

于 2013-03-13T16:19:51.080 回答
0

我收到异常错误“线程“main”中的异常 java.lang.NumberFormatException:对于输入字符串:“80, 90”

您正在尝试在代码中无法解析的某处解析“80,90” double 。所以你得到了 NumberFormatException。

因此,与其在查找 indexOf 和 substring 操作上做这么多的工作。试试看嘛

String[] splitted = input.split(",");

您在拆分给定字符串时犯了某个错误。我发现了错误。

String finalmark ; 
    finalmark = input.substring(com4+1);

应该

String finalmark ; 
    finalmark = input.substring(com5+1);
于 2013-03-13T16:23:16.953 回答