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

Asked

Viewed 359 times

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

            }
    });
}
}
  • 1

    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. :)

3 answers

1

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

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

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

0

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

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

This way you ensure that the variable will always be 0 (zero) until you change its value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.