Java-值意外重置为0
我正在制作一个求导计算器,要求用户输入多项式的阶数,然后是每个项的系数,部分原因是我是一个没有经验的程序员,不能解析像Java-值意外重置为0,java,loops,Java,Loops,我正在制作一个求导计算器,要求用户输入多项式的阶数,然后是每个项的系数,部分原因是我是一个没有经验的程序员,不能解析像3x^4/4+sin(x)这样的输入 这是我的课 package beta; import java.math.RoundingMode; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; import javax.swing.JOptionPane; pub
3x^4/4+sin(x)
这样的输入
这是我的课
package beta;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class DerivativeCalculator
{
public DerivativeCalculator(String d, String v)
{
int degree = Integer.parseInt(d);
double value = Double.parseDouble(v);
coeffList = new ArrayList<Double>();
for (int i = 0; i <= degree; i++)
{
String console = JOptionPane.showInputDialog("Enter the coefficient of the "
+ "x^" + i + " term.");
Double coeff = Double.parseDouble(console);
coeffList.add(coeff);
}
}
public double calc()
{
double dx = 0.0001;
double x1 = value;
double y1 = 0;
for (int d = degree; d >= 0; d--)
{
y1 += coeffList.get(d) * Math.pow(x1, d);
}
double x2 = x1 + dx;
double y2 = 0;
for (int d = degree; d >= 0; d--)
{
y2 += coeffList.get(d) * Math.pow(x2, d);
}
double slope = (y2 - y1)/ (x2 - x1);
DecimalFormat round = new DecimalFormat("##.##");
round.setRoundingMode(RoundingMode.DOWN);
return Double.valueOf(round.format(slope));
}
public String getEquation()
{
String equation = "";
for (int d = degree; d >= 0; d--)
{
equation = equation + String.valueOf(coeffList.get(d)) + "x^" + String.valueOf(d) + " + ";
}
return equation;
}
public String getValue()
{
return String.valueOf(value);
}
private int degree;
private double value;
private List<Double> coeffList;
}
运行此操作将创建一个小程序窗口,其中显示
5.0x^0+
0.0
0.0
这不是正确的导数
我调试了我的程序,一切都按预期运行,直到视图切换到我的测试类并执行g2.drawString(String.valueOf(myDerivCalc.calc()),10100)代码>
执行此行后,度数
(多项式的度数)重置为0,即使用户输入了5。这就搞乱了我班上所有的for循环
package beta;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class DerivativeCalculator
{
public DerivativeCalculator(String d, String v)
{
int degree = Integer.parseInt(d);
double value = Double.parseDouble(v);
coeffList = new ArrayList<Double>();
for (int i = 0; i <= degree; i++)
{
String console = JOptionPane.showInputDialog("Enter the coefficient of the "
+ "x^" + i + " term.");
Double coeff = Double.parseDouble(console);
coeffList.add(coeff);
}
}
public double calc()
{
double dx = 0.0001;
double x1 = value;
double y1 = 0;
for (int d = degree; d >= 0; d--)
{
y1 += coeffList.get(d) * Math.pow(x1, d);
}
double x2 = x1 + dx;
double y2 = 0;
for (int d = degree; d >= 0; d--)
{
y2 += coeffList.get(d) * Math.pow(x2, d);
}
double slope = (y2 - y1)/ (x2 - x1);
DecimalFormat round = new DecimalFormat("##.##");
round.setRoundingMode(RoundingMode.DOWN);
return Double.valueOf(round.format(slope));
}
public String getEquation()
{
String equation = "";
for (int d = degree; d >= 0; d--)
{
equation = equation + String.valueOf(coeffList.get(d)) + "x^" + String.valueOf(d) + " + ";
}
return equation;
}
public String getValue()
{
return String.valueOf(value);
}
private int degree;
private double value;
private List<Double> coeffList;
}
为什么会发生这种情况?有没有解决这个问题的建议?谢谢不是5.0x^0=5.0*1=5.0,它的导数是0。。
我相信您的代码正在工作。您将度和值重新定义为构造函数中的局部变量。它们使用相同的名称创建类变量
不要重新申报
而不是
int degree = <something>;
double value = <something>;
int度=;
双值=;
你需要
degree = <something>;
value = <something>;
degree=;
值=;
当您将评论作为答案发布时,您将面临向下投票。当这种情况发生时,您将失去rep,并且无法发表评论。我输入的等式类似于x^4-3x^3+x^2+2x^1+5x^0,但我的程序仅显示5.0x^0。它完全忽略了我的多项式的其余部分,直到我达到125次重复,除非我知道确切的答案,否则我帮不上忙?我马上删除这条评论。我认为这是一个有效的答案<代码>5.0 x^0
确实是一个常量函数。我只是不确定OP中的+
尾随字符是什么snippet@ChiefTwoPencils他还会做什么。。。这是一件非常合理的事情…我想补充一点,将属性放在类的末尾是一个糟糕的想法。它们应该在类的第一个构造函数之前。是的,我想知道为什么我的变量名没有像Eclipse通常格式化的那样变成蓝色