printwriter我一直只通过3种方法获得这些编译答案。JAVA
请忽略粗心的编码。我对java还很陌生,我正在为学校做这个项目。我找不到导致此错误的原因
DeCrossDiscipline.java:455: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
PrintWriter outputFile = new PrintWriter(filename);
^
DeCrossDiscipline.java:533: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
PrintWriter outputFile = new PrintWriter(filename);
^
DeCrossDiscipline.java:623: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
PrintWriter outputFile = new PrintWriter(filename);
^
3 errors
这就是我正在处理错误的程序。请帮忙。我觉得我需要另一双眼睛来帮我找出问题所在。 导入java。util。扫描仪; 导入java。io.*
public class DeCrossDiscipline
{
public static void main(String[] args) throws IOException
{
Scanner keyboard = new Scanner(System.in);
int deSelection;
int method;
int method2;
int method3;
blanklines();
System.out.println("\tDifferential Equation Cross Discipline Project");
System.out.println();
demenu();
deSelection = keyboard.nextInt();
if (deSelection<1)
{
System.out.println();
System.out.println("That is not a choice. Please try again.\n");
demenu();
deSelection = keyboard.nextInt();
}
if (deSelection>3)
{
System.out.println();
System.out.println("That is not a choice. Please try again.\n");
demenu();
deSelection = keyboard.nextInt();
}
if (deSelection==1)
{menu1();
method=keyboard.nextInt();
if (method<1)
{
System.out.println();
System.out.println("Not a valid choice!");
menu1();
method=keyboard.nextInt();
}
if (method>2)
{
System.out.println();
System.out.println("Not a valid choice!");
menu1();
method=keyboard.nextInt();
}
if (method==1)
{rungeKutta1();
}
if (method==2)
{euler1();
}
}
if (deSelection==2)
{menu2();
method2=keyboard.nextInt();
if (method2<1)
{
System.out.println();
System.out.println("Not a valid choice!");
menu2();
method2=keyboard.nextInt();
}
if (method2>2)
{
System.out.println();
System.out.println("Not a valid choice!");
menu2();
method2=keyboard.nextInt();
}
if (method2==1)
{rungeKutta2();
}
if (method2==2)
{euler2();
}
}
if (deSelection==3)
{menu3();
method3=keyboard.nextInt();
if (method3<1)
{
System.out.println();
System.out.println("Not a valid choice!");
menu3();
method3=keyboard.nextInt();
}
if (method3>2)
{
System.out.println();
System.out.println("Not a valid choice!");
menu3();
method3=keyboard.nextInt();
}
if (method3==1)
{rungeKutta3();
}
if (method3==2)
{euler3();
}
}
}
public static void blanklines()
{
for (int l=0; l<11; l++)
{
System.out.println();
}
}
public static void demenu()
{
System.out.println("1. y'=(x+1)^2\n" +
"2. y'=3(x^2)*(y-4)^2\n" +
"3. y'=3(x^2)*(y-2)^2");
System.out.print("Please enter the number of which \ndifferential equation you want to use: ");
}
public static void menu1()
{
System.out.print("\n1. Runge Kutta\n" +
"2. Euler\n");
System.out.print("Please enter the number of which \nmethod you want to use: ");
}
public static void menu2()
{
System.out.print("\n1. Runge Kutta\n" +
"2. Euler\n");
System.out.print("Please enter the number of which \nmethod you want to use: ");
}
public static void menu3()
{
System.out.print("\n1. Runge Kutta\n" +
"2. Euler\n");
System.out.print("Please enter the number of which \nmethod you want to use: ");
}
public static void rungeKutta1() throws IOException
{
Scanner keyboard = new Scanner(System.in);
double y;
double h;
double x;
double i;
System.out.print("\nPlease enter an initial y value: ");
y = keyboard.nextDouble();
System.out.print("Please enter an initial x value: ");
x = keyboard.nextDouble();
System.out.print("Please enter a h value: ");
h = keyboard.nextDouble();
if (h<=0)
{
System.out.println("\nEnter a valid h value");
System.out.print("Please enter a h value: ");
h = keyboard.nextDouble();
}
System.out.print("Please enter the amount of results that you want: ");
i = keyboard.nextInt();
if (i<1)
{
System.out.println("\nPlease enter an amount of 1 or greater");
System.out.print("Please enter the amount of results that you want: ");
i = keyboard.nextInt();
}
System.out.println();
System.out.print("Your results will be printed\n"
+ "to a file. Please enter a name\n"
+ "for the file: ");
String filename = keyboard.next();
System.out.println();
System.out.println("x values\t" + "y values");
System.out.println("___________________________");
double K1, K2, K3, K4, K5, K6, K7, K8, K9, K10;
File file = new File(filename);
PrintWriter outputFile = new PrintWriter(filename);
for (int number=1; number<=i; number++)
{
System.out.printf("When x = %.2f\t", x);
System.out.printf("y = %.4f\n", y);
outputFile.printf("When x = %.2f\t" + "y = %.4f", x,y);
outputFile.println();
K1 = Math.pow(x+1, 2);
K2 = x+h/2;
K3 = y+(h/2)*K1;
K4 = Math.pow(K2+1, 2);
K5 = y+(h/2)*K2;
K6 = Math.pow(K2+1, 2);
K7 = x+h;
K8 = y+h*K6;
K9 = Math.pow(K7+1, 2);
K10 = y+h/6*(K1+2*(K4+K6)+K9);
x = x+h;
y = K10;
}
outputFile.close();
}
public static void euler1()throws IOException
{
Scanner keyboard = new Scanner(System.in);
double y;
double h;
double x;
double i;
System.out.print("\nPlease enter an initial y value: ");
y = keyboard.nextDouble();
System.out.print("Please enter an initial x value: ");
x = keyboard.nextDouble();
System.out.print("Please enter a h value: ");
h = keyboard.nextDouble();
if (h<=0)
{
System.out.println("\nEnter a valid h value");
System.out.print("Please enter a h value: ");
h = keyboard.nextDouble();
}
System.out.print("Please enter the amount of results that you want: ");
i = keyboard.nextInt();
if (i<1)
{
System.out.println("\nPlease enter an amount of 1 or greater");
System.out.print("Please enter the amount of results that you want: ");
i = keyboard.nextInt();
}
System.out.println();
System.out.print("Your results will be printed\n"
+ "to a file. Please enter a name\n"
+ "for the file: ");
String filename = keyboard.next();
System.out.println();
System.out.println("x values\t" + "y values");
System.out.println("___________________________");
double E1, E2, E3, E4, E5;
File file = new File(filename);
PrintWriter outputFile = new PrintWriter(filename);
for (int number=1; number<=i; number++)
{
System.out.printf("While x = %.2f\t", x);
System.out.printf("y = %.4f\n", y);
outputFile.printf("When x = %.2f\t" + "y = %.4f", x,y);
outputFile.println();
E1 = Math.pow(x+1, 2);
E2 = x+h;
E3 = y+(h*E1);
E4 = Math.pow(E2+1, 2);
E5 = y+h/2*(E1+E4);
x = x+h;
y = E5;
}
outputFile.close();
}
public static void rungeKutta2() throws IOException
{
Scanner keyboard = new Scanner(System.in);
double y;
double h;
double x;
int i;
System.out.print("\nPlease enter an initial y value: ");
y = keyboard.nextDouble();
System.out.print("Please enter an initial x value: ");
x = keyboard.nextDouble();
System.out.print("Please enter a h value: ");
h = keyboard.nextDouble();
if (h<=0)
{
System.out.println("\nEnter a valid h value");
System.out.print("Please enter a h value: ");
h = keyboard.nextDouble();
}
System.out.print("Please enter the amount of results that you want: ");
i = keyboard.nextInt();
if (i<1)
{
System.out.println("\nPlease enter an amount of 1 or greater");
System.out.print("Please enter the amount of results that you want: ");
i = keyboard.nextInt();
}
System.out.println();
System.out.print("Your results will be printed\n"
+ "to a file. Please enter a name\n"
+ "for the file: ");
String filename = keyboard.next();
System.out.println();
System.out.println("x values\t" + "y values");
System.out.println("___________________________");
double R1, R2, R3, R4, R5, R6, R7, R8, R9, R10;
File file = new File(filename);
PrintWriter outputFile = new PrintWriter(filename);
for (int number=1; number<=i; number++)
{
System.out.printf("While x = %.2f\t", x);
System.out.printf("y = %.4f\n", y);
outputFile.printf("When x = %.2f\t" + "y = %.4f", x,y);
outputFile.println();
R1 = 3*(x*x)*Math.pow((y-4), 2);
R2 = x+h/2;
R3 = y+(h/2)*R1;
R4 = (3*(R2*R2))*Math.pow(R3-4, 2);
R5 = y+(h/2)*R4;
R6 = (3*(R2*R2))*Math.pow(R5-4, 2);
R7 = x+h;
R8 = y+h*R6;
R9 = (3*(R7*R7))*Math.pow(R8-4, 2);
R10 = y+h/6*(R1+2*(R4+R6)+R9);
x = x+h;
y = R10;
}
outputFile.close();
}
public static void euler2()
{
int i;
double y;
double h;
double x;
Scanner keyboard = new Scanner(System.in);
System.out.print("\nPlease enter an initial y value: ");
y = keyboard.nextDouble();
System.out.print("Please enter an initial x value: ");
x = keyboard.nextDouble();
System.out.print("Please enter a h value: ");
h = keyboard.nextDouble();
if (h<=0)
{
System.out.println("\nEnter a valid h value");
System.out.print("Please enter a h value: ");
h = keyboard.nextDouble();
}
System.out.print("Please enter the amount of results that you want: ");
i = keyboard.nextInt();
if (i<1)
{
System.out.println("\nPlease enter an amount of 1 or greater");
System.out.print("Please enter the amount of results that you want: ");
i = keyboard.nextInt();
}
System.out.println();
System.out.print("Your results will be printed\n"
+ "to a file. Please enter a name\n"
+ "for the file: ");
String filename = keyboard.next();
System.out.println();
System.out.println("x values\t" + "y values");
System.out.println("___________________________");
double E1, E2, E3, E4, E5;
File file = new File(filename);
PrintWriter outputFile = new PrintWriter(filename);
for (int number=0; number<=i; number++)
{
System.out.printf("While x = %.2f\t", x);
System.out.printf("y = %.4f\n", y);
outputFile.printf("When x = %.2f\t" + "y = %.4f", x,y);
outputFile.println();
E1 = 3*(x*x)*Math.pow((y-4), 2);
E2 = x+h;
E3 = y+h*E1;
E4 = (3*(E2*E2))*Math.pow((E3-4), 2);
E5 = y+h/2*(E1+E4);
x = x+h;
y = E5;
}
outputFile.close();
}
public static void rungeKutta3()
{
Scanner keyboard = new Scanner(System.in);
double y;
double h;
double x;
int i;
System.out.print("\nPlease enter an initial y value: ");
y = keyboard.nextDouble();
System.out.print("Please enter an initial x value: ");
x = keyboard.nextDouble();
System.out.print("Please enter a h value: ");
h = keyboard.nextDouble();
if (h<=0)
{
System.out.println("\nEnter a valid h value");
System.out.print("Please enter a h value: ");
h = keyboard.nextDouble();
}
System.out.print("Please enter the amount of results that you want: ");
i = keyboard.nextInt();
if (i<1)
{
System.out.println("\nPlease enter an amount of 1 or greater");
System.out.print("Please enter the amount of results that you want: ");
i = keyboard.nextInt();
}
System.out.println();
System.out.print("Your results will be printed\n"
+ "to a file. Please enter a name\n"
+ "for the file: ");
String filename = keyboard.next();
System.out.println();
System.out.println("x values\t" + "y values");
System.out.println("___________________________");
double R1, R2, R3, R4, R5, R6, R7, R8, R9, R10;
File file = new File(filename);
PrintWriter outputFile = new PrintWriter(filename);
for (int number=1; number<=i; number++)
{
System.out.printf("While x = %.2f\t", x);
System.out.printf("y = %.4f\n", y);
outputFile.printf("When x = %.2f\t" + "y = %.4f", x,y);
outputFile.println();
R1 = 3*(x*x)*Math.pow((y-2), 2);
R2 = x+h/2;
R3 = y+(h/2)*R1;
R4 = (3*(R2*R2))*Math.pow(R3-2, 2);
R5 = y+(h/2)*R4;
R6 = (3*(R2*R2))*Math.pow(R5-2, 2);
R7 = x+h;
R8 = y+h*R6;
R9 = (3*(R7*R7))*Math.pow(R8-2, 2);
R10 = y+h/6*(R1+2*(R4+R6)+R9);
x = x+h;
y = R10;
}
outputFile.close();
}
public static void euler3()
{
int i;
double y;
double h;
double x;
Scanner keyboard = new Scanner(System.in);
System.out.print("\nPlease enter an initial y value: ");
y = keyboard.nextDouble();
System.out.print("Please enter an initial x value: ");
x = keyboard.nextDouble();
System.out.print("Please enter a h value: ");
h = keyboard.nextDouble();
if (h<=0)
{
System.out.println("\nEnter a valid h value");
System.out.print("Please enter a h value: ");
h = keyboard.nextDouble();
}
System.out.print("Please enter the amount of results that you want: ");
i = keyboard.nextInt();
if (i<1)
{
System.out.println("\nPlease enter an amount of 1 or greater");
System.out.print("Please enter the amount of results that you want: ");
i = keyboard.nextInt();
}
System.out.println();
System.out.print("Your results will be printed\n"
+ "to a file. Please enter a name\n"
+ "for the file: ");
String filename = keyboard.next();
System.out.println();
System.out.println("x values\t" + "y values");
System.out.println("___________________________");
double E1, E2, E3, E4, E5;
File file = new File(filename);
PrintWriter outputFile = new PrintWriter(filename);
for (int number=0; number<=i; number++)
{
System.out.printf("While x = %.2f\t", x);
System.out.printf("y = %.4f\n", y);
outputFile.printf("When x = %.2f\t" + "y = %.4f", x,y);
outputFile.println();
E1 = 3*(x*x)*Math.pow((y-2), 2);
E2 = x+h;
E3 = y+h*E1;
E4 = (3*(E2*E2))*Math.pow((E3-2), 2);
E5 = y+h/2*(E1+E4);
x = x+h;
y = E5;
}
outputFile.close();
}
}
# 1 楼答案
问题在于,euler2()、euler3()和rungeKutta3()既不捕获也不声明要抛出的FileNotFoundException(或其超类IOException)。因此,您可以将相应的代码包装在try-catch块中,或者为了简单起见,只声明要在这两个方法上抛出的IOException:
public static void euler2() throws IOException { [...] }
public static void euler3() throws IOException { [...] }
public static void rungeKutta3() throws IOException { [...] }
按照另一个答案中的建议在主方法中抛出FileNotFoundException没有任何效果,因为这3个方法已经捕获了这些异常。此外,FileNotFoundException是一个IOException,所以已经讨论过了
# 2 楼答案
这些是我注意到的抛出filenotfound异常的方法 因为这些方法使用这个特定的行
PrintWriter outputFile = new PrintWriter(filename);
您可以将方法更改为
rungeKutta1()throws Exception
或 您可以在每个方法中嵌入代码,以便像这样处理异常# 3 楼答案
关于这个方法
您声明此方法可能会引发类似FileNotFoundException的IOException
你必须在任何可能发生这种情况的方法中做到这一点,包括
我强烈建议您使用IDE来