加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何使用我的程序解决这些问题? (内部解释了很多问题)

发布时间:2020-12-15 08:32:03 所属栏目:Java 来源:网络整理
导读:这个问题不适合胆小的人,而且耗费时间. 我正在学习Java,我想知道是否有人可以通过我的程序告诉我我做错了什么以及如何解决它以便我可以学习. 我的整个程序(如果需要的话):http://pastie.org/private/xpbgpypetvfmivcs88gbcq 分配: Not allowed to use glob
这个问题不适合胆小的人,而且耗费时间.

我正在学习Java,我想知道是否有人可以通过我的程序告诉我我做错了什么以及如何解决它以便我可以学习.

我的整个程序(如果需要的话):http://pastie.org/private/xpbgpypetvfmivcs88gbcq

分配:

Not allowed to use global variables!
You have been asked to write a program
to grade the results of a true-false
quiz and display the results in
tabular form. The quiz consists of 10
questions. The data file for this
problem consists of 1 set of correct
responses (answerkey) on line one and
succeeding lines containing a four
digit student identification number
followed by that student’s 10
responses. (The number of students
taking the quiz in currently unknown.)

The data file is in the form :

TFFTFTTFTT

0461 TTFTTFTFTT

3218 TFFTTTTTFT

…………………

………………..

Your program should read the key and
store it in a one dimensional array.
It should then read and process each
succeeding line,storing the student
identification numbers in a one
dimensional array and the grades in
two separate one dimensional arrays-
one for numeric grades and the for
letter grades.

Numeric grades are converted to letter
according to the following system :
A(10),B(9),C(8-7),D (6-5),F(4-0).

Program output should include the
following :

  1. Each Student’s four digit identification number,their numeric
    grade and their letter grade.

  2. A printed count of how many students took the quiz.

  3. The numeric quiz average for the entire class.

  4. A frequency count of the number of A’s,B’s,C’s,D’s and F’s for the
    entire class.

编辑:

噢!嗨.对不起,我仍然知道如何做到这一切:(好吧,这里有.

该程序没有编译,我很确定这取决于我的逻辑. (以及一些句法的东西).

我遇到的问题是:
1)如何将所有参数传递给每个不同的方法,如此处所示.我想要做的是让我返回的所有值,例如answerKey,studentAnswers,numericGrade,average等,这样我就可以使用它们并打印出来.但我不知道该怎么做.

int[] numericGrade = computeNumericGrade(answerKey,studentAnswers);
      int[] letterGrade = computeLetterGrade(numericGrade);
      double average = average(numericGrade);
      int gradeTally = gradeTally(letterGrade);
      int studentCount = studentCount(numericGrade);

2)我无法弄清楚如何单独检查每个T或F值并检查它是否与答案键中的T或F相对应,如下所示.

我要做的是检查特定元素的每个字母,如果值是“正确”或==到answerKey,则在该元素的numericGrade中添加总数.

public static int[] computeNumericGrade(int[] answerKey,int[] studentAnswers) {
      int[] numericGrade = new int[50];
      int total = 0;
      for(int i = 0; i < 50;i++) {
        for(int a = 0;a == 10;a++) {
          total = studentAnswers[i].charAt(a);
          if(total == answerKey[i]) numericGrade[i]++;
        }
      }
    return numericGrade;
    }

3)我不知道如何返回所有这些不同的值,因为我需要它们以上所有!我希望这组代码检查每个元素的字母等级,如果找到这个元素,那么对应它的变量,所以我知道这个字母等级有多少被调用.

public static int gradeTally(String[] letterGrade) {
      int a,b,c,d,f = 0;
      for(int i = 0;i < letterGrade.length;i++) {
        if(letterGrade[i] == 'A') a++;
        else if (letterGrade[i] == 'B') b++;
        else if (letterGrade[i] == 'C') c++;
        else if (letterGrade[i] == 'D') d++;
        else if (letterGrade[i] == 'F') f++;
      }
      return a + b + c + d + f;
    }

4)我无法弄明白……我想我很亲近.在这里,我正在尝试读取文件,对其进行标记,然后对于每个标记,将其添加到数组1中.但是,我不认为这是正确的,但我无法检查,因为我可以不能编译……

public static String[] getData() throws IOException {
      int[] studentID = new int[50];
      String[] studentAnswers = new String[50];
      int total = 0;

      String line = reader.readLine();
      strTkn = new StringTokenizer(line);
      String answerKey = strTkn.nextToken();

      while(line != null) {
        studentID[total] = Integer.parseInt(strTkn.nextToken());
        studentAnswers[total] = strTkn.nextToken();
        total++;
      }
    return studentAnswers;
    }

我现在正在编辑我的帖子.我希望这足够了:)

解决方法

我猜你从评论中意识到这有点难以帮助.

我不确定它会有多大帮助,但我想提供一些关于如何处理这个问题的一般提示.

首先,我可能会扔掉你所拥有的并重新开始.我从不遗憾地丢弃代码 – 从长远来看,它总能帮助你的代码,第二次你会学到很多东西.

其次,从小做起.做一些有用的东西 – 即使它只是读入值并证明它们被正确读入,然后展开它.经常测试.每当您添加代码时,请在测试中添加一行以确保新代码的工作方式与之前的所有代码一致

这样一来,如果有什么破坏,你就会确切地知道是什么打破了它 – 你编辑的最后一件事.

要进行这种快速测试,您应该学会使用JUnit进行单元测试,或者让主要测试用于测试,这样您就可以点击一个按钮并立即得到答案. (JUnit真的不难,它内置于Eclipse中.哦,是的,下一个提示). .

使用ECLIPSE或NetBeans!

这些都是有经验的程序员自动知道和做的事情.从一开始就花费一点时间和精力可以节省很多痛苦.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读