英文:
Try/catch blocks and if statements in rainfall-averaging program
问题
我正在编写一个降雨平均计算程序。该程序允许用户输入文件名,如果找不到文件,则提示用户重新输入文件名。用户有4次尝试的机会,之后应用程序将在不处理数据的情况下退出,该应用程序本身就是一个像我之前说的降雨平均计算程序。
package experiment8;
import java.io.*;
import java.util.Scanner;
public class Exceptions
{
static Scanner inFile;
public static void main(String[] args) throws IOException
{
int fileTry = 0;
String fileName;
Scanner inName = new Scanner(System.in);
System.out.println("Enter file name>");
fileName = inName.nextLine();
boolean fileOk;
do
{
fileOk = false;
try
{
Scanner scan = new Scanner(System.in);
Scanner file = new Scanner(new File("inData.dat"));
fileOk = true;
}
catch(FileNotFoundException error)
{
System.out.println("Reenter file name>");
fileName = inName.nextLine();
fileTry++;
}
} while (!fileOk && fileTry < 4);
PrintWriter outFile = new PrintWriter(new FileWriter("outData.dat"));
if (fileOk && fileTry < 4 )
{
int numDays = 0;
double average;
double inches = 0.0;
double total = 0.0;
while (inFile.hasNextFloat())
{
inches = inFile.nextFloat();
total = total + inches;
outFile.println(inches);
numDays++;
}
if (numDays == 0)
System.out.println("Average cannot be computed " +
" for 0 days.");
else
{
average = total / numDays;
outFile.println("The average rainfall over " +
numDays + " days is " + average);
}
inFile.close();
}
else
System.out.println("Error");
outFile.close();
}
}
我正在尝试编写这个程序,以便在输入正确的文件名 "inData.dat" 时,我能够获得正确的输出。然而,当我这样做时,我继续被提示重新输入文件名,之后我会在接下来的3次尝试后收到 "Error" 信息。我的 try/catch 块或 if 语句是否存在问题?
英文:
I am coding a rainfall averaging program. The program lets the user input a file name and if the file cannot be found, then the user is prompted to reenter the file name. The user gets 4 tries before the application quits without processing the data, and the application itself is a rainfall averaging program like I said.
package experiment8;
import java.io.*;
import java.util.Scanner;
public class Exceptions
{
static Scanner inFile;
public static void main(String[] args) throws IOException
{
int fileTry = 0;
String fileName;
Scanner inName = new Scanner(System.in);
System.out.println("Enter file name>");
fileName = inName.nextLine();
boolean fileOk;
do
{
fileOk = false;
try
{
Scanner scan = new Scanner (System.in);
Scanner file = new Scanner(new File("inData.dat"));
fileOk = true;
}
catch(FileNotFoundException error)
{
System.out.println("Reenter file name>");
fileName = inName.nextLine();
fileTry++;
}
} while (!fileOk && fileTry < 4);
PrintWriter outFile = new PrintWriter(new FileWriter("outData.dat"));
if (fileOk && fileTry < 4 )
{
int numDays = 0;
double average;
double inches = 0.0;
double total = 0.0;
while (inFile.hasNextFloat())
{
inches = inFile.nextFloat();
total = total + inches;
outFile.println(inches);
numDays++;
}
if (numDays == 0)
System.out.println("Average cannot be computed " +
" for 0 days.");
else
{
average = total / numDays;
outFile.println("The average rainfall over " +
numDays + " days is " + average);
}
inFile.close();
}
else
System.out.println("Error");
outFile.close();
}
}
I am trying to code this program so when I input the correct file name, "inData.dat", I will get the proper output. However, when I do this, I continue to get prompted to reenter the file name for the next 3 times, after which I get the "Error" message. Is there something wrong with my try/catch blocks or if statements?
答案1
得分: 0
我对你的代码有两个问题。
-
在try块中,
Scanner scan = new Scanner(System.in);
这一行的目的是什么? -
为什么在do-while块获取文件后,你在if语句中检查了
if (fileOk && fileTry < 4)
这个条件?这似乎是多余的。do-while块已经检查了相同的条件。一旦程序执行到这个if语句,这个条件肯定已经满足了。如果不满足,do-while块会再次运行。
有可能出现这样一种情况,do-while结束是因为找到了文件,但是这个if语句的条件为假,因为 fileTry < 4
。我不明白为什么在找到正确文件后还要关心fileTry计数。如果用户尝试输入文件名4次,并且在最后一次尝试中输入正确,程序将进入这个if语句的else部分并打印 "Error"。
英文:
I have two questions about your code.
-
What is the purpose of the line
Scanner scan = new Scanner (System.in);
in the try-block? -
Why do you have an if statement check
if (fileOk && fileTry < 4)
after the do-while block for getting the file? It seems redundant. The do-while block checks for the same condition. Once the program gets to the location of this if-statement this condition must have been met. If it wasn't, the do-while would have run again.
It is possible for you to get to a case where the do-while ends because the file was found, and the condition of this if-statement is false because fileTry < 4. I don't understand why you would care about the fileTry counter once you found the correct file. If the user tried to enter the file name 4 times and got it correct on the last try, the program will go to the else part of this if-statement and print "Error".
答案2
得分: 0
以下是翻译好的内容:
有很多关于你的程序的问题。以下是一些帮助你解决问题的提示。
- 文件
inData.dat
不存在。请在适当的位置创建它。 - 一旦你解决了上述问题,在第40行会出现空指针错误:
inFile
为空。
我建议你在编辑器中打开它,比如 Visual Studio Code。它会指出许多警告,你也可以进行程序调试。
英文:
There are lot of issues with your program. Here are a few to get you on the way.
- The file
inData.dat
does not exist. Please create it in the appropriate place. - After you get over that hump, there would be a Null Pointer on Line 40:
inFile
is null.
My recommendation is to open it in an editor such as Visual Studio Code. It would point you to a lot of warnings and you could debug your program as well.
专注分享java语言的经验与见解,让所有开发者获益!
评论