【问题标题】:Try-Catch Block Issue with TextTry-Catch 文本块问题
【发布时间】:2016-04-29 21:57:31
【问题描述】:

所以基本上我正在尝试创建一个程序,当提示用户询问他们的姓名时,如果他给出的名称出现在文件“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.");
  }
   }
  }   

【问题讨论】:

  • 此代码在语法上不是有效的 Java。请发布可编译的代码。

标签: java try-catch


【解决方案1】:

编辑第一部分并测试它的工作

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.");
        }
}

【讨论】:

    猜你喜欢
    • 2011-02-23
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 1970-01-01
    • 2011-08-26
    • 2011-01-24
    • 2011-09-10
    相关资源
    最近更新 更多