Java 使用线程的sleep()方法实现测验
导入java.util.Scanner 导入thread.prompt 公共类Answer3扩展了线程{Java 使用线程的sleep()方法实现测验,java,multithreading,Java,Multithreading,导入java.util.Scanner 导入thread.prompt 公共类Answer3扩展了线程{ static Scanner s = new Scanner(System.in); static String [] ques = {"aa","bb","cc","dd","ee","ff","gg","hh","ii","jj"}; static String [] ans = {"a","b","c","d","e","f","g","h","i","j"}; /** *
static Scanner s = new Scanner(System.in);
static String [] ques = {"aa","bb","cc","dd","ee","ff","gg","hh","ii","jj"};
static String [] ans = {"a","b","c","d","e","f","g","h","i","j"};
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] q=null;
for(int i=0;i<ques.length;i++)
{
//q[i]=q[i](ques[i]);
q[i].start();
if (ques[i] != null)
Thread.interrupt();
}
}
public void run() {
try {
for(int i=0;i<ques.length;i++)
{
System.out.println(ques[i]);
Thread.sleep(30000);
System.out.println("Time out..!!!!");
}
} catch (InterruptedException e) {
for(int i=0;i<ques.length;i++)
{
if (ques[i].equalsIgnoreCase(ans[i]))
{
System.out.println("Congratulations...!!!!");
} else {
System.out.println("Better Luck Next Time...!!!!");
}
}
}
}}
使用sleep方法,如何修改上述代码以进行测验并获得候选人的结果?
任何解决方案。首先,您需要查看线程,因为您的代码可以说任何东西,但不能说线程,所以这里是您需要的线程代码 请尽量理解:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Application;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author husseyn
*/
public class quiz {
static Scanner keyboard;
static Thread reader;
static String answer;
static String[] questions={"Q1","Q2","Q3","Q4","Q5","Q6"};
static String[] answers={"Q1","Q2","Q3","Q4","Q5","Q6"};
static boolean wait=false;
public static void main(String[] args) {
new Thread(){
@Override
public void run() {
System.out.println("welcome to quiz v1.0");
try {
sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(quiz.class.getName()).log(Level.SEVERE, null, ex);
}
int questionIndex=0;
while (true) {
try {
questionIndex=(int)(Math.random()*questions.length);
System.out.println(questions[questionIndex]);
// System.out.println("your answer : ");
int count=0;
wait=true;
while (wait) {
sleep(1000);
count++;
//System.out.print(count+" ");
if (count==10) {
reader.stop();
wait=false;
}
}
wait=false;
if(answers[questionIndex].toLowerCase().trim().equals(answer.toLowerCase().trim())) {
System.out.println("congratulation");
}else{
if (answer.equals("")) {
System.out.println("you r an idio try harder");
}else{
System.out.println("good try but answer is false :p next time");
}
}
answer="";
} catch (Exception e) {
}
}
}
}.start();
new Thread(){
@Override
public void run() {
keyboard=new Scanner(System.in);
while (true) {
while (!wait) {
try {
sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(quiz.class.getName()).log(Level.SEVERE, null, ex);
}
try {
} catch (Exception e) {
}
System.out.print("answer: ");
reader=new Thread(){
@Override
public void run() {
answer=keyboard.nextLine();
wait=false;
}
};
reader.run();
}
}
}
}.start();
}
}
请更具体一点。你在这里向我们展示的代码有什么问题?我想要使用Thread的测试代码实现这不是一个代码请求站点。如果你有一个特定的问题,请详细描述它,并说明如何复制它。否则,你的问题很可能会被关闭。好的,兄弟…我想要一个单线程在此程序中执行,每个问题从字符串[]传递我的代码中的问题和每个问题都给出了30秒的时间…那么如何做到这一点呢?演示如何做到这一点超出了so中通常的答案长度。查看java.util.Timer并阅读一些关于线程同步的教程。-但是您确定要在您目前的编程经验水平上深入研究这些相当高级的概念吗经验?尝试编辑代码我使用了随机问题循环,10秒时间尝试理解和管理代码以满足您的需要。我这样做是为了给您留下一些练习您了解线程尝试查看时间任务非常有用请验证我的答案如果您发现它有用我已经验证了…您的代码帮助很大