1
First of all, I’m still a little layy on the subject, hehehe
I’m doing a program on Eclipse (Android) that calculates the CR of a student, through two buttons: one takes care of adding n notes and their respective weights (sum) and store the total note and the total weight, and another button in charge of making the division (total note/pesototal).
However, I cannot assign values to total notation, pesototal and n (initials), since they should start with value = 0, as I could do? if assigning zero values for these variables inside the buttons, the sum does not work, since at each click, the value will be reset and not stored and accumulated.
Obs.: n is a variable responsible only for determining how many notes were inserted in the calculation Obs2.. in formulas where a variable gets 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));
}
});
}
}
Just a tip, don’t use button1, button2 etc. use buttonSomarNotas, buttonDividirPesos. don’t be lazy. is more serviceable and when you need maintenance will be easier. :)
– Hoppy