2017-04-10 38 views
-2

在開關盒中出現錯誤 - > RUNNING,ABORTED和READY無法解析爲變量。我能做些什麼來使它工作?試過枚舉,但它確實沒有工作。將字符串作爲開關盒

主類,我不能編輯:

public class Main { 
public static void main(String[] args) throws InterruptedException { 
StringTask task = new StringTask("A", 70000); 
System.out.println("Task " + task.getState()); 
task.start(); 
if (args.length > 0 && args[0].equals("abort")) { 

/*<- code that interrupts task after 1 sec and start a new thread 
*/ 
} 
while (!task.isDone()) { 
    Thread.sleep(500); 
    switch(task.getState()) { 
    //error case RUNNING: System.out.print("R."); break; 
    //error case ABORTED: System.out.println(" ... aborted."); break; 
    //error case READY: System.out.println(" ... ready."); break; 
    default: System.out.println("unknown state"); 
    } 

} 
System.out.println("Task " + task.getState()); 
System.out.println(task.getResult().length()); 
} 
} 

StringTask類:

public class StringTask implements Runnable { 
String string; 
String result = ""; 
String status = ""; 
int x; 
boolean end = false; 
boolean done = false; 

public StringTask(String string, int x) { 
    this.string = string; 
    this.x = x; 
    this.status = "CREATED"; 

} 

public void start() { 
    Thread thread = new Thread(this); 
    thread.start(); 
} 

public void run() { 
    this.status = "RUNNING"; 
    synchronized (this.result) { 
     try { 
      for (int i = 0; i < x; i++) { 
       result += string; 
      } 
      this.status = "READY"; 
      this.done = true; 
     } catch (Exception ex) { 
      this.status = "ABORTED"; 
      this.done = false; 
     } 
    } 
} 

public void abort() { 
    this.end = true; 
    this.done = true; 
    this.status = "ABORTED"; 
    Thread.interrupted(); 
} 

public StringTask() { 
    this.status = "ABORTED"; 
} 

public String getState() { 
    return this.status; 
} 

public boolean isDone() { 
    return this.done; 
} 

public String getResult() { 
    return this.result; 
} 
} 
+0

你會做的更好,如果你定義的狀態爲'enum'比爲字符串。無論如何,如果你想打開字符串,看看這個[指南](http://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html)。 – RealSkeptic

回答

0

您是否嘗試過在switch情況下使用String S'

"RUNNING"而不是RUNNING等?

+0

我無法編輯主類。這是我的家庭作業規則。 – marek

+0

然後你需要使用'Enum'來保存狀態。 – 0xflotus

1

我剛剛注意到你不允許編輯主類。爲了改善您的問題,你就必須做出enum存儲狀態:

public enum Status { 
    RUNNING, ABORTED, READY 
} 

改變StringTask#getState後返回Status,你可以用你的switch語句:

switch (task.getState()) { 
    case RUNNING: 
     System.out.print("R."); 
     break; 
    case ABORTED: 
     System.out.println(" ... aborted."); 
     break; 
    case READY: 
     System.out.println(" ... ready."); 
     break; 
    default: 
     System.out.println("unknown state"); 
} 
-1

看來你的任務是要讓主要課程與Enum一起工作,要做到這一點,您必須更改StringTask課程。改變狀態類型State

State status; 

    public StringTask(String string, int x) { 
     this.string = string; 
     this.x = x; 
     this.status = State.CREATED; 

    } 
    ... 

    public State getState() { 
     return this.status; 
    } 

    ... 

    enum State { 
     RUNNING, ABORTED, READY 
     } 

那麼你的開關應正常工作

switch(task.getState()) { 
    case RUNNING: 
     System.out.print("R."); 
     break; 
    case ABORTED: 
     System.out.println(" ... aborted."); 
     break; 
    case READY: 
     System.out.println(" ... ready."); 
     break; 
    default: 
     System.out.println("unknown state"); 
    } 
+2

複製粘貼時請多加註意。您只是刪除了註釋標記,但「錯誤」一詞不是Java代碼的一部分。 – RealSkeptic