我的Java progpram不会正确处理字符串中的-1。它会跳过e.getActionCommand()中对于“equation”的所有if语句。我尝试从字符串和处理字符[0]制作一个字符数组,但这也不起作用。
if ("equation".equals(e.getActionCommand()))
{
if (interceptB != "0" && slopeX == "1")
{
if (strIntercept != "")
{
String slash = "[/]";
String[] slashedUp = interceptB.split(slash);
float numer = new Float(slashedUp[0]);
float denomer = new Float(slashedUp[1]);
float fl = numer / denomer;
String minus = "[-]";
String[] splitUp = interceptB.split(minus);
if (fl > 0)
{
textEquation.setText("y = x + " + interceptB);
}
else if (fl < 0)
{
textEquation.setText("y = x - " + splitUp[1]);
}
}
else
{
float numb = new Float(interceptB);
String minus = "[-]";
String[] splitUp = interceptB.split(minus);
if (numb > 0)
{
textEquation.setText("y = x + " + interceptB);
}
else if (numb < 0)
{
textEquation.setText("y = x - " + splitUp[1]);
}
}
}
else if (interceptB != "0" && slopeX == "-1")
{
if (strIntercept != "")
{
String slash = "[/]";
String[] slashedUp = interceptB.split(slash);
float numer = new Float(slashedUp[0]);
float denomer = new Float(slashedUp[1]);
float fl = numer / denomer;
String minus = "[-]";
String[] splitUp = interceptB.split(minus);
if (fl > 0)
{
textEquation.setText("y = -x + " + interceptB);
}
else if (fl < 0)
{
textEquation.setText("y = -x - " + splitUp[1]);
}
}
else
{
float numb = new Float(interceptB);
String minus = "[-]";
String[] splitUp = interceptB.split(minus);
if (numb > 0)
{
textEquation.setText("y = -x + " + interceptB);
}
else if (numb < 0)
{
textEquation.setText("y = -x - " + splitUp[1]);
}
}
}
else if (slopeX != "0" && interceptB == "0")
{
if (slopeX == "1")
{
textEquation.setText("y = x");
}
else if (slopeX == "-1")
{
textEquation.setText("y = -x");
}
else
{
textEquation.setText("y = " + slopeX + "x");
}
}
else if (slopeX == "0" && interceptB != "0")
{
textEquation.setText("y = " + interceptB);
}
else
{
if (strIntercept != "")
{
String slash = "[/]";
String[] slashedUp = interceptB.split(slash);
float numer = new Float(slashedUp[0]);
float denomer = new Float(slashedUp[1]);
float fl = numer / denomer;
String minus = "[-]";
String[] splitUp = interceptB.split(minus);
if (fl > 0)
{
textEquation.setText("y = " + slopeX + "x" + " + " + interceptB);
}
else if (fl < 0)
{
textEquation.setText("y = " + slopeX + "x" + " - " + splitUp[1]);
}
}
else
{
float numb = new Float(interceptB);
String minus = "[-]";
String[] splitUp = interceptB.split(minus);
if (numb > 0)
{
textEquation.setText("y = " + slopeX + "x" + " + " + interceptB);
}
else if (numb < 0)
{
textEquation.setText("y = " + slopeX + "x" + " - " + splitUp[1]);
}
}
}
}
if ("reset".equals(e.getActionCommand()))
{
point2.setEnabled(false);
getSlope.setEnabled(false);
getIntercept.setEnabled(false);
totalEquation.setEnabled(false);
firstPoint = null;
secondPoint = null;
slope = null;
totalSlope = null;
strIntercept = null;
slopeX = null;
interceptB = null;
x1 = 0;
x2 = 0;
y1 = 0;
y2 = 0;
slopex = 0;
slopey = 0;
simplifiedSlope = 0;
mx = 0;
totalMx = 0;
intercept = 0;
ySide = 0;
totalY = 0;
textField1.setText("");
textField2.setText("");
textSlope.setText("");
textIntercept.setText("");
textEquation.setText("");
}
}
public void GetFirstPoint()
{
firstPoint = textField1.getText();
String comma = "[,]";
String[] x1Y1 = firstPoint.split(comma);
x1 = new Float(x1Y1[0]);
y1 = new Float(x1Y1[1]);
}
public void GetSecondPoint()
{
secondPoint = textField2.getText();
String comma = "[,]";
String[] x2y2 = secondPoint.split(comma);
x2 = new Float(x2y2[0]);
y2 = new Float(x2y2[1]);
}
public void GetSlope()
{
slopey = y2 - y1;
slopex = x2 - x1;
if (slopey < 0 && slopex < 0)
{
slopey = -slopey;
slopex = -slopex;
}
if (slopex < 0)
{
slopey = -slopey;
slopex = -slopex;
}
int slopeGCF = calcGCF(slopey, slopex);
if (slopey % slopex == 0)
{
simplifiedSlope = (int)slopey/(int)slopex;
totalSlope = "";
}
else if (slopey % slopex != 0 && slopeGCF > 0)
{
slopey = slopey / slopeGCF;
slopex = slopex / slopeGCF;
String slopeY = RoundUp(slopey);
String slopeX = RoundUp(slopex);
totalSlope = slopeY + "/" + slopeX;
}
else
{
String slopeY = RoundUp(slopey);
String slopeX = RoundUp(slopex);
totalSlope = slopeY + "/" + slopeX;
}
}
public void GetIntercept()
{
if (totalSlope == "")
{
mx = simplifiedSlope * x1;
intercept = y1 - mx;
strIntercept = "";
}
else if (totalSlope != "")
{
mx = slopey * x1;
if (mx % slopex == 0)
{
totalMx = mx / slopex;
intercept = y1 - totalMx;
strIntercept = "";
}
else
{
ySide = y1 * slopex;
totalY = ySide - mx;
float interceptGCF = calcGCF(totalY, slopex);
float ceptGCFx = 0;
float ceptGCFy = 0;
if (totalY % slopex == 0)
{
intercept = (int)totalY / (int)slopex;
strIntercept = "";
}
else if (totalY % slopex != 0 && interceptGCF > 0)
{
ceptGCFy = totalY / interceptGCF;
ceptGCFx = slopex / interceptGCF;
strIntercept = RoundUp(ceptGCFy) + "/" + RoundUp(ceptGCFx);
}
else
{
strIntercept = RoundUp(totalY) + "/" + RoundUp(slopex);
}
}
}
}
public int calcGCF(float num, float denom)
{
float s;
if (num < 0)
{
num = -num;
}
else if (denom < 0)
{
denom = -denom;
}
if (num > denom)
{
s = denom;
}
else
{
s = num;
}
if (s > 0)
{
for (float i = s; i > 0; i--)
{
if ((num % i == 0) && (denom % i == 0))
{
return (int)i;
}
}
}
return -1;
}
public String RoundUp(float f)
{
int i = (int)f;
String valToStore = (i==f) ? String.valueOf(i) : String.valueOf(f);
return valToStore;
}
}
答案 0 :(得分:3)
字符串比较应使用"foo".equals(s)
,而不是==
。
(您有很多很多的重构可能性;它会使代码更多更容易推理。)
答案 1 :(得分:1)
使用String.equals()
代替==
来比较字符串。如果所有字符串在编译时都已知或被中断,则与==
的比较可能会成功,但通常不会,因为==
比较对象引用而不是内容。