java - 我是否过度简化了返回?

标签 java arrays return

基本上,我正在为一门大学类(class)做老司机考试计划,并且我已经起草了整个类(class)。当我尝试编译它时,如果在命令提示符下工作,我会收到错误消息:

missing return statement

在 Eclipse 中:

This method must return a result of type int` pertaining to my methods re missed questions.

现在我想我在尝试编译之前就已经知道问题出在哪里了——我的方法是 int 类型,并且因为我的回答是“array is char”,所以当我尝试返回 'index + 1' 或 'index++) 它返回实际元素,一个字符(即答案 a、b、c、d)。我想做的是返回下标数字 + 1(删除一个错误),所以当我编写程序时,我可以系统输出“你错过了问题 1、3、5 等”。

我意识到这段代码中可能还有一百万个其他错误,但现在我只是希望有人能帮助我解决这个问题。意识到这可能很简单而且很愚蠢,但是我已经阅读论坛和我的教科书好几个小时了,还是想不通。也许我试图通过仅使用下标 + 1 作为显示遗漏问题编号的方式来简化太多。

public class DriverExam {

private char[] rightAnswers = { 'b', 'd', 'a', 'a', 'c', 'a', 'b', 'a',   
'c', 'd', 'b', 'c', 'd', 'a', 'd', 'c', 'c', 'b', 'd', 'a' }; //Answers 
to test.
char[] Answers; //Student answer input.
int[] missed = {}; //Array for missed questions.
int correct = 0; //Create and initialise to 0.
int qMissed = 0; //Create and initialise to 0.

/** 
Constructor that accepts array of answers and copies to the answers array 
field.
@parem ans The array of student driver answers */


public DriverExam(char[] ans)
{
Answers = ans;
}

/**
An int array containing the question numbers of those questions that the 
student missed.
*/

public int questionsMissed() 

{
for (int index = 0; index < 20; index++) //Ask program to step through 
each element in array Answers.
    {
    if (Answers[index] == 0) //I'm assuming here that a question not 
    answered = 0, not null as it would for string.
        return index + 1; //I want to return the subscript assigned to 
    any questions that = 0 ie haven't been answered, plus 1 to avoid out 
    by one error.
    }
    }

public int qMissed() 
{
for (int index = 0; index < 20; index++) //Ask program to step through    
each element in array Answers.
    {
    if (Answers[index] == 0) //Asking it to repeat process above.
        return index++; //I want to ask it to take the subscript and add 
1 to the existing subscript for all additional missed questions.
        }
}


/**
A method that returns the total number of correctly answered questions.
@return Returns the number of correctly answered questions.
*/

public int totalCorrect()
{
for (int index = 0; index < Answers.length; index++)
{
if (Answers[index] == rightAnswers[index]) 
correct++;
}
return correct;
}


/**
A method that returns the total number of incorrectly answered questions.
@return Returns the number of incorrect answers.
*/

public int totalIncorrect()
{
    int incorrect = (rightAnswers.length - (totalCorrect() + qMissed));
    return incorrect;
}

/**
A method that returns true if the student passed the exam, or false if    
the student failed.
*/


public boolean passed()
{
    if(totalCorrect() >= 10); return true;
}

}

最佳答案

您需要添加一行来为 else 部分返回一个 int。当函数中有一个非 void 返回类型时,您需要为所有代码分支 (if-else) 显式返回一个值。

public int questionsMissed() 

{
for (int index = 0; index < 20; index++) 
    {
    if (Answers[index] == 0) 
        return index + 1; 
    }

return a int here for ex return -1

关于java - 我是否过度简化了返回?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30010083/

相关文章:

java - 在android中使用公钥加密

java - 如果我在不同的国家/地区,我如何知道特定国家/地区的时间?

c++ - 如何使数组变量指向不同的数组,有效地将第二个数组的内容复制到第一个数组中?

r - 从 R 中的函数中捕获警告并仍然获得它们的返回值?

python - 列表未返回正确的值作为输出

java - 将 Double 转换为OptionalDouble 的方法

java - 在 Java 内部,Liquibase 更新在应用变更集后挂起

ios - 如何在 Swift 中对字典类型的数组应用 removeAtIndex?

c# - 在 WHERE 查询中使用数组作为比较

java - 为什么当返回值不同时我的排序方法返回 0?