在if语句中运行

问题描述:

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

这里有一个semantic error

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

你的意思不是说你想在这里:你是龙VAR分配真实的,之后是总是如此。

您必须使用

if (dragon == true) 

一个错误是你使用=赋值运算符来测试平等。相反,请使用==运算符。

if (dragon == true) 

但是,你得到同样的结果,如果你简单地测试了布尔值本身:

if (dragon) 

=是赋值运算符。等于运算符是==,所以,你的代码应该阅读:

if (dragon == true) 

更妙的是,因为dragonboolean,它可以直接评价:

if (dragon) 
+0

非常感谢您的帮助,所以我尝试了这一点,但它仍然得到相同的错误!有任何想法吗? – user3910313 2014-09-25 16:17:06

我编辑您的文章,可读性更强。请注意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); 
     } 
    } 
} 

我还没有试过这种代码,但它更有意义。让我知道如何去:)

通过你的原代码,我想如果条件应该是

if(Dragon == 360000){ 

如果你真的希望它是一个布尔值,这将是一个更好的办法将其命名为“isDragon “把它放在如果条件。

还有一件事情,按照Java命名规则,你应该用第一个字符命名的局部变量被小写。