【问题标题】:Can you add multiple questions in a while loop in java?你可以在java的while循环中添加多个问题吗?
【发布时间】:2020-10-31 08:18:00
【问题描述】:

我目前是一名高中生,我正在做一个简短的问答游戏作为一个项目。我想知道您是否可以添加多个问题。这是代码

import java.util.Scanner;
import java.util.Random;
public class Quiz 
{

    public static void main(String[] args) 
    {
        Scanner s = new Scanner(System.in);
        Random r = new Random();
        int lives = 3;
        String answers;     
        
        while (lives > 0)
        {
            
            System.out.println("Question Goes Here: ");
            answers = s.nextLine();
            
                if (answers.equalsIgnoreCase("Answer")) 
                {
                    System.out.println("Correct! Please press ENTER to continue");
                    s.nextLine();
                }
                
                
            
                
                //TO-DO IF THE ANSWER IS WRONG
                else
                    {
                        --lives;
                        System.out.println("You have " + lives + " lives left");
                    }
            
        }
        
        
            //TO-DO IF "LIVES" IS EQUAL TO 0
            if (lives == 0) 
            {
                System.out.println("Game Over");
            }
        
    }

【问题讨论】:

  • 您可以在数组中添加任意数量的问题,然后从那里获取它们

标签: java


【解决方案1】:

假设你已经准备了一个二维数组的问题和答案:

final Scanner s = new Scanner(System.in);

String[][] arrQA = {
    {"question1", "answer1"},
    {"question2", "answer2"},
    {"question3", "answer3"},
    {"question4", "answer4"},
};
int lives = 3;
int success = 0;

for (int i = 0; i < arrQA.length && lives > 0; i++) {
    String[] qa = arrQA[i];

    System.out.print(qa[0] + " goes here, type your answer: ");
    String answer = s.nextLine();
        
    if (answer.equalsIgnoreCase(qa[1])) {
        success++;
        System.out.println("Correct! Please press ENTER to continue");
        s.nextLine();
    } else {
        --lives;
        System.out.println("Incorrect! You have " + lives + " lives left");
    }
}
System.out.println("Game Over! You have answered " + success + " questions correctly");

【讨论】:

  • 另一个问题“[]”在 String[] 后面的意思是什么String[] [] arrQA = { {"First Question: ", "answer1"}, {"Second Question: ", "answer2"}, {"Third Question: ", "answer3"}, {"Fourth Question: ", "answer4"}, };
  • String[][] 表示"array of String arrays",是Java中创建多维数组的一种方式。
【解决方案2】:

我建议为问题创建一个 POJO 类。像这样的:

    class Question{
        String question;
        String answer;
    
        public Question(String question, String answer) {
            this.question = question;
            this.answer = answer;
        }
    
        public String getQuestion() {
            return question;
        }
    
        public String getAnswer() {
            return answer;
        }
    }

然后用问题列表创建你的主函数,用一些变量迭代你的问题。

public static void main(final String[] args) {
    //Variables
        final Scanner s = new Scanner(System.in);
        final Random r = new Random();
        int lives = 3;
        String answers;
        ArrayList<Question> quizBrain = new ArrayList<Question>();
    
        quizBrain.add(new Question("question1", "answer1"));
        quizBrain.add(new Question("question2", "answer2"));
        quizBrain.add(new Question("question3", "answer3"));
        quizBrain.add(new Question("question4", "answer4"));
        quizBrain.add(new Question("question5", "answer5"));

        int questionCounter = 0;
        //Conditions
        while (lives > 0)
        {
            System.out.println(quizBrain.get(questionCounter).getQuestion());
            answers = s.nextLine();
                if (answers.equalsIgnoreCase(quizBrain.get(questionCounter).getAnswer())) 
                {
                    System.out.println("Correct! Please press ENTER to continue");
                    questionCounter++;
                    s.nextLine();
                }
                //TO-DO IF THE ANSWER IS WRONG
                else
                    {
                        --lives;
                        System.out.println("You have " + lives + " lives left");
                    }   
        }
            //TO-DO IF "LIVES" IS EQUAL TO 0
            if (lives == 0) 
                System.out.println("Game Over");

        }

您可以看到 POJO 类的优点。如果您需要将问题存储在数据库中,这将是一个很大的帮助。

【讨论】:

    【解决方案3】:

    我建议您创建一个包含所有问题的数组,然后使用 math.random 访问它。您还必须按照与问题相同的顺序为答案创建一个数组,以便 math.random 可以得到所选问题的正确答案。

    【讨论】:

    • 它是如何工作的?你能提供例子吗?谢谢!
    猜你喜欢
    • 2023-04-03
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 2021-02-08
    相关资源
    最近更新 更多