Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在if语句中运行_Java - Fatal编程技术网

Java 在if语句中运行

Java 在if语句中运行,java,Java,首先,我对Java非常陌生(大约一周),我对一些东西有点困惑,基本上我想看看布尔值是否等于true,然后启动一个线程,所以这里是我的代码(顺便说一句,我使用了两个类) 及 我对第一个类没有任何问题,当它实际运行线程时,当我遇到问题时,我真的不知道怎么做,错误发生在第二个类的第16行,它说:令牌“if”上的语法错误,无效的AnnotationName 任何帮助都将不胜感激,如果你想了解更多关于我的问题的信息,请询问!谢谢:这里有一个: 您并不是想说出您在这里的想法:您正在将true赋值给drago

首先,我对Java非常陌生(大约一周),我对一些东西有点困惑,基本上我想看看布尔值是否等于true,然后启动一个线程,所以这里是我的代码(顺便说一句,我使用了两个类)

我对第一个类没有任何问题,当它实际运行线程时,当我遇到问题时,我真的不知道怎么做,错误发生在第二个类的第16行,它说:令牌“if”上的语法错误,无效的AnnotationName 任何帮助都将不胜感激,如果你想了解更多关于我的问题的信息,请询问!谢谢:这里有一个:

您并不是想说出您在这里的想法:您正在将true赋值给dragon var,并且之后总是true

你必须使用

if (dragon == true) 

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

if (dragon == true)
但是,如果只测试布尔值本身,则会得到相同的结果:

 if ( dragon ) 

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

if (dragon == true) 
更好的是,由于
dragon
是一个
boolean
,因此可以直接对其进行评估:

if (dragon)

为了便于阅读,我编辑了你的文章。请注意elseif语句,它可以避免代码缩进过多,并使代码更具可读性

但是,您正在调用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");
        }
    }
}


我没有尝试过这个代码,但它更有意义。让我知道它是如何运行的:)

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

if(Dragon == 360000){
如果您确实希望它是一个布尔值,那么将它命名为“isDragon”是一种更好的方法,将其置于If条件中


还有一件事,按照Java命名约定,您应该将第一个字符命名的局部变量用小写字母命名。

如果dragon=true,这是一个赋值,不是一个条件有问题的if语句似乎也超出了类中任何方法的范围。我不知道在哪里声明了
dragon
,我想可能还有其他错误,谢谢你的帮助,所以我尝试了一下,但仍然得到了相同的错误!有什么想法吗?
if (dragon)
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){