我是Java新手,我们要写一个琐事游戏,让玩家两次尝试猜测答案。在游戏结束时,我们要计算他们答错了多少题。
我写了一个可以计算尝试次数的方法的伪代码,但我不确定如何编写它,并使用for循环来计算问题是否正确/错误,然后计数。这包括那些在第一次尝试时就猜对了的,以及那些在最初的错误响应后改变为正确的。
如果有人能帮助我,我会很感激的!
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//Introduction
System.out.println("Welcome to the Animal Answers Trivia Game!");
System.out.println("You will be answering questions about various animals and " +
"tested on your knowledge of furry critters to slimy snakes.");
//Ask at least 5 questions and prompt for answers.
//First Question
System.out.println("What types of animal(s) can't vomit?");
String input = scan.nextLine(); //get input
String answer = "Rodents";
public void guess ();
//Second Question
System.out.println("What is the only mammal capable of flight?");
String input = scan.nextLine(); //get input
String answer = "Bats";
public void guess ();
//Third Question
System.out.println("What mammal has the most powerful bite");
String input = scan.nextLine(); //get input
String answer = "Hippos";
public void guess ();
//Fourth Question
System.out.println("Where is the only place that dogs have sweat glands?");
String input = scan.nextLine(); //get input
String answer = "paws";
public void guess ();
//Fifth Question
System.out.println("What is a male duck called?");
String input = scan.nextLine(); //get input
String answer = "drake";
public void guess ();
//Closing message with how many answers were correct
public void correct (guess) {
int correct = 0
// for(get questions and answers)
//if answer correct {
//increment correct
//}
//} return correct
//Write any additional methods you need here
public static boolean guess (String answer, Scanner input){
int guess = 0;
for (int guess = 0; guess < 2; guess++) {
if (input.nextLine().equalsIgnoreCase(answer)) {
System.out.println("Good job, you got the correct answer!");
} else {
System.out.println("Try again!");
if (input.nextLine().equalsIgnoreCase(answer)) { //need a nested if else this is if they got it wrong the second time
System.out.println("Good job, you got the correct answer! Let's move onto the next question")
} else {
System.out.println("Sorry! Answer is incorrect. Time for the next question!");
}
}
guess++;
}
}
3条答案
按热度按时间llycmphe1#
你在看问题的时候要保持这种状态。你已经声明了
public static boolean guess()
,但实际上并没有返回一个布尔值。因此,将其更改为基于答案正确与否返回。然后,如果在
main
方法中有public void guess ();
,则将其替换为guess
调用,并在方法返回true
时增加正确答案的总数。qrjkbowd2#
首先,我想让你完成并运行你所拥有的代码。删除这些行:每个
public void guess ();
,public void correct (guess) {
。此外,请确保{
和}
正确配对和嵌套。请记住,您不能在另一个方法中编写一个方法。这个例子是正确的:但是,这是不正确的:
另外,如果你有一个非void返回类型的方法,则该方法中的代码必须通过该方法中的任何执行路径返回指定类型的值。例如,如果你有一个
int
返回类型,代码必须有一个return
语句和一个int
表达式。表达式可以是常数。对于int
返回类型方法,以下内容有效:return 3;
return arg * arg + 7;
return foo;
当你得到一个干净的编译,运行你的代码。至少有一个问题,给予不正确的答案。
我想,做了这件事,你不需要更多的帮助
让我们继续一个设计技巧。此部分是可选的。你重复了代码。这可能是你的老师,在这一点上,希望你有重复的代码,因为下一课将是关于如何避免重复的代码。
如果你想让代码重复,请考虑使用以下几种方式的组合:
class
定义。由于您是Java新手,我假设
Collection
对象将在以后的课程中介绍。你的代码有几对,每对都有一个问题和一个答案。将问题与答案配对的一种方法是使用新的
class
:这样,你就可以得到一个
QuizEntry
数组:如果组成一对的项目是相同类型的,另一种方法是使用2D数组:
现在,你可以有一个循环来运行你的测验。但是,在此之前,在获得和处理答案方面存在冲突。在你的代码中,以一个问题为例,你有这样的:
在上面,显示问题,检索用户的答案并等待处理。处理可能由
public static boolean guess (String answer, Scanner input)
方法处理。问题是这个方法也从用户那里得到答案。这意味着用户必须在评估答案之前输入两次答案。第一个答案被忽略了。第二个是加工。如果第二个用户响应不正确,则将处理第三个响应。现在,回到循环:
现在,更新代码并运行它。不要担心完成计算正确答案的部分。#21463;,并删除
*******
。它不会与他们编译。这样做反映了“编写一点代码,测试一点”的做法。如果您还没有找到并修复它,您可能会发现guess
方法中仍然存在一个bug。现在,在我有
*******
的地方,你应该做更多的修改。返回类型为
int
的方法将返回int
,并且可以在可以使用int
表达式的地方使用。如果bin
、bar
、bat
都是一个方法,它接受一个int
并返回一个int
,那么你可以得到这样的语句:int foo = bar (3) * bin (k) + bat (7);
int frob = bar (3) * m + (bat (bin (8));
在可以使用
boolean
表达式的地方,可以使用返回boolean
的方法:您可以在
if
中使用它:您可以将其分配给另一个
boolean
:因此,您可以使用
uess
方法返回的结果来更新correct
的值或不更新它。7y4bm7vi3#
使用 record 或 class 来包含每个问题和答案。
还有一个 Map,用来表示回答是否正确。
然后遍历 map。
“……在比赛结束时,我们必须计算他们做错了多少题。..."*
我用的是你的伪代码。
计算 map 条目的数量,并使用 *true**值 *。
或者,一条小溪。
下面是一个示例输出。