0

你不必阅读我所有的代码,不用担心。我的主要问题在此消息的正文中突出显示。

我对编程很陌生。我应该创建一个Java程序来读取字符串输入,将该字符串分成映射到数组的字符(该数组是用于回答多项选择问题的备忘录)。然后从用户读取另一个字符串输入,将该输入也分成数组中的字符,然后比较两个数组索引。基本上,它应该查看有多少数组索引是相同的,然后生成正确答案的分数(这是一个多项选择答案程序,因此字符串中的字符将是 AB 或 C)。

我必须存储学生(用户)的姓名并保留他的分数。但是程序会询问您有多少学生,然后运行一个循环询问姓名,并回答(与您输入的学生数量一样多次。最大为 10),然后将答案与备忘录进行比较,生成一个分数,并使用所有学生的答案计算一个平均分数。到目前为止,我已经设法创建了一个可以为一个用户执行此操作的程序。我遇到的问题是循环存储多个用户名和分数,以计算平均分数。

这是我的代码。如果您将其复制并粘贴到您的 IDE 中,您会发现它运行良好(除了一些错误处理和JOptionPane对话框按钮内容遗漏)。但它只有在您输入用户数为 1 时才会起作用。其他任何东西,它都不会正常运行。那么如何让它为多个用户循环呢?

package multiquestions;

import javax.swing.JOptionPane;

public class MultiQuestions {

  public static void main(String[] args) {
    String aMemo = getInput0();
    int aStudentNumber = getInput1();
    String aStudentName = getInput2();
    String aStudentAnswer = getInput3();
    int aScore = CalcScore(aStudentAnswer, aMemo);
    if (aStudentNumber == 1) {
      StudentInfo(aStudentName, aScore);
    }
    System.exit(0);
  }
  public static String getInput0() {
    String Memo = JOptionPane
        .showInputDialog("Please enter the memo answers to the 10 multiple choice questions\nno spaces and all in Capital letters e.g.ABCDEABCDE");
    return Memo;
  }
  public static int getInput1() {
    int StudentNumber = Integer.parseInt(JOptionPane
        .showInputDialog("Please enter the number of students in the class"));
    return StudentNumber;
  }
  public static String getInput2() {
    String StudentName = JOptionPane.showInputDialog("Please enter the student's name");
    return StudentName;
  }
  public static String getInput3() {
    String StudentAnswer = JOptionPane
        .showInputDialog("Please enter the student's answers to the 10 multiple choice questions\nno spaces and all in Capital letters e.g.ABCDEABCDE");
    return StudentAnswer;
  }
  public static int CalcScore(String bStudentAnswer, String bMemo) {
    int One;
    int Two;
    int Three;
    int Four;
    int Five;
    int Six;
    int Seven;
    int Eight;
    int Nine;
    int Ten;
    char Reference[] = new char[10];
    for (char Ref : Reference) {
      Reference[0] = bMemo.charAt(0);
      Reference[1] = bMemo.charAt(1);
      Reference[2] = bMemo.charAt(2);
      Reference[3] = bMemo.charAt(3);
      Reference[4] = bMemo.charAt(4);
      Reference[5] = bMemo.charAt(5);
      Reference[6] = bMemo.charAt(6);
      Reference[7] = bMemo.charAt(7);
      Reference[8] = bMemo.charAt(8);
      Reference[9] = bMemo.charAt(9);
    }
    char Answer[] = new char[10];
    for (char Ans : Answer) {
      Answer[0] = bStudentAnswer.charAt(0);
      Answer[1] = bStudentAnswer.charAt(1);
      Answer[2] = bStudentAnswer.charAt(2);
      Answer[3] = bStudentAnswer.charAt(3);
      Answer[4] = bStudentAnswer.charAt(4);
      Answer[5] = bStudentAnswer.charAt(5);
      Answer[6] = bStudentAnswer.charAt(6);
      Answer[7] = bStudentAnswer.charAt(7);
      Answer[8] = bStudentAnswer.charAt(8);
      Answer[9] = bStudentAnswer.charAt(9);
    }
    /*
     * Below is the list of if statements which add one to each variable (Variables One to Ten
     * declared at top of this method) if the array characters match each other at their indeces
     */
    if (Reference[0] == Answer[0]) {
      One = 1;
    } else {
      One = 0;
    }
    if (Reference[1] == Answer[1]) {
      Two = 1;
    } else {
      Two = 0;
    }
    if (Reference[2] == Answer[2]) {
      Three = 1;
    } else {
      Three = 0;
    }
    if (Reference[3] == Answer[3]) {
      Four = 1;
    } else {
      Four = 0;
    }
    if (Reference[4] == Answer[4]) {
      Five = 1;
    } else {
      Five = 0;
    }
    if (Reference[5] == Answer[5]) {
      Six = 1;
    } else {
      Six = 0;
    }
    if (Reference[6] == Answer[6]) {
      Seven = 1;
    } else {
      Seven = 0;
    }
    if (Reference[7] == Answer[7]) {
      Eight = 1;
    } else {
      Eight = 0;
    }
    if (Reference[8] == Answer[8]) {
      Nine = 1;
    } else {
      Nine = 0;
    }
    if (Reference[9] == Answer[9]) {
      Ten = 1;
    } else {
      Ten = 0;
    }
    int Score = One + Two + Three + Four + Five + Six + Seven + Eight + Nine + Ten;
    return Score;
  }
  public static void StudentInfo(String bStudentName, int bScore) {
    switch (bScore) {
    /*
     * Below is case stament for values of bScore(score returned from CalcScore() For each value of
     * bScore it must do something specific
     */
    case 10:
      JOptionPane.showMessageDialog(null, bStudentName + "     " + bScore + "/10     A     Marvelous");
      break;
    case 9:
      JOptionPane.showMessageDialog(null, bStudentName + "     " + bScore + "/10     A     Marvelous");
      break;
    case 8:
      JOptionPane.showMessageDialog(null, bStudentName + "     " + bScore + "/10     B     Outstanding");
      break;
    case 7:
      JOptionPane.showMessageDialog(null, bStudentName + "     " + bScore + "/10     C     Significant");
      break;
    case 6:
      JOptionPane.showMessageDialog(null, bStudentName + "     " + bScore
          + "/10     D     Above Average");
      break;
    case 5:
      JOptionPane.showMessageDialog(null, bStudentName + "     " + bScore
          + "/10     E     Barely Adequate");
      break;
    case 4:
      JOptionPane.showMessageDialog(null, bStudentName + "     " + bScore + "/10     F     Fail");
      break;
    case 3:
      JOptionPane.showMessageDialog(null, bStudentName + "     " + bScore + "/10     F     Fail");
      break;
    case 2:
      JOptionPane.showMessageDialog(null, bStudentName + "     " + bScore + "/10     F     Fail");
      break;
    case 1:
      JOptionPane.showMessageDialog(null, bStudentName + "     " + bScore + "/10     F     Fail");
      break;
    case 0:
      JOptionPane.showMessageDialog(null, bStudentName + "     " + bScore + "/10     F     Fail");
    }
  }
  public static void Exit() {
    int exit = JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?");
    if (exit == JOptionPane.YES_OPTION) {
      System.exit(0);
    }
  }
  public static void CalcAverage() {}
}
4

2 回答 2

0

而不是说 if == 1,做

for (int i = 0; i < aStudentNumber; i++) {
  String aStudentName = getInput2();
  String aStudentAnswer = getInput3();
  int aScore = CalcScore(aStudentAnswer, aMemo);

  StudentInfo(aStudentName, aScore);
}

现在,如果您想以编程方式存储该数据以便以后可以访问它,请将您的 StudentInfo 方法更改为类似

public static StudentInfo createStudentInfo(String bStudentName, int bScore){
  ...
  return someObj;
}

其中someObj是 StudentInfo 类的一个实例(您必须创建),它实际上包含您要跟踪的所有数据。然后,只需将该类添加到列表中(在循环内):

ArrayList<StudentInfo> siList = new ArrayList<StudentInfo>();

for (int i = 0; i < aStudentNumber; i++) {
    String aStudentName = getInput2();
    String aStudentAnswer = getInput3();
    int aScore = CalcScore(aStudentAnswer, aMemo);

    //StudentInfo(aStudentName, aScore); // get rid of this line since we are adding it to a list now
    siList.add(createStudentInfo(aStudentName, aScore));
}

现在,如果需要,您可以调用所有这些数据。

最重要的是你需要一个循环。循环直到索引达到您的计数,并对每个答案集执行相同的操作。

编辑:以下是解决评论中发布的问题。

我认为您最好使用构造函数。使用这样的类:

public class StudentInfo {

    private String bStudentName;
    private int bScore;

    public StudentInfo (String bStudentName, int bScore) {
        this.bStudentName = bStudentName;
        this.bScore = bScore;
    }

    public String getStudentName() {
        return this.bStudentName; //'this' keyword not really necessary, but helpful
    }

    public int getScore() {
        return this.bScore;// again, 'this' not necessary
    }

    public void doStuff() { // optional method to do stuff with the data if need be
        ...
    }
}

然后,在你的循环中,做

ArrayList<StudentInfo> siList = new ArrayList<StudentInfo>();
for (int i = 0; i < aStudentNumber; i++) {
    String aStudentName = getInput2();
    String aStudentAnswer = getInput3();
    int aScore = CalcScore(aStudentAnswer, aMemo);

    //StudentInfo(aStudentName, aScore); // get rid of this line since we are adding it to a list now
    //siList.add(createStudentInfo(aStudentName, aScore));
    StudentInfo si = new StudentInfo(aStudentName, aScore);
    si.doStuff(); // optional method call in case you want to do something with the data other than store it.
    siList.add(si);
}

您的StudentInfo课程应该与您的主课程在同一个包中。如果由于某种原因不是,请在主类的顶部添加include theotherpackage.StudentInfo;

于 2013-06-24T12:21:39.403 回答
0

可能有助于学习目的(假设字符串不为空并且字符串的长度相等):

public static int CalcScore(String bStudentAnswer, String bMemo) {
    char[] answer = bStudentAnswer.toCharArray();
    char[] memo = bMemo.toCharArray();
    int score = 0;
    for (int i = 0; i < answer.length; i++) {
        if (answer[i] == memo[i]) score++;
    }
    return score;
}
于 2013-06-24T12:30:19.433 回答