[英]Using a Java Timer in a Quiz
I am currently creating a quiz. 我目前正在创建测验。 After each question is answered, I want the user to have a 15 second break until the next question is displayed.
在回答每个问题之后,我希望用户休息15秒,直到显示下一个问题。 I also want the user to have the option to choose to proceed to the next question before the time runs out.
我还希望用户可以选择在时间用完之前继续进行下一个问题。 If possible, maybe a countdown from 15 could be displayed in the window.
如果可能的话,可能会在窗口中显示倒数15。 Here is a copy of the code (which starts at the beginning of the program, and ends after the code for the first question. I won't bother adding the other 19 questions, as the code for them is almost identical).
这是代码的副本(从程序的开头开始,在第一个问题的代码之后结束。我不会费心添加其他19个问题,因为它们的代码几乎相同)。 Can someone please show me how they would do this, as I have no knowledge of how to use Java timers.
有人可以告诉我他们将如何执行此操作,因为我不知道如何使用Java计时器。 All of my research and trying to figure out what to do hasn't really helped.
我所有的研究并试图弄清楚该怎么做并没有真正帮助。
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++;
}
... Here is something I tried, but the thread.sleep(1000); ...这是我尝试过的东西,但是thread.sleep(1000); line has an error: 'unreachable code'.
行有一个错误:“无法访问的代码”。 If I run the program without that line, nothing happens after 15 seconds:
如果我在没有该行的情况下运行该程序,则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++;
}
I tried to fix the problem, but it doesn't work :( Nothing happens after 15 seconds: 我试图解决此问题,但不起作用:( 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);
}
}
PLEASE HELP ME! 请帮我!
Now the WHOLE PROGRAM is just buggy and crappy: 现在,整个计划简直是臭虫and脚:
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);
}
}}
EDIT 编辑
put it in a method so you can use it every question without copying and pasting. 将其放在方法中,这样您就可以在不复制和粘贴的情况下使用每个问题。
I would have to go with Thread.sleep(ms);
我将不得不使用
Thread.sleep(ms);
. 。 I think that this will solve your problem, and I have prepared some untested code to demonstrate.
我认为这将解决您的问题,并且我准备了一些未经测试的代码来演示。
Before I show you that, I want to give you some background on what the sleep method actually does. 在向您展示这一点之前,我想为您提供有关睡眠方法实际作用的背景知识。 OK, the sleep method pauses the current Thread for a set millisecond amount, in this case, 15 seconds, or 15000 milliseconds (1 second = 1000 milliseconds).
好的,sleep方法将当前线程暂停设置的毫秒数,在这种情况下为15秒或15000毫秒(1秒= 1000毫秒)。 Now, for the code, you might want to do this:
现在,对于代码,您可能想要执行以下操作:
public static boolean interrupted;
public void wait()
{
interrupted = false;
//this is just the boolean value allowing the countdown to be interrupted as you requested.
Integer currentTime = 15;
//set the integer value of the countdown time.
while(currentTime > 0 || interrupted){
//must throw InterruptedException here.
Thread.sleep(1000);
currentTime--;
//'timerText' is just the graphics object displaying the current countdown state.
timerText.setText(currentTime.toString);
}
currentTime = 15;
interrupted = false;
//resetting the values
}
For the interrupted value, just set that when the input method for cancelling the countdown is activated. 对于中断值,只需设置为取消倒计时的输入法被激活即可。
Please remember that this code may not work, and is just a reference peice. 请记住,此代码可能不起作用,仅作为参考。
Hope this helps. 希望这可以帮助。
import javax.swing.*;
import java.io.*;
public class quizbee
{
static LineNumberReader cin = new LineNumberReader(new InputStreamReader(System.in));
public static void main(String[] args)
{
int score = 0;
final int NumberofQuestions = 10;
System.out.println("Quiz Bee\n\n");
String[][] QandA = {
{"Instance of a class. A. Syntax B. Compiler C. Object Your answer:","c"},
{"A group or collection of objects with common properties. A. Class B. Attributes C. Variables Your answer:","a"},
{"It occur when you use a correct word in the wrong context in program code. A. Syntax Error B. Semantic Error C. Program Error Your answer:","b"},
{"A characteristic that define an object as part of a class. A. Attributes B. Boolean C. Compiler Your answer:","a"},
{"A style in of programming in which sets of operations are executed one after in secuence A. Programming B. Procedure C. Procedural Programming Your answer:","c"},
{"Named computer memory locations that hold vakues that may vary. A. Class B. Variables C. Compiler Your answer:","b"},
{"A statement or programs means to carry it out. A. executing B. Variables C. Compiler Your answer:","a"},
{"An error that occurs when you introduce typing errors into your program. A. Syntax Error B. Semantic Error C. Program Error Your answer:","a"},
{"It refers to the hiding of data and method within an object. A. Inheritance B. Polymorphism C. Encapsulation Your answer:","c"},
{"It refers to the rule of language. A. Syntax B. Software C. Program Your answer:","a"} };
String[] Answers = new String[NumberofQuestions];
for(int x = 0; x < NumberofQuestions; x++)
{
System.out.print((x+1) + ". " + QandA[x][0] + " ");
try { Answers[x] = cin.readLine(); }
catch (IOException e) { System.err.println("Error."); }
Answers[x].toLowerCase();
if(QandA[x][1].equals(Answers[x]))
{
score++;
}
System.out.print("\n");
}
System.out.println("\n\t\tYou got " + score + " of "
+ NumberofQuestions + " right!\n\n\n");
System.exit(0);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.