0

我正在为学校制作一个充满词汇游戏的小街机。当我在 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;
    }
}

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

4

1 回答 1

0

没关系,问题是扫描仪没有读取文件,因为它在 jar 中找不到路径。它在 Greenfoot.ask 处暂停,因为在读取文件时,它抛出了 IOException,使文件继续,最终导致空指针异常

于 2017-05-31T02:04:34.903 回答