我的Greenfoot程序作为greenfoot文件,但不是jar文件

时间:2017-05-19 02:54:18

标签: java greenfoot

我正在为学校制作一个充满词汇游戏的小街机。当我在greenfoot窗口中运行程序时,它运行得很好。但是当我创建一个jar文件时,它会在使用Greenfoot.ask()提示时停止。这是代码:

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

如果你需要整个项目,请告诉我,我会分享它。

1 个答案:

答案 0 :(得分:0)

没关系,问题是扫描仪没有读取文件,因为它无法在jar中找到路径。它在Greenfoot.ask暂停,因为在读取文件时,它抛出IOException,使文件继续,最终导致NullPointerException