【问题标题】:If statement containing char not executing如果包含 char 的语句未执行
【发布时间】:2014-09-23 23:39:13
【问题描述】:

我正在尝试创建一个程序来测试数组中每十个位置中“正确”答案的数量。我发现即使满足条件,带有条件textarray[i * 10 + k] == answerarray[k] 的 if 循环也不会执行(我已经验证了这一点)。

else if 语句 textarray[i * 10 + k] == ' ' 正在工作,所以我不明白为什么前者没有。任何帮助将不胜感激!

char[] textarray = new char[textfile.length()];
for (int i = 0; i < textfile.length(); i++) {
    textarray[i] = textfile.charAt(i);
}

double[] eachquestiona = new double[10];
double[] eachquestionb = new double[10];
double[] eachquestionc = new double[10];
double[] eachquestiond = new double[10];
double[] eachquestione = new double[10];

//stores how many questions each student got correct, incorrect or left blank
int[] studentcorrect = new int[textfile.length() / 10];
int[] studentincorrect = new int[textfile.length() / 10];
int[] studentblank = new int[textfile.length() / 10];

for (int i = 0; i < textfile.length() / 10; i++) {
    for (int k = 0; k < 10; k++) {
        if (textarray[i * 10 + k] == 'A') {
            eachquestiona[i]++;
        }

        if (textarray[i * 10 + k] == 'B') {
            eachquestionb[i]++;
        }

        if (textarray[i * 10 + k] == 'C') {
            eachquestionc[i]++;
        }

        if (textarray[i * 10 + k] == 'D') {
            eachquestiond[i]++;
        }

        if (textarray[i * 10 + k] == 'E') {
            eachquestione[i]++;
        }

        if (textarray[i * 10 + k] == answerarray[k]) {
            studentcorrect[i]++;
        }
        else if (textarray[i * 10 + k] == ' ') {
            studentblank[i]++;
        }
        else {
            studentincorrect[i]++;
        }
    }
}

【问题讨论】:

  • 我认为重复的for (int i = 0; ... 是错字?
  • answerarray 是如何声明的?
  • 顺便说一句,String 有一个 toCharArray() 方法可以让你的第一个循环变得不必要。
  • 你试过简单的调试语句吗?也许这会有所帮助:System.out.println(textarray[i * 10 + k] + " " + answerarray[k]);
  • 谢谢大家,我想通了。原来这是我的代码早期的一个问题。我有 for (int i = 0; i

标签: java if-statement char


【解决方案1】:

好的,这有点乱,所以我将在你的 SO 代码 reivew 中进行第一遍:

首先,您需要解决 Edd 和 Baldy 指出的问题。我们还需要知道什么是“文本文件”?如果没有数据类型,我不确定“charAt”或“length”是否按照您的预期进行评估。

另外,不确定这是否会导致您的问题,但您的索引被搞砸了。你应该使用

每个问题[k]++

不是

每个问题[i]++

我还将 A 和“answerArray”之间的“if”语句更改为“else if”语句(不应超过一个),将空白支票移到它们之后,然后让你的“answerarray”在最后一个 else 不正确之前检查最后一个(前面没有“else if”)。它不能同时是“A”和“B”和“”,您的代码应该反映这一点。它要么正确要么不正确(理论上''可能是正确答案)。

编辑:

实际上我会对这段代码进行很多更改以使其更清晰(这可能会使错误跳到你身上),这是我对它的修改:

String textFile=//Initialize it here and show us how you did
final int NUMQUESTIONS = 10; //This may become a variable later
final int NUMSTUDENTS = textFile.length() / NUMQUESTIONS; //Probably should check that this is correct
double[] eachquestiona = new double[NUMQUESTIONS];
double[] eachquestionb = new double[NUMQUESTIONS];
double[] eachquestionc = new double[NUMQUESTIONS];
double[] eachquestiond = new double[NUMQUESTIONS];
double[] eachquestione = new double[NUMQUESTIONS];

//stores how many questions each student got correct, incorrect or left blank
int[] studentcorrect = new int[NUMSTUDENTS];
int[] studentincorrect = new int[NUMSTUDENTS];
int[] studentblank = new int[NUMSTUDENTS];

for (int studentCount = 0; studentCount < NUMSTUDENTS; studentCount++) {
    for (int answerCount = 0; answerCount < NUMQUESTIONS; answerCount++) {
        char thisAnswer=Character.toUpperCase(charAt(studentCount * NUMQUESTIONS + answerCount));
        switch(thisAnswer) {
            case 'A': eachquestiona[answerCount]++; break;
            case 'B': eachquestionb[answerCount]++; break;
            case 'C': eachquestionc[answerCount]++; break;
            case 'D': eachquestiond[answerCount]++; break;
            case 'E': eachquestione[answerCount]++; break;
            case ' ': studentblank[studentCount]++; break;
        }

        if (thisAnswer == Character.toUpperCase(answerarray[answerCount])) {
            studentcorrect[studentCount]++;
        } else {
            studentincorrect[studentCount]++;
        }
    }
}

如果有错别字,请见谅——我实际上并没有运行这个。

【讨论】:

    猜你喜欢
    • 2022-12-16
    • 2022-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 2013-10-18
    • 2019-12-03
    • 1970-01-01
    相关资源
    最近更新 更多