First of all, I'm still a bit lazy on the subject, hehehe
I'm doing a program in Eclipse (Android) that calculates the CR of a student, through two buttons: one is in charge of adding n notes and their respective weights (summation) and store the total note and the total weight, and another button in charge of dividing (total / pesototal).
But I can not assign values for total, pesototal, and n (initial), since they should start with value = 0, how could I do it? if you assign zero values to these variables inside the buttons, the sum does not work, since with each click, the value will be reset to zero and not stored and accumulated.
obs: n is a variable responsible only for determining how many notes were entered in the calculation obs2 .: in formulas where a variable receives its own value plus something, there is an error message "the local variable X may not have been initialized"
package com.example.calculocr_android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class CalculoCRActivity extends Activity {
EditText editText1;
EditText editText2;
EditText editText3;
EditText editText4;
Button button1;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculo_cr);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
editText4 = (EditText) findViewById(R.id.editText4);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
double nota, notatotal, cr;
int peso, pesototal, n;
nota = Double.parseDouble(editText2.getText().toString());
peso = Integer.parseInt(editText3.getText().toString());
notatotal = notatotal + (nota*peso);
pesototal = pesototal + peso;
n = n + 1;
editText1.setText(String.valueOf(n));
}
});
button2.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
double notatotal, cr;
int pesototal;
cr = notatotal/pesototal;
editText4.setText(String.valueOf(cr));
}
});
}
}