请忽略草率的编码。我是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.Scanner;导入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();
}
}发布于 2016-04-22 02:47:58
在这个方法上
public static void euler1()throws IOException您声明此方法可能抛出类似于FileNotFoundException的IOException。
你必须在每种可能发生的方法中做到这一点,包括。
public static void euler2()
{我强烈建议您使用IDE来
发布于 2016-04-22 03:00:22
问题是euler2()、euler3()和rungeKutta3()既没有捕获也没有声明抛出FileNotFoundException (或者它的超类IOException)。因此,您可以将相应的代码包装在try-catch-block中,或者为了简单起见,只需在这两个方法上声明抛出IOExceptions:
public static void euler2() throws IOException { [...] }
public static void euler3() throws IOException { [...] }
public static void rungeKutta3() throws IOException { [...] }
按照另一个答案中的建议,在你的main方法中抛出FileNotFoundException是没有效果的,因为这些已经被3个方法捕获了。此外,FileNotFoundException是一个IOException,所以我们已经讲过了。
发布于 2016-04-22 02:53:18
rungeKutta1()
euler1()
rungeKutta2()
euler2()
rungeKutta3()
euler3()以下是我注意到抛出filenotfound异常的方法,因为这些方法使用以下特定行
PrintWriter outputFile = new PrintWriter(filename);
您可以将方法更改为rungeKutta1()throws Exception,也可以将代码嵌入到每个方法中来处理异常,如下所示
try{
//your code inside the methods
}catch(Exception e){
System.out.println("exeption");
}https://stackoverflow.com/questions/36777900
复制相似问题