我的Java progpram不會正確處理字符串-1。它在e.getActionCommand()中爲「等式」跳過了所有那些if語句。我嘗試從字符串和處理字符[0]中創建一個字符數組,並且這也不起作用。Java在線等式程序中無法識別字符串「-1」。查看「等式」操作命令
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;
}
}
你可以將代碼修剪到相關部分嗎?這是一大堆代碼。 –
隨着詹姆斯的請求,我建議你創建一個[SSCCE](http://sscce.org/)。 – Jeffrey
修剪它這是在actionPerformed模塊 – dsta