Pergunta

Hello guys I m new to android and want to display the quadratic formula calculation here is the code i am doing in android there is no error showing in this code but still on the emulator its not displaying the result :-(

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button b1 = (Button) findViewById(R.id.btnSubmit);
    final EditText et1 = (EditText) findViewById(R.id.etVA);//whats wrong
    final EditText et2 = (EditText) findViewById(R.id.etVC);//whats wrong
    final EditText et3 = (EditText) findViewById(R.id.etVC);//whats wrong
    final TextView answer = (TextView) findViewById(R.id.txtResult);//whats wrong
    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            String a1 = et1.getText().toString();//whats wrong
            double a = Double.parseDouble(a1);//whats wrong

            String b1 = et2.getText().toString();//whats wrong
            double b = Double.parseDouble(b1);//whats wrong

            String c1 = et3.getText().toString();//whats wrong
            double c = Double.parseDouble(c1);//whats wrong

            double x1 = (-b+Math.sqrt(b*b-4*a*c))/(2*a);//whats wrong

            System.out.println("x1 = "+ x1);

            String R = x1+ "";  //whats wrong
            answer.setText("Your Answer is " + R); //whats wrong
        }
    });
}
Foi útil?

Solução 3

I have found the solution. Code was fine but I was only doing a simple logical mistake

final EditText et1 = (EditText) findViewById(R.id.etVA); 
final EditText et2 = (EditText) findViewById(R.id.etVC);//that is the mistake it should be like (R.id.etVB).... 
final EditText et3 = (EditText) findViewById(R.id.etVC); 

Outras dicas

try like this

        String a1 = et1.getText().toString();
        double a =  Double.valueof(a1);

        String b1 = et2.getText().toString();
        double b =  Double.valueof(b1);

        String c1 = et3.getText().toString();
        double c = Double.valueof(c1);

        double x1 = (-b+Math.sqrt(b*b-4*a*c))/(2*a);

        System.out.println("x1 = "+ x1);

        String R = String.valueof(x1);
        answer.setText("Your Answer is " + R); 

Your program doesn't have any errors and you must provide the valid inputs. Otherwise you will get the output Your Answer is NAN i.e., Not a Number.

The valid inputs in the sense, the values of a, b and c provided must satisfy the equation ax2 + bx + c = 0. So that the quadratic formula can be used find the roots.

Example Valid Inputs :

a = 1.0
b = 3.0
c = 2.0

Output For the above Inputs :

Your Answer is -1.0

call onclicklistner() through View

        b1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

        }
    });
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top