2014-09-23 180 views
2

首先,我對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

+1

'如果dragon = true',這是一個任務,而不是條件 – Coffee 2014-09-23 17:13:27

+2

有問題的if語句似乎也超出了類中任何方法的範圍。 – user3062946 2014-09-23 17:15:24

+1

我看不到'龍'是在哪裏申明的,我想可能還有其他的bug – Coffee 2014-09-23 17:15:49

回答

1

這裏有一個semantic error

if (dragon = true) 
    public void run(){ /* etc */ 

你的意思不是說你想在這裏:你是龍VAR分配真實的,之後是總是如此。

您必須使用

if (dragon == true) 
2

一個錯誤是你使用=賦值運算符來測試平等。相反,請使用==運算符。

if (dragon == true) 

但是,你得到同樣的結果,如果你簡單地測試了布爾值本身:

if (dragon) 
3

=是賦值運算符。等於運算符是==,所以,你的代碼應該閱讀:

if (dragon == true) 

更妙的是,因爲dragonboolean,它可以直接評價:

if (dragon) 
+0

非常感謝您的幫助,所以我嘗試了這一點,但它仍然得到相同的錯誤!有任何想法嗎? – user3910313 2014-09-25 16:17:06

0

我編輯您的文章,可讀性更強。請注意else-if語句,它使您不必縮減代碼太多並使其更具可讀性。

但是,您在您的線程類中調用該類中不存在的變量。你的if語句也應該有@AndyThomas提到的格式,但你也可以不具有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"); 

     boolean dragon = false; 
     selection = scanner.next(); 
     if (selection.equalsIgnoreCase("dragon")){ 
      dragon = true; 
      new Threads(dragon, "Dragon", "THREAD1"); 
     }else if (selection.equalsIgnoreCase("baron")){ 
      new Threads(dragon, "Baron", "THREAD2"); 
     }else if (selection.equalsIgnoreCase("redbuffneutral")){ 
      new Threads(dragon, "Red Buff Neutral", "THREAD3"); 
     }else if (selection.equalsIgnoreCase("bluebuffneutral")){ 
      new Threads(dragon, "Blue Buff Neutral", "THREAD4"); 
     }else if (selection.equalsIgnoreCase("redbuffenemy")){ 
      new Threads(dragon, "Red Buff Enemy", "THREAD5"); 
     }else if (selection.equalsIgnoreCase("bluebuffenemy")){ 
      new Threads(dragon, "Blue Buff Enemy", "THREAD6"); 
     }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(boolean dragon, String x, String z){ 
     text = z; 
     name = x; 
     if (dragon == true) { 
      run(); 
     } 
    } 

    @Override 
    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); 
     } 
    } 
} 

我還沒有試過這種代碼,但它更有意義。讓我知道如何去:)

2

通過你的原代碼,我想如果條件應該是

if(Dragon == 360000){ 

如果你真的希望它是一個布爾值,這將是一個更好的辦法將其命名爲「isDragon 「把它放在如果條件。

還有一件事情,按照Java命名規則,你應該用第一個字符命名的局部變量被小寫。