我正在尝试用 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/