请忽略草率编码。我是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
这是我收到错误的程序。请帮忙。我觉得我只需要另一双眼睛就可以找到问题所在。 import java.util.Scanner; import 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();
}
}
答案 0 :(得分:1)
关于这个方法
public static void euler1()throws IOException
您声明此方法可能会抛出类似FileNotFoundException的IOException。
您必须在可能发生的每种方法中执行此操作,包括。
public static void euler2()
{
我强烈建议您使用IDE
答案 1 :(得分:1)
问题是euler2(),euler3()和rungeKutta3()既没有捕获也没有声明FileNotFoundException(或者它的超类IOException)被抛出。所以你可以在try-catch-blocks中包含相应的代码,或者为了简单起见,只需要在这两个方法上声明IOExceptions:
public static void euler2() throws IOException {
[...]
}
public static void euler3() throws IOException {
[...]
}
public static void rungeKutta3() throws IOException {
[...]
}
按照另一个答案中的建议在主方法中抛出FileNotFoundException没有任何效果,因为已经有3个方法必须捕获它们。此外,FileNotFoundException是一个IOException,因此已经涵盖了。
答案 2 :(得分:0)
PrintWriter outputFile = new PrintWriter(filename);
这些是我注意到抛出filenotfound异常的方法 因为这些方法使用此特定行
rungeKutta1()throws Exception
您可以将方法更改为
try{
//your code inside the methods
}catch(Exception e){
System.out.println("exeption");
}
要么
你可以在每个方法中嵌入代码来处理这样的异常
Dim psi As New ProcessStartInfo
psi.UseShellExecute = True
psi.Verb = "print"
psi.WindowStyle = ProcessWindowStyle.Hidden
psi.FileName = sReport
Process.Start(psi)