2016-04-29 84 views
1

所以基本上我試圖創建一個程序,當提示用戶詢問他們的名字時,如果他給出了文件「badWords.txt」中出現的名稱,用戶將不會能夠繼續。如果用戶輸入文檔中未找到的名稱,該程序將僅循環。我正在嘗試在下面做到這一點,但是我失敗了,我可以得到任何幫助嗎?我知道我做了第二部分正確的與catch語句,只需要第一個幫助。謝謝!Try-Catch Block Issue with Text

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

這個代碼在語法上不有效的Java。請發佈可編譯代碼。 –

回答

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

非常感謝你,我真的很感激它,這工作完美! – Alex

+0

歡迎你的朋友 – Hosseini