我是否过度简化了回报?(Am I oversimplifying returns?)

编程入门 行业动态 更新时间:2024-10-10 08:23:31
我是否过度简化了回报?(Am I oversimplifying returns?)

基本上我正在为一个单一的课程做旧的驾驶员考试计划,我已经完成了整个课程。 当我尝试编译它时,如果在命令提示符下工作,我收到错误:

缺少退货声明

在Eclipse中:

此方法必须返回与我的方法有关的类型int`的结果。

现在我想在尝试编译之前我已经知道问题是什么 - 我的方法是一个int类型,因为我的答案“数组是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; } }

Basically I'm doing the old driver exam program for a uni course and I've drafted out the whole class. When I try to compile it, if working in command prompt, I get the error:

missing return statement

and in Eclipse:

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

Now I think I already knew what the problem was before trying to compile - my method is an int type, and because my answers "array is char", when I try to return 'index + 1' or 'index++) it's returning the actual element, a character (i.e. answer a,b,c, d). What I wanted to do was to return the subscript number + 1 (to remove out by one error), so when I write the program, I can system out print 'you missed question 1, 3, 5 etc'.

I realize there are probably a million other errors in this code, but right now I'm just hoping someone can help me with this one. Realize it's probably simple and stupid but have been reading forums and my textbooks for hours and can't figure this out. Maybe I'm trying to simplify too much by just using the subscript + 1 as a means of showing question number missed.

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; } }

最满意答案

如果你的方法已经声明返回int它必须为任何情况返回int ,如果你想循环所有项并检查数组是否包含'0'。如果所有失败都return -1或throw Exception

public int questionsMissed() { for (int index = 0; index < 20; index++) { if (Answers[index] == 0) { return index + 1; } } return -1;//If nothing works }

注意:您应该以小写字母开始变量名称。 回答而不回答

If your method has declared to return int it must return int for any case,if you want to loop for all items and check whether array contains '0' or not.If all fails return -1 or throw Exception

public int questionsMissed() { for (int index = 0; index < 20; index++) { if (Answers[index] == 0) { return index + 1; } } return -1;//If nothing works }

Note: You should start name of variable with lower case. answer and not Answer

更多推荐

本文发布于:2023-08-07 11:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464166.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:简化了   oversimplifying   returns

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!