Try-Catch Block问题与文本

时间:2016-04-29 21:57:32

标签: java try-catch

所以基本上我正在尝试创建一个程序,当提示用户询问他们的名字时,如果他给出了一个出现在文件中的名字" badWords.txt"用户将无法继续。只有当用户输入文档中未找到的名称时,程序才会循环。我试图在下面这样做,但我失败了,我可以得到任何帮助吗?我知道我用catch语句做了第二部分正确,只需要帮助第一部分。谢谢!

import java.util.Scanner;
import java.util.Random;
import java.io.*;

public class NameExperiment
   /**
    * Prompts user ith 5 quick-fire addition problems, and times
    * the user for the response provided.
    */
   public static void main(String[] args)
  {
     Scanner in = new Scanner(System.in); 
    Random rand = new Random();

    System.out.print("Please enter your name: "); 
    String name = in.nextLine();
     try
      {
      Scanner badWords = new Scanner(new File("badWords.txt"));
      }
      while (badWords.hasNext())
      catch{
       {
      if (name.equalsIgnoreCase(badWords.next()))
       {
        throw (new BadWordException("Watch your tongue"));
    }
}
 }
      System.out.println("Hello " + name +
   ". Please Answer the questions as fast as you can.");


     for (int i = 0; i < 5; i++)
     {
     System.out.println("Hit <ENTER> when ready for a question."); 
     in.nextLine();

     int a = rand.nextInt(100); 
     int b = rand.nextInt(100);

     long startTime = System.currentTimeMillis();

     System.out.print(a + " + " + b + " = "); 
     String response = in.nextLine();
     try
     {
     int number = Integer.parseInt(response);

     long endTime = System.currentTimeMillis();




     String outcome = (number == a +
     b) ? "Correct!" : "Incorrect.";

     System.out.println(outcome); 
     System.out.println("That took " + (endTime -
     startTime) + " milliseconds");

     }
  catch (NumberFormatException exception)
     {
     System.out.print("Inappropriate Input:  please enter a number.");
     }

  }
  System.out.println("Thank you "+ name + ", goodbye.");
  }
   }
  }   

1 个答案:

答案 0 :(得分:0)

编辑第一部分并立即测试其工作

public class NameExperiment{
        /**
         * Prompts user ith 5 quick-fire addition problems, and times
         * the user for the response provided.
         */
        public static void main(String[] args) throws IOException {
            Scanner in = new Scanner(System.in);
            Random rand = new Random();
            Scanner badWords  = new Scanner(new File("src", "badWords.txt"));;

            System.out.print("Please enter your name: ");
            String name = in.nextLine();

            while (badWords.hasNext())
                if (name.equalsIgnoreCase(badWords.next()))
                {
                    throw new NumberFormatException("Watch your tongue");
                }
            System.out.println("Hello " + name +". Please Answer the questions as fast as you can.");


            for (int i = 0; i < 5; i++)
            {
                System.out.println("Hit <ENTER> when ready for a question.");
                in.nextLine();

                int a = rand.nextInt(100);
                int b = rand.nextInt(100);

                long startTime = System.currentTimeMillis();

                System.out.print(a + " + " + b + " = ");
                String response = in.nextLine();
                try
                {
                    int number = Integer.parseInt(response);

                    long endTime = System.currentTimeMillis();

                    String outcome = (number == a +
                            b) ? "Correct!" : "Incorrect.";

                    System.out.println(outcome);
                    System.out.println("That took " + (endTime -
                            startTime) + " milliseconds");

                }
                catch (NumberFormatException exception)
                {
                    System.out.print("Inappropriate Input:  please enter a number.");
                }

            }
            System.out.println("Thank you "+ name + ", goodbye.");
        }
}