📄 equationtest.java
字号:
package numericalAnalysis.equation;
import java.util.Scanner;
import numericalAnalysis.function.Function;
public class EquationTest {// -------------注意!!!!!!方程f(x)=0的f(x)在下面Function接口写入-----------------------------------------
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Equation equation;
MyFunction function;// f(x)=0的f(x)
String anotherChoice;// 新的求解方法
System.out.println("\n----------------------------本程序求解方程"
+ "----------------------------\n\n");
//确认f(x)已经写入Function接口
System.out.println("你要的方程f(x)=0的f(x)若未在源码中写好则请先写好,若写好了请按c:\n");
anotherChoice=input.next();
function=new MyFunction();
equation=new Equation(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("请选择:");
try {
choice = Integer.parseInt(input.next());
} catch (NumberFormatException e) {
System.out.println("请别胡乱输入!\n");
retry = true;
}
}
switch (choice) {
case 1:
System.out.println("用双点弦截法解方程,方程f(x)=0必须满足:\n" +
"1.f(a)*f(b)<0\n" +
"2.f'(x)不等于0,当x属于[a,b]时.");
//输入a,b
System.out.println("请输入a:\n");
double leftEndpoint = input.nextDouble();
System.out.println("请输入b:\n");
double rightEndpoint = input.nextDouble();
System.out.println();
System.out.println("方程的根:");
System.out.println(equation.getSolution(leftEndpoint, rightEndpoint));
break;
case 2:
System.out.println("用双点弦截法解方程,方程f(x)=0必须满足:\n" +
"1.f(a)*f(b)<0\n" +
"2.f'(x)不等于0,当x属于[a,b]时.");
//输入a,b
System.out.println("请输入a:\n");
leftEndpoint = input.nextDouble();
System.out.println("请输入b:\n");
rightEndpoint = input.nextDouble();
System.out.println();
System.out.println("计算过程:");
System.out.println(equation.doubleChordSection(leftEndpoint, rightEndpoint));
break;
}
System.out.println(" 要再使用其他方法吗? y/n:");
anotherChoice = input.next();
} while (anotherChoice.equalsIgnoreCase("y"));
}
private static class MyFunction implements Function {
// -------------注意!!!!!!方程f(x)=0的f(x)在此写入-----------------------------------------
public double f(double x) {
return Math.pow(x, 3)-Math.pow(x, 2)-x-1;
}
public double f(double x, double y) {
return 0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -