我一直在开发一款Android应用,可以计算复杂数字的各种动作。我有几个错误,但stackoverflow社区非常有帮助,我确实意识到我的风格需要改进(命名事物)。然而问题是该程序正在计算错误,我已经对所有代码进行了三重检查,但由于某种原因它没有正确计算(即2 + 2不是4)并且logcat没有显示错误。我也尝试将输出答案的部分移动到各个地方,但它仍然会出错。这是我的代码:
package complex.OliverV;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.RadioButton;
import android.widget.EditText;
public class ComplexNumbersActivity extends Activity {
/** Called when the activity is first created. */
Button Check;
RadioButton plus, minus, multiply, div;
EditText X1,X2,Y1,Y2;
TextView Ans;
int sign;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Check = (Button) findViewById(R.id.Check);
plus = (RadioButton) findViewById(R.id.plus);
minus = (RadioButton) findViewById(R.id.minus);
multiply = (RadioButton) findViewById(R.id.multiply);
div = (RadioButton) findViewById(R.id.div);
Ans = (TextView) findViewById(R.id.Ans);
X1=(EditText) findViewById(R.id.X1);
X2=(EditText) findViewById(R.id.X2);
Y1=(EditText) findViewById(R.id.Y1);
Y2=(EditText) findViewById(R.id.Y2);
plus.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sign=1;
}
});
minus.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sign=2;
}
});
multiply.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sign=3;
}
});
div.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sign=4;
}
});
Check.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String xs=X1.getText().toString();
String xss=X2.getText().toString();
String ys=Y1.getText().toString();
String yss=Y2.getText().toString();
double x3, y3;
if(!xs.equals("") && !xss.equals("") && !ys.equals("") && !yss.equals("")&& xs != null && xss != null && ys != null && yss != null)
{
double x1=Double.parseDouble(xs);
double x2=Double.parseDouble(xss);
double y1=Double.parseDouble(ys);
double y2=Double.parseDouble(yss);
switch(sign)
{
case(1):
{
x3=(x1+x2);
y3=(y1+y2);
Ans.setText("x = "+x3+"y = "+y3);
}
case(2):
{
x3=(x1-x2);
y3=(y1-y2);
Ans.setText("x = "+x3+"y = "+y3);
}
case(3):
{
x3=((x1*x2) - (y1*y2));
y3=((x2*y1) + (x1*y2));
Ans.setText("x = "+x3+"y = "+y3);
}
case(4):
{
if(x2!=0 && y2!=0)
{
x3 = ((x1 * x2) + (y1 * y2)) / ((x2 * x2) + (y2 * y2));
y3 = ((x2 * y1) - (x1 * y2)) / ((x2 * x2) + (y2 * y2));
Ans.setText("x = "+x3+"y = "+y3);
}
else
{
Ans.setText("Enter valid numbers!");
}
}
}
}
else
{
Ans.setText("Enter valid numbers!");
}
}
});
}
}
答案 0 :(得分:2)
您在break;
中遗漏了switch
条。在每个case
的末尾添加它们。此外,每个{}
都不需要case
。