2014-09-23 3 views
2

우선 Java (약 1 주일)에 익숙하지 않고 약간의 내용과 약간 혼동됩니다. 기본적으로 부울이 다음과 같은지 확인하려고합니다. 실제로 올 때 다음 스레드를 시작 사실, 그래서 여기에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); 
      } 
     } 
    } 
} 

내가 첫 번째 클래스에 문제가없는 오전 (BTW 나는 두 개의 클래스를 사용하고 있습니다) 내 코드, 그것은이다 스레드를 실행하면 문제가 생길 때 실제로 어떻게해야하는지 실마리가 없으며 두 번째 클래스의 16 번째 줄에서 오류가 발생합니다. s : 토큰 "if"에 대한 구문 오류, 잘못된 AnnotationName 도움을 주시면 감사하겠습니다. 내 문제에 대한 자세한 정보를 원하시면 그냥 물어보십시오! 감사합니다 :

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

당신은 당신이 생각하는 말을 의미하지 않는다 : D

+1

'dragon = true'는 조건문이 아니라 할당 임 – Coffee

+2

문제가있는 if 문도 클래스의 메서드 범위를 벗어난 것으로 보입니다. – user3062946

+1

'dragon'이 선언 된 곳을 보지 못했습니다. 다른 버그가있을 수 있다고 생각합니다. – Coffee

답변

1

여기 semantic error이 당신이 용 VAR에 충실 할당하고, 그 후 항상 사실입니다.

당신은 오류의

if (dragon == true) 
2

하나는 평등을 테스트 할 = 할당 연산자의 사용이다 사용해야합니다. 대신 == 연산자를 사용하십시오.

if (dragon) 
3

=는 할당 연산자입니다 : 당신은 단순히 부울 값 자체를 테스트하는 경우

if (dragon == true) 

그러나 같은 결과를 얻을. 항등 연산자는 코드를 읽어야합니다, ==, 그래서 : dragon 이후

if (dragon == true) 

더 좋은가하는 boolean, 그것은 직접적으로 평가 될 수있다 :

if (dragon) 
+0

도움을 주셔서 감사합니다. 그래서 이것을 시도했지만 여전히 같은 오류가 있습니다! 어떤 아이디어? – user3910313

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

하지만, 그것이 더 의미가 있습니다 : 그것은 다음과 같이 보일 것이다. 그것은 원래 코드 :

2

을 어떻게되는지 알려주세요, 당신이 실제로는 부울을 수 원하는 경우 경우 조건이

if(Dragon == 360000){ 

을해야한다 생각, 그것은 "isDragon 명명 더 좋은 방법이 될 것입니다 "if 조건에 넣습니다.

그리고 Java 명명 규칙에서 첫 번째 문자로 명명 된 로컬 변수를 소문자로 사용해야합니다.