📄 integrationtest.java
字号:
package numericalAnalysis.integration;
import java.util.Scanner;
import numericalAnalysis.function.Function;
public class IntegrationTest {// -------------注意!!!!!!被积函数f(x)在下面Function接口写入-----------------------------------------
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Integration problem;
MyFunction function;//被积函数
String anotherChoice;// 新的积分方法
String another;// 新建一个积分问题的判断
System.out.println("\n----------------------------本程序求解积分问题"
+ "----------------------------\n\n");
do {
//确认被积函数已经写入Function接口
System.out.println("你要的被积函数若未在源码中写好则请先写好,若写好了请按c:\n");
another=input.next();
//输入积分区间左右端点
System.out.println("请输入积分区间左端点:\n");
double leftEndpoint = input.nextDouble();
System.out.println("请输入积分区间右端点:\n");
double rightEndpoint = input.nextDouble();
function=new MyFunction();
problem=new Integration(leftEndpoint,rightEndpoint,function);//新建这个定积分
do {
int choice = 0;
boolean retry = true;
while (retry) {
retry = false;
System.out.println();
System.out.println("1---牛顿柯特斯公式求解积分");
System.out.println("2---龙贝格法求解积分");
System.out.println("3---复化牛顿柯特斯公式求解积分");
System.out.println("请选择:");
try {
choice = Integer.parseInt(input.next());
} catch (NumberFormatException e) {
System.out.println("请别胡乱输入!\n");
retry = true;
}
}
switch (choice) {
case 1:
System.out.println("请输入牛顿柯特斯公式的n (0<n<8):");
int n = input.nextInt();
System.out.println();
System.out.println("牛顿柯特斯公式求解积分结果:");
System.out.println(problem.newtonCotes(n));
break;
case 2:
System.out.println("请输入龙贝格积分的允许误差:");
double errorAllowed = input.nextDouble();// 允许误差
System.out.println();
System.out.println("龙贝格法求解积分结果:");
System.out.println(problem.romberg(errorAllowed));
break;
case 3:
System.out.println("请输入大段数m:");
int m = input.nextInt();
System.out.println("请输入牛顿柯特斯公式的n (0<n<8):");
n = input.nextInt();
System.out.println();
System.out.println("复化牛顿柯特斯公式求解积分结果:\nI=");
System.out.println(problem.minuteNewtonCotes(m, n));
break;
}
System.out.println(" 要再使用其他积分方法(对于刚才这个定积分)吗? y/n:");
anotherChoice = input.next();
} while (anotherChoice.equalsIgnoreCase("y"));
System.out.println("\n 改变定积分区间左右端点? y/n:");
another = input.next();
} while (another.equalsIgnoreCase("y"));
}
private static class MyFunction implements Function {
// -------------注意!!!!!!被积函数f(x)在此写入-----------------------------------------
public double f(double x) {
return Math.sqrt(x);
}
public double f(double x, double y) {
return 0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -