java - 如何用java制作一个在每个问题之前播放声音的测验?

标签 java

我正在尝试用 Java 进行音乐和弦测验,尽管中途有东西干扰,但我需要在提出每个问题之前进行编程以播放声音文件。

我尝试使用数组列表和 fileInputStream 播放器 这是我尝试过的一些代码。

public static void main(String[] args) {

    try {
    FileInputStream fileInputStream = new FileInputStream("eminor.mp3");
    Player player = new Player(fileInputStream);
    player.play();
    }catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch(JavaLayerException e) {
        e.printStackTrace();
    }
    {


    String question1 = "What chord is this?\n"
            + "(a)E minor\n(b)E major\n(c)C minor\n";
    String question2 = "What chord is this?\n"
            + "(a)E Major\n(b)C major\n(c)G major\n";
    String question3 = "What chord is this?\n"
            + "(a)E Major\n(b)C major\n(c)G major\n";
            Question [] questions = {
            new Question(question1,"a")

            {;
    }
    };
    Trial(questions);
    }
            }
    public static void Trial(Question[] questions) {
        int points = 0;
        Scanner keyboardInput = new Scanner(System.in);
        for(int i = 0; i< questions.length; i++) {
            System.out.println(questions[i].prompt);    
            String answer = keyboardInput.nextLine();
            if(answer.equals(questions[i].answer)) {
                points++;}
                else 
                    System.out.println("Wrong Answer");
                System.out.println(" Your score is " + ""+ points + "/" + questions.length);

    System.out.println("Next Question");



        try {
            FileInputStream fileInputStream = new FileInputStream("gmajor.mp3");
            Player player = new Player(fileInputStream);
            player.play();
            }catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch(JavaLayerException e) {
                e.printStackTrace();
            }
        }
    }



            public static void Trial2(Question[] questions) {
                int points = 0;
                Scanner keyboardInput = new Scanner(System.in);
                for(int i = 0; i< questions.length; i++) {
                    System.out.println(questions[i].prompt);    
                    String answer = keyboardInput.nextLine();
                    if(answer.equals(questions[i].answer)) {
                        points++;}
                                else 
                                    System.out.println("Wrong answer");
                    System.out.println("Your score is " + ""+ points + "/" + questions.length);


        }

最佳答案

您应该关闭您的 FileInputStream.close()如果您使用的是 java 8 或更高版本,则更好,使用 The try-with-resources Statement

关于java - 如何用java制作一个在每个问题之前播放声音的测验?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57563465/

相关文章:

Java EJB @Asynchronous 循环 - 如何在继续之前知道所有调用何时完成?

java - 如何在maven中添加jar路径?

java - 在 Eclipse 中使用 Java.applet.Applet

java - 在安卓中创建视频

java - JMF的转播能力?

java - GET 始终返回 0 id 值。 Spring数据JPA

java - 带有一组选项的代码,用于打印联系人列表、联系人数量、联系人平均年龄以及每种类型联系人的数量

java - 如何从 String2 中删除 String1 并用剩余的字符串构建新的 String2?

java - Spring 中 Autowiring 特定的内部 bean

java - Guava Lists.transform - 如果一个输入错误怎么办?