2017-05-19 48 views
0

我正在爲學校製作一個充滿詞彙遊戲的小型街機。當我在greenfoot窗口中運行程序時,它運行完美。但是當我製作一個jar文件時,它會在用Greenfoot.ask()提示時停止。這裏的代碼:我的Greenfoot程序作爲greenfoot文件而不是jar文件

import greenfoot.*; 
import java.util.*; 
import java.io.*; 
import java.lang.*; 

public class Vocabulary 
{ 
    static ArrayList<World> back = new ArrayList<World>(); 
    static String vocab_List; 
    static Scanner list = null; 
    static ArrayList<String> last = new ArrayList<String>(); 
    static Scanner all; 
    static ArrayList<String> problems = new ArrayList<String>(); 
    static Map<String, String> answers = new HashMap<String, String>(); 
    static int x2 = 0; 
    static int y = 1; 
    static int count= 0; 
    static boolean an; 
    static World help; 
    static int right = 0; 

    public static boolean run() throws IOException 
    {  
     right = 0; 
     Collections.shuffle(problems); 
     for(int x = 0; x < 3; x++) 
     { 
     String[] def = answers.get(problems.get(x)).split(" "); 
     String answer = Greenfoot.ask("What does " + problems.get(x) + " mean?"); 
     count = 0; 
     for(String temp: def) 
     { 
      if(answer.contains(temp)) 
       count++; 
     } 
     if(count > def.length/2 && count > 1 || count == def.length) 
     { 
      help.showText("Correct",300,100); 
      Greenfoot.delay(50); 
      right++; 
     } 
     else 
     { 
      help.showText("Actually... it means " + answers.get(problems.get(x)), 300, 100); 
      Greenfoot.delay(50); 
     } 
     if(right >= 2) 
     { 
      an = true; 
     } 
     else 
     { 
      an = false; 
     } 
     try 
      { 
      Thread.sleep(1000); 
     } 
     catch (InterruptedException e) 
     { 
      e.printStackTrace(); 
     }  
     help.showText("",300,100); 
     } 
     return an; 
    } 

    public static boolean run(int num) throws IOException 
    {  
     right = 0; 
     Collections.shuffle(problems); 
     for(int x = 0; x < num; x++) 
     { 
     String[] def = answers.get(problems.get(x)).split(" "); 
     String answer = Greenfoot.ask("What does " + problems.get(x) + " mean?"); 
     count = 0; 
     for(String temp: def) 
     { 
      if(answer.contains(temp)) 
       count++; 
     } 
     if(count > def.length/2 && count > 1 || count == def.length) 
     { 
      help.showText("Correct",300,100); 
      Greenfoot.delay(50); 
      right++; 
     } 
     else 
     { 
      help.showText("Actually... it means " + answers.get(problems.get(x)), 300, 100); 
      Greenfoot.delay(50); 
     } 
     if(right >= num/2) 
     { 
      an = true; 
     } 
     else 
     { 
      an = false; 
     } 
     try 
      { 
      Thread.sleep(1000); 
     } 
     catch (InterruptedException e) 
     { 
      e.printStackTrace(); 
     }  
     help.showText("",300,200); 
     } 
     return an; 
    } 

    public static void setBack(World world) 
    { 
     back.add(world); 
    } 
    public static void setMode(ArrayList<Integer> lesson) throws IOException 
    { 
     problems.clear(); 
     answers.clear(); 
     x2 = 0; 
     all = new Scanner(new File("All_Vocab.in")); 
     for(int x : lesson) 
     { 
      list = new Scanner(new File("Vocab " + x + ".in")); 
     while(all.hasNextLine()) 
     { 
      last.add(all.nextLine() + "\n"); 
     } 
     FileWriter fw = new FileWriter("All_Vocab.in"); 
      BufferedWriter bw = new BufferedWriter(fw); 
     for(int c = 0; c < last.size(); c++) 
     { 
      bw.write(last.get(c)); 
     } 
     bw.write(vocab_List+".in"); 
     bw.close(); 
     while(list.hasNextLine()) 
     { 
      problems.add(list.nextLine()); 
      answers.put(problems.get(x2), list.nextLine()); 
      x2++; 
     } 
     } 

    } 

    public static void setHelp(World hell) 
    { 
     help = hell; 
    } 
} 

如果你需要整個項目,告訴我,我會分享它。

回答

0

沒關係,問題在於掃描儀沒有讀取文件,因爲它無法在jar中找到路徑。它在Greenfoot.ask中暫停,因爲在讀取文件時,它拋出IOException,使文件繼續,最終導致NullPointerException

相關問題