java命令行无法识别字符串
我的代码运行得很好,但这正是折磨我的原因,我找不到答案。我无法将字符串直接从命令行参数传递到构造函数中,然后传递给构造期间调用的方法(evaluateMethod)。出于某种原因,该方法没有将其视为字符串。为什么要这样做?我让它工作的唯一方法是在我的主方法中放入if/else语句,这些语句调用构造函数并传入参数。附件是我的密码java命令行无法识别字符串,java,string,args,Java,String,Args,我的代码运行得很好,但这正是折磨我的原因,我找不到答案。我无法将字符串直接从命令行参数传递到构造函数中,然后传递给构造期间调用的方法(evaluateMethod)。出于某种原因,该方法没有将其视为字符串。为什么要这样做?我让它工作的唯一方法是在我的主方法中放入if/else语句,这些语句调用构造函数并传入参数。附件是我的密码 /** * Program to calculate approximation, actual value, absolute error, and relative
/**
* 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);
}
}
}
/**
*用定积分计算近似值、实际值、绝对误差和相对误差的程序
*@author c-dub
*
*/
公共类集成{
私用字符串whichWay;
双上、下、deltaX、实际、绝对误差、相对误差;
int-rect;
//建造师
集成(字符串str、双a、双b、整数n){
this.whichWay=str;
这个。下=a;
这个上=b;
this.rect=n;
this.deltaX=(b-a)/rect;
this.actual=(Math.pow(上,3)/3)-(Math.pow(下,3)/3);
评价方法(whichWay);
System.out.println(“实际值:+实际值”);
System.out.println(“绝对误差:+absoluteError+”,相对误差:+relativeError+”);
}
/**
*评估所有计算
*@param whichWay,判断字符串左、右、梯形的方法
*/
私有void evaluateMethod(字符串whichWay){
如果(whichWay==“l”| whichWay==“l”){
System.out.println(“使用左手端点,在整数(“+lower+”,“+upper+”)上使用”
+矩形+“矩形…”;
System.out.println(“近似值为:“+integrateLeft()+”);
}
else if(whichWay==“r”| whichWay==“r”){
System.out.println(“使用右手端点,在整数(“+lower+”,“+upper+”)上使用”
+矩形+“矩形…”;
System.out.println(“近似值为:“+integraterRight()+”);
}
else if(whichWay==“t”| whichWay==“t”){
System.out.println(“使用梯形方法,在积分(“+下+”,“+上+”)上加上”
+矩形+“矩形…”;
System.out.println(“近似值为:“+integrateTrapezoid()+”);
}
否则{
抛出新的IllegalArgumentException(“错误的字符串输入。请重新启动程序”);
}
}
/**
*左端点积分
*@以双精度返回近似值
*/
私有双积分函数({
双和=0;
//第一个间隔到最后一个间隔,取决于矩形的数量
双低=低;
对于(int i=0;i
在我的evaluateMethod()方法中,它检查字符串,即使命令args传递的是合法的字符串字母(不是字符),它也不会将其视为字符串,而是引发异常。
感谢您的评估方法()更改whichWay==“l”与whichWay.equals(“l”)类似于main():
因为要比较字符串值,必须使用此函数。(见文件
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");
}
}
if(whichWay == "l" || whichWay == "L")
if(whichWay.equals("l") || whichWay.equals("L")