Как мне сделать мою игру "BruhSoundeffect.wav", когда вызывается другой устав. Если человек получает неправильный ответ на вопрос, это играет эффект - PullRequest
1 голос
/ 26 февраля 2020
public class ThreeDoors {

    private String message;
    private String answer1;
    private String answer2;
    private String answer3;


    public ThreeDoors()
    {

        answer1 = "ECHO"; //I speak without a mouth and hear without ears. I have no body, but I come alive with wind. What am I?
        answer2 = "CANDLE";//You measure my life in hours and I serve you by expiring. I’m quick when I’m thin and slow when I’m fat. The wind is my enemy.
        answer3 = "MAP";//I have cities, but no houses. I have mountains, but no trees. I have water, but no fish. What am I?


    }

    public int getRandom()
    {
        Random randy = new Random();
        return randy.nextInt(11 - 1) -10;


    }

    public String getQ1()
    {
        return "I speak without a mouth and hear without ears. I have no body, but I come alive with wind. What am I?";
    }
    public String getQ2()
    {

        return "You measure my life in hours and I serve you by expiring. I’m quick when I’m thin and slow when I’m fat. The wind is my enemy.";
    }
    public String getQ3()
    {

        return "I have cities, but no houses. I have mountains, but no trees. I have water, but no fish. What am I?";
    }

    public String getA1()
    {

        return answer1;

    }
    public String getA2()
    {

        return answer2;

    }
    public String getA3()
    {

        return answer3;

    }




}

Это класс проекта выше, а мой основной метод ниже. Я начинающий программист, пожалуйста, будьте спокойны со мной:).

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.*;
public class ThreeDoorsDB {

    //https://docs.oracle.com/javase/tutorial/sound/playing.html
    //I saw that if advanced sequencer methods are not needed then you can just use a clip.
    //Using the open method of the clip, which contains a audio input stream.
    //Now I can take my clip object and open the AudioInputStream.
    //I use a try statement to make sure the program does not crash if an exeption is thrown.
    //An invisible JFRAMe is used just to play music because it is easier to do it in jframe than JOP.
    //The imports came in with the errors, thanks intellij.
    //Exception in thread "main" java.io.FileNotFoundException: big.mp3 (The system cannot find the file specified)

    public static void main(String [] args) throws IOException, LineUnavailableException {

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(false);

        try {
            AudioInputStream audio = AudioSystem.getAudioInputStream(new File("big.wav"));
            Clip clip = AudioSystem.getClip();
            clip.open(audio);
            clip.start();
        } catch (UnsupportedAudioFileException e) {
            e.printStackTrace();
        }


        ThreeDoors demo = new ThreeDoors();
        int rando1 = demo.getRandom();

        ImageIcon recap = new ImageIcon("bruh.png");
        String input = (String) JOptionPane.showInputDialog(null, "Enter number generated = " + rando1, "Trivia", JOptionPane.INFORMATION_MESSAGE, recap, null,"");

        int rando = Integer.parseInt(input);

        if (rando == rando1)
        {
            ImageIcon correct = new ImageIcon("cor.png");
            JOptionPane.showMessageDialog(null, "Acess Accepted", "Trivia", JOptionPane.INFORMATION_MESSAGE, correct);

        }
        else
        {
            ImageIcon stop = new ImageIcon("Israeli_Stop_Sign.png");
            JOptionPane.showMessageDialog(null, "Acess Denied", "Trivia", JOptionPane.INFORMATION_MESSAGE,stop);
            System.exit(0);

            JFrame frame1 = new JFrame();
            frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame1.setVisible(false);

            try {
                AudioInputStream audio = AudioSystem.getAudioInputStream(new File("bruh.wav"));
                Clip clip = AudioSystem.getClip();
                clip.open(audio);
                clip.start();
            } catch (UnsupportedAudioFileException e) {
                e.printStackTrace();
            }


        }

        ImageIcon ear = new ImageIcon("bigbrain.png");
        input = (String) JOptionPane.showInputDialog(null, demo.getQ1(), "Question 1", JOptionPane.INFORMATION_MESSAGE, ear, null, "");

        if (input.equalsIgnoreCase(demo.getA1()))
        {
            ImageIcon ok = new ImageIcon("loam.png");
            JOptionPane.showMessageDialog(null, "", "Correct!", JOptionPane.INFORMATION_MESSAGE,ok);

        }
        else
        {

            ImageIcon no = new ImageIcon("no.gif");
            JOptionPane.showMessageDialog(null, "", "Incorrect!", JOptionPane.INFORMATION_MESSAGE, no);

            System.exit(0);
        }

        ImageIcon light = new ImageIcon("unnamed.png");
        input = (String) JOptionPane.showInputDialog(null, demo.getQ2(), "Question 2", JOptionPane.INFORMATION_MESSAGE, light,null,"");

        if (input.equalsIgnoreCase(demo.getA2()))
        {

            ImageIcon yes = new ImageIcon("prime.gif");
            JOptionPane.showMessageDialog(null, "", "Correct!", JOptionPane.INFORMATION_MESSAGE, yes);

        }
        else
        {

            ImageIcon nope = new ImageIcon("giphy.gif");
            JOptionPane.showMessageDialog(null, "", "Incorrect!", JOptionPane.INFORMATION_MESSAGE, nope);

            JFrame frame1 = new JFrame();
            frame.setVisible(false);

            try {
                AudioInputStream audio = AudioSystem.getAudioInputStream(new File("bruhsound.wav"));
                Clip clip = AudioSystem.getClip();
                clip.open(audio);
                clip.start();
            } catch (UnsupportedAudioFileException e) {
                e.printStackTrace();
            }
            System.exit(0);


        }

        ImageIcon mount = new ImageIcon("mount.gif");
        input = (String) JOptionPane.showInputDialog(null, demo.getQ3(), "Question 3", JOptionPane.INFORMATION_MESSAGE, mount, null, "");

        if (input.equalsIgnoreCase(demo.getA3()))
        {

            ImageIcon yup = new ImageIcon("yt.gif");
            JOptionPane.showMessageDialog(null, "", "Correct!", JOptionPane.INFORMATION_MESSAGE, yup);

        }
        else
        {
            //Mark Cuban > Everyone
            ImageIcon nah = new ImageIcon("Mark.gif");
            JOptionPane.showMessageDialog(null, "", "Incorrect!", JOptionPane.INFORMATION_MESSAGE, nah);
            System.exit(0);

        }

        ImageIcon gold = new ImageIcon("god.png");
        JOptionPane.showMessageDialog(null, "", "CLAIM THE GOLD!", JOptionPane.INFORMATION_MESSAGE, gold);



    }

}

Как вы можете видеть, когда человек неправильно задает вопрос, ему следует предлагать звуковой эффект, но он не работает. Исходный фоновый звук в начале основного класса работает, но то же самое не в операторе else. Спасибо за помощь!

...