TIPOS DE ESTRUCTURAS Y CICLO EJERCICIO 2

 


package com.example.sumaciclos;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
public int suma, ct, ct2;
public String res;
TextView tvfor, tvwh, tvdowh;
EditText edt1, edt2, edt3, edt4, edt5, edt6, edt7, edt8, edt9;
ImageButton btnsalir;
int[][] mat = new int[3][3];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnsalir = (ImageButton) findViewById(R.id.btnsalir);
tvfor = (TextView) findViewById(R.id.tvfor);
tvwh = (TextView) findViewById(R.id.tvwh);
tvdowh = (TextView) findViewById(R.id.tvdowh);
edt1 = (EditText) findViewById(R.id.edt1);
edt2 = (EditText) findViewById(R.id.edt2);
edt3 = (EditText) findViewById(R.id.edt3);
edt4 = (EditText) findViewById(R.id.edt4);
edt5 = (EditText) findViewById(R.id.edt5);
edt6 = (EditText) findViewById(R.id.edt6);
edt7 = (EditText) findViewById(R.id.edt7);
edt8 = (EditText) findViewById(R.id.edt8);
edt9 = (EditText) findViewById(R.id.edt9);

}


public void ciclofor(View view) {
suma = 0;
mat[0][0] = Integer.parseInt(edt1.getText().toString());
mat[0][1] = Integer.parseInt(edt2.getText().toString());
mat[0][2] = Integer.parseInt(edt3.getText().toString());
mat[1][0] = Integer.parseInt(edt4.getText().toString());
mat[1][1] = Integer.parseInt(edt5.getText().toString());
mat[1][2] = Integer.parseInt(edt6.getText().toString());
mat[2][0] = Integer.parseInt(edt7.getText().toString());
mat[2][1] = Integer.parseInt(edt8.getText().toString());
mat[2][2] = Integer.parseInt(edt9.getText().toString());

for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < 3; j++) {
suma = suma + mat[i][j];
}
}
res = String.valueOf(suma);
tvfor.setText(res);
}


public void ciclowhile(View view) {
suma = 1;
ct = 0;
ct2 = 0;
mat[0][0] = Integer.parseInt(edt1.getText().toString());
mat[0][1] = Integer.parseInt(edt2.getText().toString());
mat[0][2] = Integer.parseInt(edt3.getText().toString());
mat[1][0] = Integer.parseInt(edt4.getText().toString());
mat[1][1] = Integer.parseInt(edt5.getText().toString());
mat[1][2] = Integer.parseInt(edt6.getText().toString());
mat[2][0] = Integer.parseInt(edt7.getText().toString());
mat[2][1] = Integer.parseInt(edt8.getText().toString());
mat[2][2] = Integer.parseInt(edt9.getText().toString());

while (ct2 < 3) {
while (ct < 3) {
suma = suma * mat[ct][ct2];
ct++;
}
ct = 0;
ct2++;
}
res = String.valueOf(suma);
tvwh.setText(res);

}

public void ciclodowhile(View view) {
suma = 0;
ct = 0;
do {
suma = suma + mat[0][ct];
ct++;

} while (ct < 3);

res = String.valueOf(suma);
tvdowh.setText(res);

}

public void salir(View view) {
if (btnsalir.isClickable() == true) {
finish();
}
}
}

Comentarios

Entradas populares