java 如何写一个计算问答游戏中正确答案的方法?

uurv41yg  于 2023-10-14  发布在  Java
关注(0)|答案(3)|浏览(130)

我是Java新手,我们要写一个琐事游戏,让玩家两次尝试猜测答案。在游戏结束时,我们要计算他们答错了多少题。
我写了一个可以计算尝试次数的方法的伪代码,但我不确定如何编写它,并使用for循环来计算问题是否正确/错误,然后计数。这包括那些在第一次尝试时就猜对了的,以及那些在最初的错误响应后改变为正确的。
如果有人能帮助我,我会很感激的!

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Scanner scan = new Scanner(System.in);
  5. //Introduction
  6. System.out.println("Welcome to the Animal Answers Trivia Game!");
  7. System.out.println("You will be answering questions about various animals and " +
  8. "tested on your knowledge of furry critters to slimy snakes.");
  9. //Ask at least 5 questions and prompt for answers.
  10. //First Question
  11. System.out.println("What types of animal(s) can't vomit?");
  12. String input = scan.nextLine(); //get input
  13. String answer = "Rodents";
  14. public void guess ();
  15. //Second Question
  16. System.out.println("What is the only mammal capable of flight?");
  17. String input = scan.nextLine(); //get input
  18. String answer = "Bats";
  19. public void guess ();
  20. //Third Question
  21. System.out.println("What mammal has the most powerful bite");
  22. String input = scan.nextLine(); //get input
  23. String answer = "Hippos";
  24. public void guess ();
  25. //Fourth Question
  26. System.out.println("Where is the only place that dogs have sweat glands?");
  27. String input = scan.nextLine(); //get input
  28. String answer = "paws";
  29. public void guess ();
  30. //Fifth Question
  31. System.out.println("What is a male duck called?");
  32. String input = scan.nextLine(); //get input
  33. String answer = "drake";
  34. public void guess ();
  35. //Closing message with how many answers were correct
  36. public void correct (guess) {
  37. int correct = 0
  38. // for(get questions and answers)
  39. //if answer correct {
  40. //increment correct
  41. //}
  42. //} return correct
  43. //Write any additional methods you need here
  44. public static boolean guess (String answer, Scanner input){
  45. int guess = 0;
  46. for (int guess = 0; guess < 2; guess++) {
  47. if (input.nextLine().equalsIgnoreCase(answer)) {
  48. System.out.println("Good job, you got the correct answer!");
  49. } else {
  50. System.out.println("Try again!");
  51. if (input.nextLine().equalsIgnoreCase(answer)) { //need a nested if else this is if they got it wrong the second time
  52. System.out.println("Good job, you got the correct answer! Let's move onto the next question")
  53. } else {
  54. System.out.println("Sorry! Answer is incorrect. Time for the next question!");
  55. }
  56. }
  57. guess++;
  58. }
  59. }
llycmphe

llycmphe1#

你在看问题的时候要保持这种状态。你已经声明了public static boolean guess(),但实际上并没有返回一个布尔值。因此,将其更改为基于答案正确与否返回。
然后,如果在main方法中有public void guess ();,则将其替换为guess调用,并在方法返回true时增加正确答案的总数。

qrjkbowd

qrjkbowd2#

首先,我想让你完成并运行你所拥有的代码。删除这些行:每个public void guess ();public void correct (guess) {。此外,请确保{}正确配对和嵌套。请记住,您不能在另一个方法中编写一个方法。这个例子是正确的:

  1. public static void main (String [] args) {
  2. // code for main
  3. } // end of main
  4. public static int foo (int arg) {
  5. // code for foo
  6. } // end of foo
  7. public static boolean bar (String bat) {
  8. // code for bar
  9. } // end of bar

但是,这是不正确的:

  1. public void static main (String [] args) {
  2. // some code for main
  3. public static int foo (int value) {
  4. // some code for foo
  5. } // end of foo
  6. } // end of main

另外,如果你有一个非void返回类型的方法,则该方法中的代码必须通过该方法中的任何执行路径返回指定类型的值。例如,如果你有一个int返回类型,代码必须有一个return语句和一个int表达式。表达式可以是常数。对于int返回类型方法,以下内容有效:

  • return 3;
  • return arg * arg + 7;
  • return foo;

当你得到一个干净的编译,运行你的代码。至少有一个问题,给予不正确的答案。
我想,做了这件事,你不需要更多的帮助
让我们继续一个设计技巧。此部分是可选的。你重复了代码。这可能是你的老师,在这一点上,希望你有重复的代码,因为下一课将是关于如何避免重复的代码。
如果你想让代码重复,请考虑使用以下几种方式的组合:

由于您是Java新手,我假设Collection对象将在以后的课程中介绍。
你的代码有几对,每对都有一个问题和一个答案。将问题与答案配对的一种方法是使用新的class

  1. public class QuizEntry {
  2. private String question;
  3. private String answer;
  4. public QuizEntry (String question, String answer) {
  5. this.question = question;
  6. this.answer = answer;
  7. }
  8. public String getQuestion () { return question; }
  9. public String getAnswer () { return answer; }
  10. }

这样,你就可以得到一个QuizEntry数组:

  1. QuizItem [] theQuiz = {
  2. new QuizItem ("What types of animal(s) can't vomit?","rodents")
  3. , new QuizItem ("What is the only mammal capable of flight?","bats")
  4. // etc.
  5. };

如果组成一对的项目是相同类型的,另一种方法是使用2D数组:

  1. String [][] theQuiz = {
  2. {"What types of animal(s) can't vomit?","rodents"}
  3. ,{"What is the only mammal capable of flight?", "bats"}
  4. // etc.
  5. };

现在,你可以有一个循环来运行你的测验。但是,在此之前,在获得和处理答案方面存在冲突。在你的代码中,以一个问题为例,你有这样的:

  1. System.out.println("What is the only mammal capable of flight?");
  2. String input = scan.nextLine(); //get input
  3. String answer = "Bats";

在上面,显示问题,检索用户的答案并等待处理。处理可能由public static boolean guess (String answer, Scanner input)方法处理。问题是这个方法也从用户那里得到答案。这意味着用户必须在评估答案之前输入两次答案。第一个答案被忽略了。第二个是加工。如果第二个用户响应不正确,则将处理第三个响应。
现在,回到循环:

  1. // 1D array with class QuizEntry version
  2. int correct = 0;
  3. for (int question = 0; question < theQuiz.length; question++) {
  4. System.out.println (theQuiz[question].getQuestion ());
  5. ******* guess (theQuiz[question].getAnswer (), scan);
  6. *******
  7. }
  1. // 2D array version
  2. int correct = 0;
  3. for (int question = 0; question < theQuiz.length; question++) {
  4. System.out.println (theQuiz[question][0]);
  5. ******* guess (theQuiz[question][1], scan);
  6. *******
  7. }

现在,更新代码并运行它。不要担心完成计算正确答案的部分。#21463;,并删除*******。它不会与他们编译。这样做反映了“编写一点代码,测试一点”的做法。如果您还没有找到并修复它,您可能会发现guess方法中仍然存在一个bug。
现在,在我有*******的地方,你应该做更多的修改。
返回类型为int的方法将返回int,并且可以在可以使用int表达式的地方使用。如果binbarbat都是一个方法,它接受一个int并返回一个int,那么你可以得到这样的语句:

  • int foo = bar (3) * bin (k) + bat (7);
  • int frob = bar (3) * m + (bat (bin (8));

在可以使用boolean表达式的地方,可以使用返回boolean的方法:

  1. public static boolean isEnglishLetter (char x) {
  2. return (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z');
  3. }

您可以在if中使用它:

  1. char choice = scan.next ().charAt(0);
  2. if (isEnglishLetter (choice)) { ... }

您可以将其分配给另一个boolean

  1. boolean goodAnswer = isEnglishLetter (choice);

因此,您可以使用uess方法返回的结果来更新correct的值或不更新它。

展开查看全部
7y4bm7vi

7y4bm7vi3#

  • "..这包括那些在第一次尝试时就猜对了的,以及那些在最初的错误响应后改变为正确的。..."*

使用 recordclass 来包含每个问题和答案。

  1. record QA(String q, String a) { }

还有一个 Map,用来表示回答是否正确。

  1. Map<QA, Boolean> map = new LinkedHashMap<>();
  2. map.put(new QA("What types of animal(s) can't vomit?", "Rodents"), false);
  3. map.put(new QA("What is the only mammal capable of flight?", "Bats"), false);
  4. map.put(new QA("What mammal has the most powerful bite", "Hippos"), false);
  5. map.put(new QA("Where is the only place that dogs have sweat glands?", "paws"), false);
  6. map.put(new QA("What is a male duck called?", "drake"), false);

然后遍历 map

  1. Scanner in = new Scanner(System.in);
  2. String s1 = "Try again!",
  3. s2 = "Sorry! Answer is incorrect. Time for the next question!",
  4. s3 = "Good job, you got the correct answer!",
  5. s4 = s3 + " Let's move onto the next question";
  6. QA qa;
  7. String s;
  8. for (Map.Entry<QA, Boolean> e : map.entrySet()) {
  9. qa = e.getKey();
  10. System.out.println(qa.q);
  11. s = in.nextLine();
  12. if (s.equalsIgnoreCase(qa.a)) {
  13. e.setValue(true);
  14. System.out.println(s3);
  15. } else {
  16. System.out.println(s1);
  17. s = in.nextLine();
  18. if (s.equalsIgnoreCase(qa.a)) {
  19. e.setValue(true);
  20. System.out.println(s4);
  21. } else System.out.println(s2);
  22. }
  23. }

“……在比赛结束时,我们必须计算他们做错了多少题。..."*
我用的是你的伪代码。
计算 map 条目的数量,并使用 *true**值 *。

  1. int correct = 0;
  2. for (boolean v : map.values()) if (v) correct++;

或者,一条小溪。

  1. int correct = (int) map.values().stream().filter(x -> x).count();

下面是一个示例输出。

  1. What types of animal(s) can't vomit?
  2. abc
  3. Try again!
  4. 123
  5. Sorry! Answer is incorrect. Time for the next question!
  6. What is the only mammal capable of flight?
  7. Bats
  8. Good job, you got the correct answer!
  9. What mammal has the most powerful bite
  10. Hippos
  11. Good job, you got the correct answer!
  12. Where is the only place that dogs have sweat glands?
  13. paws
  14. Good job, you got the correct answer!
  15. What is a male duck called?
  16. abc
  17. Try again!
  18. 123
  19. Sorry! Answer is incorrect. Time for the next question!
展开查看全部

相关问题