首先,我對Java很新穎(大概一個星期),我對某些東西有點困惑,基本上我試着看看一個布爾值是否等於真,則啓動一個線程,所以這裏是我的代碼(順便說一句,我用兩個班)在if語句中運行
package apackage;
import java.util.Scanner;
public class Threads2 {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
String selection;
System.out.println("Input one of the following answers for which timer you would like to start:");
System.out.println("Dragon, Baron, RedBuffNeutral, BlueBuffNeutral, RedBuffEnemy & BlueBuffEnemy");
selection = scanner.next();
if (selection.equalsIgnoreCase("dragon")){
boolean dragon = true;
Thread t1 = new Thread(new Threads("Dragon", "THREAD1"));
t1.start();
}else if (selection.equalsIgnoreCase("baron")){
Thread t2 = new Thread(new Threads("Baron", "THREAD2"));
t2.start();
}else if (selection.equalsIgnoreCase("redbuffneutral")){
Thread t3 = new Thread(new Threads("Red Buff Neutral", "THREAD3"));
t3.start();
}else if (selection.equalsIgnoreCase("bluebuffneutral")){
Thread t4 = new Thread(new Threads("Blue Buff Neutral", "THREAD4"));
t4.start();
}else if (selection.equalsIgnoreCase("redbuffenemy")){
Thread t5 = new Thread(new Threads("Red Buff Enemy", "THREAD5"));
t5.start();
}else if (selection.equalsIgnoreCase("bluebuffenemy")){
Thread t6 = new Thread(new Threads("Blue Buff Enemy", "THREAD6"));
t6.start();
}else{
System.out.println("You inputted an incorrect answer, please choose from the following next time:");
System.out.println("Dragon, Baron, RedBuffNeutral, BlueBuffNeutral, RedBuffEnemy & BlueBuffEnemy");
}
}
}
和
package apackage;
import java.util.Random;
public class Threads implements Runnable{
String name;
String text;
int time = 999;
int RedBuffNeutral, BlueBuffNeutral, RedBuffEnemy, BlueBuffEnemy = 300000;
int Dragon = 360000;
int Baron = 420000;
Random r = new Random();
public Threads(String x, String z){
text = z;
name = x;
}
if (dragon = true)
public void run(){
try{
System.out.printf("%s is dead for %d\n", name, Dragon);
Thread.sleep(Dragon);
System.out.printf("%s has respawned!\n", name);
}catch(InterruptedException exception){
System.out.printf("An error has occured in %s", name);
}
}
}
}
我有一流的沒有問題,這是當它實際上涉及到運行線程是當我遇到問題的時候,我真的不知道該怎麼做,並且錯誤發生在第二課的第16行,它說s:令牌「if」的語法錯誤,無效AnnotationName 任何幫助將不勝感激,如果您想了解我的問題的更多信息只需要問!謝謝:d
'如果dragon = true',這是一個任務,而不是條件 – Coffee 2014-09-23 17:13:27
有問題的if語句似乎也超出了類中任何方法的範圍。 – user3062946 2014-09-23 17:15:24
我看不到'龍'是在哪裏申明的,我想可能還有其他的bug – Coffee 2014-09-23 17:15:49