How to assign a zero value to a variable (s) before running the program?

1

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));

            }
    });
}
}
    
asked by anonymous 11.12.2015 / 16:08

3 answers

1

You can always initialize the EditText with the value 0 in the activity_calculo_cr.xml using the android:text="0" attribute

<EditText
   android:id="@+id/edittext"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   .....
   .....
   android:text="0"
   android:inputType="number"/>

Additionally, the android:inputType="number" attribute indicates that EditText can only receive numbers.

    
11.12.2015 / 16:31
0

When you start the variable you can also put int x = 0. Or do as our friend said up there, setting directly in xml.

    
16.12.2015 / 15:59
0

Yan, in code the only option is to declare the value of the variable in the attribute, as follows.

public class CalculoCRActivity extends Activity {

EditText editText1;
Button button2;
private int variavel = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 .....

In this way you guarantee that the variable will always be worth 0 (zero) until you change its value.

    
28.12.2015 / 12:05