目前,该计划的输出是
Hi
Hola
Ciao
Bonjour
Guten tag
How do you say hi in English?
How do you say hi in Spanish?
How do you say hi in Italian?
How do you say hi in French?
How do you say hi in German?
这是我的预期,但我的目的是让输出更像这样:
How do you say hi in English?
Hi
How do you say hi in Spanish?
Hola
How do you say hi in Italian?
Ciao
How do you say hi in French?
Bonjour
How do you say hi in German?
Guten tag
我的预测是我必须制作两个线程才能做到这一点?我不确定如何使用线程,但我只是想确保线程是我接下来要研究的,以便弄清楚如何做我想做的事情。这只是一个例子,我的实际代码有点复杂,我觉得这个例子更容易理解我的意图。
好的,所以这是我的代码:
package test;
public class Class {
public static void main(String[] args) {
sayHiLanguages();
howToSayHi();
}
public static void sayHiLanguages() {
sleepThread(1);
System.out.println("Hi");
sleepThread(1);
System.out.println("Hola");
sleepThread(1);
System.out.println("Ciao");
sleepThread(1);
System.out.println("Bonjour");
sleepThread(1);
System.out.println("Guten tag");
}
public static void howToSayHi() {
System.out.println("How do you say hi in English?");
sleepThread(1);
System.out.println("How do you say hi in Spanish?");
sleepThread(1);
System.out.println("How do you say hi in Italian?");
sleepThread(1);
System.out.println("How do you say hi in French?");
sleepThread(1);
System.out.println("How do you say hi in German?");
}
public static void sleepThread(long time) {
try {
Thread.sleep(time * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
如果你们能指导我一篇关于线程的好文章,那就太棒了!
答案 0 :(得分:1)
让生活变得简单 -
package test;
public class Class {
public static void main(String[] args) {
sayHiAsLanguages();
}
public static void sayHiAsLanguages() {
new Thread(new Runnable(){
public void run(){
try{
//Change Thread sleep amount to see delay difference
System.out.println("How do you say hi in English?");
Thread.sleep(500);
System.out.println("Hi");
Thread.sleep(1000);
System.out.println("How do you say hi in Spanish?");
Thread.sleep(500);
System.out.println("Hola");
Thread.sleep(1000);
System.out.println("How do you say hi in Italian?");
Thread.sleep(500);
System.out.println("Ciao");
Thread.sleep(1000);
System.out.println("How do you say hi in French?");
Thread.sleep(500);
System.out.println("Bonjour");
Thread.sleep(1000);
System.out.println("How do you say hi in German?");
Thread.sleep(500);
System.out.println("Guten tag");
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}).start();
}
}
答案 1 :(得分:0)
除非你需要使用方法,否则我会推荐使用两个列表并迭代它们。
List<String> answers = new ArrayList<>();
answers.add("Hi");
answers.add("Hola");
answers.add("Ciao");
answers.add("Bonjour");
answers.add("Guten tag");
List<String> questions = new ArrayList<>();
questions.add("How do you say hi in English?");
questions.add("How do you say hi in Spanish?");
questions.add("How do you say hi in Italian?");
questions.add("How do you say hi in French?");
questions.add("How do you say hi in German?");
for(int i = 0; i < answers.size(); i++)
{
System.out.println(questions[i])
System.out.println(answers[i])
}
如果您打算使用它,我还建议您创建一个方法,以便快速添加到这些列表中。
addToLists(String question, String answer)
{
question.add(question); //question.add("How do you say hi in " + question); would make it even easier
answers.add(answer);
}