我目前正在创建一个测验。在回答每个问题后,我希望用户有 15 秒的休息时间,直到显示下一个问题。我还希望用户可以选择在时间用完之前选择继续下一个问题。如果可能的话,也许可以在窗口中显示从 15 开始的倒计时。这是代码的副本(从程序的开头开始,在第一个问题的代码之后结束。我不会费心添加其他 19 个问题,因为它们的代码几乎相同)。有人可以告诉我他们将如何做到这一点,因为我不知道如何使用 Java 计时器。我所有的研究和试图弄清楚该怎么做并没有真正帮助。
package quiz;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.Scanner;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Icon;
import java.util.Timer;
import java.applet.*;
import java.net.*;
public class Main {
BufferedImage img = null;
BufferedImage img2 = null;
Image dbImage;
Graphics dbg;
private ImageIcon image;
private JLabel label;
public static void main(String[] args) {
int score = 0;
int seconds = 0;
int loop1 = 0;
int loop2 = 0;
int loop3 = 0;
int loop4 = 0;
int loop5 = 0;
int loop6 = 0;
int loop7 = 0;
int loop8 = 0;
int loop9 = 0;
int loop10 = 0;
int loop11 = 0;
int loop13 = 0;
int loop14 = 0;
int loop15 = 0;
int loop16 = 0;
int loop17 = 0;
int loop18 = 0;
int loop19 = 0;
int loop20 = 0;
int loop21 = 0;
int loop22 = 0;
long begin = 0;
long end = 0;
long SECbegin = 0;
long SECend = 0;
String name = JOptionPane.showInputDialog(null, "What is your name? ");
while (loop1 < 100) {
JOptionPane.showMessageDialog(null, "Hello " + name + ". Welcome to the Game Show. In this quiz, you will be given $1 to start with.");
JOptionPane.showMessageDialog(null, "There will be 20 multiple choice questions. \n" +
"You will have 30 seconds to answer each question. Answer each question with the letter (A, B, C or D) corresponding with that answer.");
JOptionPane.showMessageDialog(null, "If you fail to answer in that time, or if you give an incorrect answer, you will be sent home with $1. \nIf you give a correct answer, your money will double.");
JOptionPane.showMessageDialog(null, "If you do not know the answer to a question, you may choose to leave the game show with half of your current prize money. You can do this by typing 'Leave' instead of an answer.");
JOptionPane.showMessageDialog(null, "After each question, you will have 15 seconds until the next question is automatically displayed. \nYou may choose to proceed to the next question before this time runs out.");
int start = JOptionPane.showConfirmDialog(null, "Would you like to begin? Yes or no?");
if (start == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "Ok. Get ready, and when you feel you are ready to begin, click 'Ok'.");
loop1++;
break;
}
if (start == JOptionPane.YES_OPTION) {
break;
} else {
System.exit(0);
break;
}
}
while (loop2 < 1) {
// start timer
begin = System.currentTimeMillis();
String Q1 = JOptionPane.showInputDialog(null, "Question 1: What was the original name of the Java programming language? \n A) Coffee B) JCode C) Oak D) Green");
// play Countdown.wav here!
if (Q1.equalsIgnoreCase("C")) {
// end timer
end = System.currentTimeMillis();
JOptionPane.showMessageDialog(null, "Correct!");
if (end - begin < 30000) {
score = 2;
} else {
JOptionPane.showMessageDialog(null, "You took too long to answer the question! YOU ARE OUT OF THE GAME WITH $1!");
System.exit(0);
}
String L1 = JOptionPane.showInputDialog(null, "You now have $" + score + "! You will automatically proceed to the next question in 15 seconds. \n If you want to proceed to the next question early, type: 'Proceed'.");
{
if (L1.equalsIgnoreCase("proceed")) {
// This is where the user chooses to proceed to the next question!
JOptionPane.showMessageDialog(null, "You may now proceed to the next question. Good luck!");
} else {
// This is where I want the program to wait 15 seconds before automatically displaying the next question!
JOptionPane.showMessageDialog(null, "You may now proceed to the next question. Good luck!");
}
break;
}
}
if (Q1.equalsIgnoreCase("leave")) {
score /= 2;
JOptionPane.showMessageDialog(null, "You have decided to leave the game show with $" + score + "! Congratulations!");
} else {
JOptionPane.showMessageDialog(null, "That is not correct! \n YOU ARE OUT OF THE GAME WITH $1!");
System.exit(0);
}
loop2++;
}
...这是我尝试过的东西,但是 thread.sleep(1000); 行有一个错误:“无法访问的代码”。如果我在没有该行的情况下运行程序,则 15 秒后没有任何反应:
...
SECbegin = System.currentTimeMillis();
String L1 = JOptionPane.showInputDialog(null, "You now have $" + score + "! You will automatically proceed to the next question in 15 seconds. \n If you want to proceed to the next question early, type: 'Proceed'.");
{
while (System.currentTimeMillis() - SECbegin < 15000) {
if (L1.equalsIgnoreCase("proceed")) {
JOptionPane.showMessageDialog(null, "You may now proceed to the next question. Good luck!");
break; // if there is an answer we stop waiting
Thread.sleep(1000); // error: unreachable code
} else {
JOptionPane.showMessageDialog(null, "You may now proceed to the next question. Good luck!");
}
break;
}
}
if (Q1.equalsIgnoreCase("leave")) {
score /= 2;
JOptionPane.showMessageDialog(null, "You have decided to leave the game show with $" + score + "! Congratulations!");
} else {
JOptionPane.showMessageDialog(null, "That is not correct! \n YOU ARE OUT OF THE GAME WITH $1!");
System.exit(0);
}
loop2++;
}
我试图解决这个问题,但它不起作用:( 15 秒后没有任何反应:
package quiz;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.Scanner;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Icon;
import java.util.Timer;
import java.applet.*;
import java.net.*;
public class Main {
BufferedImage img = null;
BufferedImage img2 = null;
Image dbImage;
Graphics dbg;
private ImageIcon image;
private JLabel label;
public static void main(String[] args) {
int score = 0;
int seconds = 0;
int loop1 = 0;
int loop2 = 0;
int loop3 = 0;
int loop4 = 0;
int loop5 = 0;
int loop6 = 0;
int loop7 = 0;
int loop8 = 0;
int loop9 = 0;
int loop10 = 0;
int loop11 = 0;
int loop13 = 0;
int loop14 = 0;
int loop15 = 0;
int loop16 = 0;
int loop17 = 0;
int loop18 = 0;
int loop19 = 0;
int loop20 = 0;
int loop21 = 0;
int loop22 = 0;
long begin = 0;
long end = 0;
long SECbegin = 0;
long SECend = 0;
String name = JOptionPane.showInputDialog(null, "What is your name? ");
while (loop1 < 100){
JOptionPane.showMessageDialog(null, "Hello " + name + ". Welcome to the Game Show. In this quiz, you will be given $1 to start with.");
JOptionPane.showMessageDialog(null, "There will be 20 multiple choice questions. \n" +
"You will have 30 seconds to answer each question. Answer each question with the letter (A, B, C or D) corresponding with that answer.");
JOptionPane.showMessageDialog(null, "If you fail to answer in that time, or if you give an incorrect answer, you will be sent home with $1. \nIf you give a correct answer, your money will double.");
JOptionPane.showMessageDialog(null, "If you do not know the answer to a question, you may choose to leave the game show with half of your current prize money. You can do this by typing 'Leave' instead of an answer.");
JOptionPane.showMessageDialog(null, "After each question, you will have 15 seconds until the next question is automatically displayed. \nYou may choose to proceed to the next question before this time runs out.");
int start = JOptionPane.showConfirmDialog(null, "Would you like to begin? Yes or no?");
if(start == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "Ok. Get ready, and when you feel you are ready to begin, click 'Ok'.");
loop1++;
break;
}
if(start == JOptionPane.YES_OPTION) {
break;
}
else{
System.exit(0);
break;
}
}
while (loop2 < 1){
// start timer
begin = System.currentTimeMillis();
String Q1 = JOptionPane.showInputDialog(null, "Question 1: What was the original name of the Java programming language? \n A) Coffee B) JCode C) Oak D) Green");
// play Countdown.wav here!
if(Q1.equalsIgnoreCase("C")) {
// end timer
end = System.currentTimeMillis();
JOptionPane.showMessageDialog(null, "Correct!");
if( end - begin < 30000 ){
score = 2;
}
else {
JOptionPane.showMessageDialog(null, "You took too long to answer the question! YOU ARE OUT OF THE GAME WITH $1!");
System.exit(0);
}
SECbegin = System.currentTimeMillis();
String L1 = JOptionPane.showInputDialog(null, "You now have $" + score + "! You will automatically proceed to the next question in 15 seconds. \n If you want to proceed to the next question early, type: 'Proceed'.");
if(L1.equalsIgnoreCase("proceed")) {
JOptionPane.showMessageDialog(null, "You may now proceed to the next question. Good luck!");
}
if(System.currentTimeMillis() - SECbegin < 15000){
}
break;
}
else{
JOptionPane.showMessageDialog(null, "You may now proceed to the next question. Good luck!");
}
if(Q1.equalsIgnoreCase("leave")) {
score /= 2;
JOptionPane.showMessageDialog(null, "You have decided to leave the game show with $" + score + "! Congratulations!");
}
else{
JOptionPane.showMessageDialog(null, "That is not correct! \n YOU ARE OUT OF THE GAME WITH $1!");
System.exit(0);
}
loop2++;
}
JOptionPane.showMessageDialog(null, "Congratulations " + name + "! You have won the game!");
System.exit(0);
}
}
请帮我!
现在整个程序都是错误的和蹩脚的:
package quiz;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.Scanner;
import javax.swing.AbstractButton;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Icon;
import java.util.Timer;
import java.applet.*;
import java.net.*;
public class Main {
BufferedImage img = null;
BufferedImage img2 = null;
Image dbImage;
Graphics dbg;
private ImageIcon image;
private JLabel label;
public static void main(String[] args) {
int score = 0;
int seconds = 0;
int loop1 = 0;
int loop2 = 0;
int loop3 = 0;
int loop4 = 0;
int loop5 = 0;
int loop6 = 0;
int loop7 = 0;
int loop8 = 0;
int loop9 = 0;
int loop10 = 0;
int loop11 = 0;
int loop13 = 0;
int loop14 = 0;
int loop15 = 0;
int loop16 = 0;
int loop17 = 0;
int loop18 = 0;
int loop19 = 0;
int loop20 = 0;
int loop21 = 0;
int loop22 = 0;
int loopw = 0;
long begin = 0;
long end = 0;
long SECbegin = 0;
long SECend = 0;
String name = JOptionPane.showInputDialog(null, "What is your name? ");
while (loop1 < 100){
JOptionPane.showMessageDialog(null, "Hello " + name + ". Welcome to the Game Show. In this quiz, you will be given $1 to start with.");
JOptionPane.showMessageDialog(null, "There will be 20 multiple choice questions. \n" +
"You will have 30 seconds to answer each question. Answer each question with the letter (A, B, C or D) corresponding with that answer.");
JOptionPane.showMessageDialog(null, "If you fail to answer in that time, or if you give an incorrect answer, you will be sent home with $1. \nIf you give a correct answer, your money will double.");
JOptionPane.showMessageDialog(null, "If you do not know the answer to a question, you may choose to leave the game show with half of your current prize money. You can do this by typing 'Leave' instead of an answer.");
JOptionPane.showMessageDialog(null, "After each question, you will have 15 seconds until the next question is automatically displayed. \nYou may choose to proceed to the next question before this time runs out.");
int start = JOptionPane.showConfirmDialog(null, "Would you like to begin? Yes or no?");
if(start == JOptionPane.NO_OPTION) {
JOptionPane.showMessageDialog(null, "Ok. Get ready, and when you feel you are ready to begin, click 'Ok'.");
loop1++;
break;
}
if(start == JOptionPane.YES_OPTION) {
break;
}
else{
System.exit(0);
break;
}
}
while (loop2 < 1){
// start timer
begin = System.currentTimeMillis();
String Q1 = JOptionPane.showInputDialog(null, "Question 1: What was the original name of the Java programming language? \n A) Coffee B) JCode C) Oak D) Green");
// play Countdown.wav here!
if(Q1.equalsIgnoreCase("C")) {
// end timer
end = System.currentTimeMillis();
JOptionPane.showMessageDialog(null, "Correct!");
if( end - begin < 30000 ){
score = 2;
}
else {
JOptionPane.showMessageDialog(null, "You took too long to answer the question! YOU ARE OUT OF THE GAME WITH $1!");
System.exit(0);
}
// start
SECbegin = System.currentTimeMillis();
String L1 = JOptionPane.showInputDialog(null, "You now have $" + score + "! You will automatically proceed to the next question in 15 seconds. \n If you want to proceed to the next question early, type: 'Proceed'.");
while(loopw < 10000){
if(L1.equalsIgnoreCase("proceed")) {
JOptionPane.showMessageDialog(null, "You may now proceed to the next question. Good luck!");
break;
}
if(System.currentTimeMillis() - SECbegin > 15000){
JOptionPane.showMessageDialog(null, "You may now proceed to the next question. Good luck!");
}
else{
}
loopw++;
}
// end
if(Q1.equalsIgnoreCase("leave")) {
score /= 2;
JOptionPane.showMessageDialog(null, "You have decided to leave the game show with $" + score + "! Congratulations!");
}
else{
JOptionPane.showMessageDialog(null, "That is not correct! \n YOU ARE OUT OF THE GAME WITH $1!");
System.exit(0);
}
loop2++;
}
JOptionPane.showMessageDialog(null, "Congratulations " + name + "! You have won the game!");
System.exit(0);
}
}}