【问题标题】:How can I loop to the beginning of the code or to certain parts?如何循环到代码的开头或某些部分?
【发布时间】:2018-05-28 20:31:43
【问题描述】:

如果重试 = 否,或者如果重试 = 是,我将如何循环代码的开头,我将如何循环回到第 1 个问题?抱歉,如果这个问题很愚蠢,我是编码新手。这是我目前的代码。

import java.util.Scanner;


public class MainGame {

    static Question[] questions = new Question[15];

    public static void main(String[] args) {
        loadQuestions(); //I found an error in my code were when I printed my questions it printed "null", I fixed that by calling the loadQuestions function. 

        Scanner kb = new Scanner(System.in);
        String userChoice = "", userAnswer = "", retry = "yes";


        System.out.println("Welcome to The Not Very Possible Quiz! ");
        System.out.println("By: Danny Gill");

        do {
            System.out.println("Type In 'Play' to start playing, or type in 'Records' to see your high scores from previous games.");
            userChoice = (kb.nextLine().toLowerCase());
        } while (!(userChoice.equals("play") || userChoice.equals("records")));

        if (userChoice.equals("records")) {
            System.out.println("High Scores:");
        } else if (userChoice.equals("play")) {
            System.out.println("Get Ready!");
            System.out.println(questions[0]);
            userAnswer = (kb.nextLine().toLowerCase());
        if (userAnswer.equals("a")) {
            System.out.println(questions[1]);
        } else System.out.println("Wrong!");


        do {
            System.out.println("\n" + "Do you want to play again?"); 
            retry = (kb.nextLine()).toLowerCase();
        } while (!(retry.equals("yes") || retry.equals("no")));
    } while (retry.equals("yes"));


}












    public static void loadQuestions() {
        questions[0] = new Question("Which concert is the cheapest?", "50 Cent", "Drake", "Rihanna", "2-Pac");
        questions[1] = new Question("Who was the first president of the USA?", "Donald Trump", "John Adams", "George Washington", "George W. Bush");
        questions[2] = new Question("Which country has the largest military?", "United States", "India", "China", "North Korea");
        questions[3] = new Question("In Hockey how many players are on the ice for each team?", "6", "5", "7", "4");
        questions[4] = new Question("What is Canada's national sport?", "Lacrosse", "Hockey", "Basketball", "Soccer");
        questions[5] = new Question("Who was the first prime minister of Canada?", "Lester B. Pearson", "Stephen Harper", "John A. Macdonald", "Alexander Mackenzie");
        questions[6] = new Question("What is the largest city in Canada?", "Calgary", "Toronto", "Montreal", "Vancouver");
        questions[7] = new Question("How many continents are there?", "5", "6", "7", "8");
        questions[8] = new Question("What is Pakistan's currency?", "Euro", "Dollar", "Rupee", "Yen");
        questions[9] = new Question("Who is currently the richest person on Earth?", "Bill Gates", "Elon Musk", "Jeff Bezos", "Mark Zuckerberg");
        questions[10] = new Question("Which planet is nearest to the sun?", "Earth", "Mercury", "Venus", "Saturn");
        questions[11] = new Question("Who invented Ferrari?", "Sergio Marchionne", "Endo Ferrari", "Enzo Ferrari", "Bill Ferrari");
        questions[12] = new Question("Who painted the Mona Lisa?", "Pablo Picasso", "Leonardo Who Cares", "Leonardo DiCaprio", "Leonardo Da Vinci");
        questions[13] = new Question("In what year was Google launched on the web?", "1998", "2000", "1997", "1990");
        questions[14] = new Question("How often are the olympics held?", "3 Years", "4 Years", "5 Years", "2 Years");

    }


}

【问题讨论】:

    标签: java arrays loops


    【解决方案1】:

    在您的“播放”部分中,我将使用 for 循环。如果用户的回答是假的,你问他是否想重复。 如果他说“是”,则再次将i 设置为 0。

    for(int i=0; i<=questions.length; i++) {
     // Ask questions[i]
     // IF answer is okay THEN continue;
     // ELSE ask IF user wants to restart
     // IF yes THEN i=0
     // IF ``no THEN break
    }
    

    提示:实现一种方法来获得正确答案并在 Question 类中比较答案。

    【讨论】:

      【解决方案2】:

      您也可以使用break来跳转代码的某些部分

      例子:

      **search:**
          for (i = 0; i < arrayOfInts.length; i++) {
              for (j = 0; j < arrayOfInts[i].length;
                   j++) {
                  if (arrayOfInts[i][j] == searchfor) {
                      foundIt = true;
                      **break search;**
                  }
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2021-12-18
        • 1970-01-01
        • 1970-01-01
        • 2021-04-23
        • 2020-05-12
        • 2016-02-01
        • 1970-01-01
        • 2016-05-08
        • 1970-01-01
        相关资源
        最近更新 更多