2014-10-17 132 views
-2

我希望能夠使用線程在程序中同時運行兩個循環,以執行類似程序的遊戲。然而,我不完全確定如何使用線程,我遇到了我一無所知的錯誤。我將發佈代碼,但忽略大部分代碼,這是我想要的一個有點空心的殼我只需要讓線程工作開始清理它。
我收到錯誤「不抽象,不重寫」,它突出了「類遊戲實現可運行」部分。這是代碼:線程問題

public class game implements Runnable 
{ 
@Override 
public void run(int time){ 
    try{ 
     time = 1; 
     while (time<=10){ 
      Thread.sleep(10); 
      System.out.print(time); 
      time++;    
     } 
     char clearer = (char)(12); 
     System.out.print(clearer); 
     System.out.println("You have ran out of time! Game over!"); 
     System.exit(0); 
    }catch (Exception e){} 
} 

public static void main (String args[]) throws Exception{ 
    System.out.println("Welcome to pseudo-Where's Wally, look for the lower cal l character."); 
    Thread.sleep(500); 
    System.out.println("You get 10 seconds to find it."); 
    Thread.sleep(500); 
    System.out.println("Ready?.."); 
    char clear = (char)(12); 
    Thread.sleep(500); 
    System.out.print(clear); 
    boolean timer = false, col = false, guess = false; 
    int length = 5, time = 0, rowNum = 1, y = 0;   
    String manip = (""); 
    int x = (length*length); 
    Random gen = new Random(); 
    int find = gen.nextInt(x)+1; 
    for (int collumn = 0; collumn<=length; collumn++){ 
     for (int row = 0; row<=length; row++){ 
      while (!col){ 
       y++; 
       System.out.print(" "+y);      
       if (y-1 == length){ 
        System.out.println(); 
        System.out.println(); 
        col = true; 
       } 
      } 
      System.out.print(" I"); 
      manip = manip + (" I"); 
     } 
     System.out.println("\t" + rowNum); 
     rowNum++;    
     manip = manip + (" I");    
    } 

    boolean contin = false; 
    do{ 
     if (find%3==0){ 
      contin = true; 
      find = find - 1; 
     } else if (find%3>0){ 
      find = find - 1; 
     } 
    }while (!contin); 

    String newManip = manip.substring(0,find)+'l'+manip.substring(find+1); 
    String subOne = newManip.substring(0,18); 
    String subTwo = newManip.substring(18,36); 
    String subThree = newManip.substring(36,54); 
    String subFour = newManip.substring(54,72); 
    String subFive = newManip.substring(72,90); 
    String subSix = newManip.substring(90,108); 
    System.out.println(subOne); 
    System.out.println(subTwo); 
    System.out.println(subThree); 
    System.out.println(subFour); 
    System.out.println(subFive); 
    System.out.println(subSix); 

    Thread threadA = new ThreadA();   

    threadA.start(); 

    while(guess != true){ 
     System.out.print("Answer (Only one try): "); 
     int answer = sc.nextInt(); 
     if (answer == finalAnswer){ 
      guess = true;     
     }  
    } 
} 

}

乾杯的任何幫助。

+0

你的錯誤與線程無關。 – 2014-10-17 22:39:24

+0

你不會重寫'run()'方法,所以你沒有真正實現'Runnable'。 – Voicu 2014-10-17 22:39:44

+0

如果你知道錯誤是什麼,你能幫忙嗎? – joescull 2014-10-17 22:41:50

回答

1

爲了實現Runnable,您需要覆蓋run()方法(除非您的類是abstract,這會打開另一個討論)。你在game課堂上有什麼是run(int time),這將不計算在內。

Runnable API:

Runnable接口應該由任何類,其實例旨在由一個線程執行來實現。 該類必須定義一個沒有參數的方法,稱爲run

Bold加我自己。