gpt4 book ai didi

java - Try-Catch 文本 block 问题

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:39 25 4
gpt4 key购买 nike

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

最佳答案

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

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

关于java - Try-Catch 文本 block 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36948057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com