分数和If语句不适用于Java
我正在做一个我今晚需要完成的程序,基本上它做了一个低俗版本的因子分解。。。 问题是,它不是给我数字,而是NaN。 这是我的密码: 第1类(涉及本程序的部分): 第2类:分数和If语句不适用于Java,java,math,Java,Math,我正在做一个我今晚需要完成的程序,基本上它做了一个低俗版本的因子分解。。。 问题是,它不是给我数字,而是NaN。 这是我的密码: 第1类(涉及本程序的部分): 第2类: public class mathey { double a,b,c; double solution1; double solution2; double discriminant; double x1 = 0; double x2 = 0; public mathey(int aN, i
public class mathey
{
double a,b,c;
double solution1;
double solution2;
double discriminant;
double x1 = 0;
double x2 = 0;
public mathey(int aN, int bN, int cN)
{
a = aN;
b = bN;
c = cN;
discriminant = (b*b)-4*a*c;
solvea();
solveb();
}
public String solvea()
{
solution1 = (-1*b + Math.sqrt(discriminant))/(2*a);
x1 = solution1;
if(discriminant > 0)
{
return"x = " + solution1;
}
else if(discriminant == 0)
{
return "x = " + solution1;
}
else
{
double root1complex = -b/(2*a);
double root1complex2 = Math.sqrt(-discriminant)/(2*a);
return root1complex + " + " + root1complex2 + " i ";
}
}
public String solveb()
{
solution2 = (-1*b - Math.sqrt(discriminant))/(2*a);
x2 = solution2;
if(discriminant > 0)
{
return"x = " + solution2;
}
else if(discriminant == 0)
{
return"x = " + solution2;
}
else
{
double root1complex = -b/(2*a);
double root1complex2 = Math.sqrt(-discriminant)/(2*a);
return root1complex + " - " + root1complex2 + " i ";
}
}
public mathey(int aFact, int bFact ,int cFact, int chooser)
{
a = aFact; b = bFact; c = cFact;
discriminant = (b*b)-4*a*c;
solvea();
solveb();
solvefact();
}
public String solvefact()
{
String Answer = "";
if((int)solution1 == solution1)
{
int wholeNum = (int)solution1/1;
double numerator = (solution1%1) * 10;
int denominator = 10;
while(numerator > denominator) {
denominator = denominator * 10;
}
Answer+="("+denominator+"x + "+((denominator * wholeNum) + numerator)+")";
}
else
{
Answer +="( x + " +(solution1*-1) +")";
}
if((int)solution2 == solution2)
{
int wholeNum = (int)solution2/1;
double numerator = (solution2%1) * 10;
int denominator = 10;
while(numerator > denominator) {
denominator = denominator * 10;
}
Answer+="("+denominator+"x + "+((denominator * wholeNum) + numerator)+")";
}
else
{
Answer +="( x + " +(solution2*-1) +")";
}
return Answer;
}
以下是输出:
Choose a Way to Solve
1. Quadratic Formula
2. Factoring
Which Method? [1/2]: 2
--------------------------------------------------
~Factoring~
--------------------------------------------------
in a polynomial, there are 3 important numbers used
to figure out x. they are a, b, and c, shown below.
1x^2 +2x -3
^ ^ ^
a b c
Please type a, b, and c here[a b c]: 1 2 -3
(10x + 10.0)(10x + -30.0)
我如何解决这个问题,从而获得应该得到的输出?(x+3.0)(x-1.0)您应该通过对函数进行单元测试来检查您的代码是否有可能。 例如,我注意到您使用了带有双倍数字的{%}运算符。该运算符处理整数,可能会将NaN结果拖到最后
您还在mathey()中声明变量,然后尝试在solvea()solveb()中使用不存在的变量。在4参数构造函数
mathey()
(您正在调用的构造函数)中,您正在重新声明变量a、b、c
,并将传递给它们的值赋值,屏蔽保持等于0(默认值)的实例变量。这些局部变量仅在构造函数的作用域中。在solveA()
和solveB()
中,a、b、c
再次引用实例变量(它们都是0),因此您要除以2*a
=0,这使得solution1
和solution2
等于NaN
将第二个构造函数中的行(如果继续使用)更改为
到
来解决掩蔽问题。不过,您很可能希望实例变量是double
s,而不是int
s,所以请更改
int a;int b;int c;
到
(您可以像这样执行相同类型的多个声明)
我不知道为什么你有两个Mathey
构造函数,所以要么放弃第二个(什么是chooser?
),只使用第一个,要么确保第二个也给行列式赋值
无论如何,这应该是一个开始。这不可能是您的全部代码。solution1
和solution2
在哪里声明和初始化?为什么要在mathey
构造函数以及“Class 1”中调用solvefact()
。@MattBall Check现在,我又添加了两个方法。基本上,Solution1和solution2被声明为双精度,用于存储二次公式的答案,很可能你只是在某处除以零。实际解决问题的第一步是在IDE中运行代码并逐行调试,观察变量的变化,找出哪里出了问题。solvea()
和solveb()
在一个字符上有所不同(+
和-
),你应该使用局部变量并返回结果,而不是使用实例变量,找出常见的代码。对不起,我对这一点很陌生,你这是什么意思“检查您是否有可能通过单元测试函数来检查代码”?嗨,我的意思是尝试单独测试您的函数,以检查它们是否按预期工作。如果开发环境中有调试工具,也会非常有帮助。您是否检查了为什么在余数%运算中使用带小数的数字?@Efren在java中,%运算符是为浮点操作数定义的。看见如果输入是NaN,如果被除数是无限的,或者除数是零,它会给出一个NaN结果。据我所知,如果使用变量%1,当没有小数点时,它将等于0,但当有小数点时,它将高于0。这段代码用于将小数转换为小数,以便以后以字符串形式显示Answer@patriciashanahan这是否意味着solution1和solution2都设置为0?我将如何解决此问题?我认为,由于a、b和c是公共变量,当我给它们赋值时,它们会改变……它们通常会改变,但您在Mathey
中用相同的名称重新声明局部变量,因此访问这些名称时引用的是局部变量,而不是实例变量。(即,在构造函数中Mathey()
)好吧,我按照你的建议做了,但我仍然有问题。。现在我得到了不同的输出。。我将用我的新代码和输出更新页面您需要使用调试器(或在不同位置输出变量的值),以便跟踪发生的情况。这比在黑暗中拍摄要有效得多。
double a = aN, b = bN, c = cN;
a = aN, b = bN, c = cN;
int a;int b;int c;
double a, b, c;