在if语句中运行

时间:2014-09-23 17:11:44

标签: java

首先,我对Java很新(大约一个星期),我对某些东西有点困惑,基本上我试图看看布尔值是否等于true然后启动一个线程,所以这是我的代码(顺便说一下,我使用两个类)

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行,它说:令牌“if”上的语法错误,无效的AnnotationName 任何帮助将不胜感激,如果您想了解我的问题的更多信息,请问!谢谢:D

5 个答案:

答案 0 :(得分:3)

=是赋值运算符。等于运算符是==,因此,您的代码应为:

if (dragon == true) 

更好的是,由于dragonboolean,因此可以直接评估:

if (dragon)

答案 1 :(得分:2)

其中一个错误是您使用=赋值运算符来测试相等性。相反,请使用==运算符。

if (dragon == true)

但是,如果您只是测试布尔值本身,则会得到相同的结果:

 if ( dragon ) 

答案 2 :(得分:2)

根据您的原始代码,我认为if条件应为

if(Dragon == 360000){

如果你确实想要它是一个布尔值,那么将它命名为“isDragon”将其置于if条件下会更好。

还有一件事,根据Java命名约定,您应该将以第一个字符命名的局部变量小写。

答案 3 :(得分:1)

这里有一个semantic error

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

你并不是要在这里说出你的想法:你为龙变量分配了真实,并且在之后它总是如此。

您必须使用

if (dragon == true) 

答案 4 :(得分:0)

我编辑了您的帖子以便于阅读。注意else-if语句,它使您不必过多地缩进代码并使其更具可读性。

但是,您在Threads类中调用该类中不存在的变量。你的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);
        }
    }
}

我还没有尝试过这段代码,但它更有意义。让我知道它是怎么回事:))