随机显示这些问题而不重复

时间:2018-04-06 18:39:10

标签: java

我想随机显示15个问题而不重复,前5个随机首先显示为简单,然后是5个为中等,然后是5个为硬。但是,我需要将它们打印出来而不重复它们,并确保在打印中等难度问题之前,首先打印所有简单的问题。我已经看到了有关collections.shuffle()的事情,但我想在不使用它的情况下实现它,因为我并不真正理解它。如果有人可以帮助谢谢。到目前为止,这是我的代码:

import java.util.Scanner;
import java.util.Random;

public class WWTBAM {

public static void main(String[] args) {

    System.out.println("Welcome to 'Who Wants to be a Millionaire'!");
    System.out.println("There will be a total of 15 questions.");
    System.out.println("You must answer all of them correctly in order to win the grand prize of $1 000 000!");
    System.out.println("This round's theme will be basketball, and the questions get harder and harder.");
    System.out.println("To help with your journey, you have one of each: 50:50, call a friend, ask the audience.");
    System.out.println("To use 50:50 enter 1, to use call a friend enter 2, to use ask the audience enter 3.");
    System.out.println("You also have the option to leave with your money after every question by entering 0.");
    System.out.println("For each question, answer either a, b, c or d.");
    System.out.println("Good luck!\n");

    String[] questions = new String[15];
    questions[0] = "How do you score points in basketball?\n a) Shoot a ball in a net\n b) Hit a birdie with a racquet\n c) Kick a ball across the field\n d) Knockout the opposing players\n";
    questions[1] = "How many players does a team have playing on an NBA court at one time?\n a) 6\n b) 10\n c) 5\n d) 3\n";
    questions[2] = "Which team won the 2016-2017 NBA championship?\n a) Cleveland Cavaliers\n b) Golden State Warriors\n c) Toronto Raptors\n d) Boston Celtics\n";
    questions[3] = "Which team does Lamarcus Aldridge currently play for?\n a) Houston Rockets\n b) Portland Trailblazers\n c) Memphis Grizzlies\n d) San Antonio Spurs\n";
    questions[4] = "How many total teams are there in the NBA currently?\n a) 10\n b) 25\n c) 30\n d) 20\n";

    questions[5] = "How many NBA championships has Michael Jordan won?\n a) 5\n b) 6\n c) 7\n d) 8\n";
    questions[6] = "Who holds the record for most points scored in one game?\n a) Kobe Bryant\n b) Wilt Chamberlain\n c) Michael Jordan\n d) Bill Russell\n";
    questions[7] = "What division are the Toronto Raptors in?\n a) Central Division\n b) Southeast Division\n c) Pacific Division\n d) Atlantic Division\n";
    questions[8] = "Which team has the most amount of championships in history?\n a) Los Angeles Lakers\n b) Chicago Bulls\n c) Boston Celtics\n d) Miami Heat\n";
    questions[9] = "Who is the youngest player to ever win the MVP award?\n a) LeBron James\n b) Derrick Rose\n c) Stephen Curry\n d) Kawhi Leonard\n";

    questions[10] = "Which team won the 1994-1995 NBA championship?\n a) Houston Rockets\n b) Chicago Bulls\n c) Orlando Magic\n d) Phoenix Suns\n";
    questions[11] = "What is the current NBA championship trophy called?\n a) Chris MacFord Trophy\n b) Walter A. Brown Trophy\n c) Larry O’Brien Trophy \n d) Jolly Mazgard Trophy\n";
    questions[12] = "Which player won the iconic 1988 3-pt contest?\n a) Drazen Petrovic\n b) Peja Stojakovic\n c) Larry Bird\n d) Pete Maravich\n";
    questions[13] = "Which player is nicknamed “The Glove” in the NBA?\n a) Gary Payton\n b) Reggie Miller\n c) Charles Barkley\n d) Shawn Kemp\n";
    questions[14] = "In which year was the NBA founded?\n a) 1943\n b) 1944\n c) 1945\n d) 1946\n";
    String[] answers = {"a", "c", "b", "d", "c", "b", "b", "d", "c", "b", "a", "c", "c", "a", "d"};

    int[] money = {100, 200, 500, 750, 1500, 3000, 5000, 10000, 15000, 20000, 50000, 100000, 200000, 500000, 1000000};
    int random;
    String response;
    String name;

    Scanner sc = new Scanner(System.in);
    Random rand = new Random();

    System.out.println("Enter your name: ");
    name = sc.nextLine();


    for (int i = 0; i < questions.length; i++) {

        if (i > 9) {
            random = rand.nextInt(5) + 10;
        } else if (i > 4) {
            random = rand.nextInt(5) + 5;
        } else {
            random = rand.nextInt(5) + 0;
        }
        System.out.println(questions[random]);
        response = sc.nextLine();


        if (!response.equalsIgnoreCase(answers[random])) {
            System.out.println("You lost! Too bad too sad.");
            break;
        }
    }

}

}

2 个答案:

答案 0 :(得分:0)

你可以创建三个HashMap,一个用于简单,中等和难度。用问题填充它们作为关键并作为价值回答。

编辑:

为简单,中等和艰难的问题HashMap执行此操作。

获取一个HashMap的密钥集。将它们添加到空列表,执行Collections.shuffle(),循环ArrayList,然后从原始HashMap获取密钥。以下是经过测试的示例代码:

import java.util.*;
public class MyClass {
    public static void main(String args[]) {
        HashMap<String, String> hm = new HashMap<>();    
        hm.put("1a", "z");
        hm.put("s4", "x");
        hm.put("4d", "c");
        hm.put("ef", "v");
        hm.put("g6", "b");
        hm.put("8h", "b");

        System.out.println("The same 'random' order of questions:");
        for(Map.Entry<String, String> entry: hm.entrySet()){
            System.out.println(entry.getKey() + " "+ entry.getValue());
        }

        new MyClass().shuffleQuestions(hm);
        new MyClass().shuffleQuestions(hm);
        new MyClass().shuffleQuestions(hm);
    }

    public void shuffleQuestions(HashMap<String, String> hm){
        System.out.println("\nShuffled Questions with answers:");
        Set<String> s = hm.keySet();
        ArrayList<String> arr = new ArrayList<>();
        arr.addAll(s);
        Collections.shuffle(arr);
        for(String s1: arr){
            System.out.println(s1 + " " + hm.get(s1));
        }
    }
}

答案 1 :(得分:0)

您可以使用Collections.shuffle()

  

不要忘记在洗牌之前用元素填充集合

示例:

    List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6));
    System.out.println("Init: " + Arrays.toString(numbers.toArray()));
    Collections.shuffle(numbers);
    System.out.println("Result: " + Arrays.toString(numbers.toArray()));

输出:

[1, 2, 3, 4, 5, 6]
[3, 5, 6, 4, 2, 1]