我的代码工作正常,但这就是杀了我,我无法找到答案。我不能直接从命令行参数传递一个字符串到我的构造函数中,然后传递给在构造期间调用的方法(evaluateMethod)。出于某种原因,该方法不会将其视为String。它为什么这样做?我让它工作的唯一方法是将if / else语句放在我的main方法中,该方法调用构造函数并传入参数。附上我的代码。
/**
* Program to calculate approximation, actual value, absolute error, and relative error with a definite integral
* @author c-dub
*
*/
public class Integrate {
private String whichWay;
double upper, lower, deltaX, actual, absoluteError, relativeError;
int rect;
// Constructor
Integrate(String str, double a, double b, int n) {
this.whichWay = str;
this.lower = a;
this.upper = b;
this.rect = n;
this.deltaX = (b - a) / rect;
this.actual = (Math.pow(upper, 3) / 3) - (Math.pow(lower, 3) / 3);
evaluateMethod(whichWay);
System.out.println("Actual value: " + actual);
System.out.println("Absolute error: " + absoluteError + ", Relative error: " + relativeError + ".");
}
/**
* Evaluates all calculations
* @param whichWay, the string to determine left, right, trapezoid method
*/
private void evaluateMethod(String whichWay) {
if(whichWay == "l" || whichWay == "L") {
System.out.println("Using left hand endpoints, on the integral (" + lower + ", " + upper + ") with "
+ rect + " rectangles.....");
System.out.println("The approximate value is: " + integrateLeft() + ".");
}
else if(whichWay == "r" || whichWay == "R") {
System.out.println("Using right hand endpoints, on the integral (" + lower + ", " + upper + ") with "
+ rect + " rectangles.....");
System.out.println("The approximate value is: " + integrateRight() + ".");
}
else if(whichWay == "t" || whichWay == "T") {
System.out.println("Using the trapezoid method, on the integral (" + lower + ", " + upper + ") with "
+ rect + " rectangles.....");
System.out.println("The approximate value is: " + integrateTrapezoid() + ".");
}
else {
throw new IllegalArgumentException("Bad string input. Please restart program");
}
}
/**
* Left hand endpoint integration
* @return the approximate value as a double
*/
private double integrateLeft() {
double sum = 0;
// First interval to the last interval, depending on number of rectangles
double lowerAlt = lower;
for(int i = 0; i < rect; i++) {
sum += evaluate(lowerAlt);
lowerAlt = lowerAlt + deltaX;
}
absoluteError = Math.abs(actual - (sum * deltaX));
relativeError = Math.abs(absoluteError / actual);
// Multiply entire sum by deltaX
return Math.abs(sum * deltaX);
}
/**
* Right hand endpoint integration
* @return the approximate value as a double
*/
private double integrateRight() {
double sum = 0;
// First interval to the last interval, depending on number of rectangles
double lowerAlt = lower + deltaX;
for(double i = 0; i < rect; i++) {
sum += evaluate(lowerAlt);
lowerAlt = lowerAlt + deltaX;
}
absoluteError = Math.abs(actual - (sum * deltaX));
relativeError = Math.abs(absoluteError / actual);
// Multiply entire sum by deltaX
return Math.abs(sum * deltaX);
}
/**
* Trapezoid method of integration
* @return the approximate value as a double
*/
private double integrateTrapezoid() {
double sum = 0;
// first interval after the lower bound, to the last interval before upper bound depending on # of rectangles
double lowerAlt = lower + deltaX;
for(int i = 1; i < rect; i++) {
sum += evaluate(lowerAlt) * 2;
lowerAlt = lowerAlt + deltaX;
}
// Now pass the lower and upper values into the function and add to total sum
sum += evaluate(lower) + evaluate(upper);
absoluteError = Math.abs(actual - (sum * (deltaX / 2)));
relativeError = Math.abs(absoluteError / actual);
// Mulitply calculation by deltaX / 2
return Math.abs(sum * (deltaX / 2));
}
/**
* Method to define the actual function
* @param x, function input value
* @return the function output value f(x)
*/
private double evaluate(double x) {
//Please enter your desired function here:
return Math.pow(x, 2);
}
/**
* Main method
* @param args, the command line arguments
*/
public static void main(String[] args) {
String str;
double a = Double.parseDouble(args[1]);
double b = Double.parseDouble(args[2]);
int n = Integer.parseInt(args[3]);
if(args[0].equals("l") || args[0].equals("L")) {
new Integrate("l", a, b, n);
}
else if(args[0].equals("r") || args[0].equals("R")) {
new Integrate("r", a, b, n);
}
else if(args[0].equals("t") || args[0].equals("T")) {
new Integrate("t", a, b, n);
}
}
}
在我的evaluateMethod()方法中,它检查一个字符串,即使命令args传入一个合法的字符串字母(不是一个字符),它也没有看到它作为字符串并抛出异常。 感谢
答案 0 :(得分:0)
在你的evaluateMethod()中改变whichWay ==“l”与main()中的哪个类似于“e”(“l”):
private void evaluateMethod(String whichWay) {
if(whichWay.equals("l") || whichWay.equals("L")) {
System.out.println("Using left hand endpoints, on the integral (" + lower + ", " + upper + ") with "
+ rect + " rectangles.....");
System.out.println("The approximate value is: " + integrateLeft() + ".");
}
else if(whichWay.equals("r") || whichWay.equals("R")) {
System.out.println("Using right hand endpoints, on the integral (" + lower + ", " + upper + ") with "
+ rect + " rectangles.....");
System.out.println("The approximate value is: " + integrateRight() + ".");
}
else if(whichWay.equals("t") || whichWay.equals("T")) {
System.out.println("Using the trapezoid method, on the integral (" + lower + ", " + upper + ") with "
+ rect + " rectangles.....");
System.out.println("The approximate value is: " + integrateTrapezoid() + ".");
}
else {
throw new IllegalArgumentException("Bad string input. Please restart program");
}
}
因为要比较String值,您必须使用此功能。 (见文件)
==测试参考相等性
.equals()测试值相等
答案 1 :(得分:0)
在evaluateMethod()方法中,当您使用==
时,您正在使用.equals()
例如
if(whichWay == "l" || whichWay == "L")
应该是:
if(whichWay.equals("l") || whichWay.equals("L")
将所有字符串比较替换为.equals("[Letter]")
,它应该有效。
有趣的是,在NetBeansIDE中,您的程序运行正常。它只是对上述错误发出警告。