我正在尝试制作一个数学应用程序,并且使用我使用的这段代码,我总是得到不同数字的相同答案。该程序运行良好,它加载并且我可以输入数据但总是得到答案 0.0。
((- numB + Math.sqrt((Math.pow(numB, 2))-4*numA*numC))/2*numA)
完整代码
public class MainActivityQuadraticEquation extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_quadraticequation);
Button btnBack = (Button)findViewById(R.id.btnBack);
btnBack.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
Log.i("Click Occured","You Clicked the Back button");
Intent myIntent = new Intent(MainActivityQuadraticEquation.this, MainActivity.class);
startActivity(myIntent);
}
});
}
public void Calculate(View v)
{
EditText inputA = (EditText)findViewById(R.id.InputA);
EditText inputB = (EditText)findViewById(R.id.InputB);
EditText inputC = (EditText)findViewById(R.id.InputC);
Integer numA = Integer.parseInt(inputA.getText().toString()), numB = Integer.parseInt(inputB.getText().toString()), numC = Integer.parseInt(inputC.getText().toString());
Integer ans_1 = (int) ((- numB + Math.sqrt((Math.pow(numB, 2))-4*numA*numC))/2*numA);
Integer ans_2 = (int) ((- numB - Math.sqrt((Math.pow(numB, 2))-4*numA*numC))/2*numA);
double ans_1a = ans_1;
double ans_2a = ans_2;
String ans1 = String.valueOf(ans_1a);
String ans2 = String.valueOf(ans_2a);
TextView answer1 = (TextView)findViewById(R.id.Ans);
TextView answer2 = (TextView)findViewById(R.id.Ans2);
answer1.setText("Answer 1: " + ans1);
//System.out.println("Answer 1: " + ans1);
answer2.setText("Answer 2: " + ans2);
//System.out.println("Answer 2: " + ans2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
它可以,但它不起作用