Tipos de estructuras repetitivas de programación.
El estudiante debe crear un programa en Android Studio, en el que se incluya estructuras repetitivas.
Para este caso hare uso de una aplicación realizada para un examen en la cual se hizo uso de ciclos for para recorrer vectores y matrices.
Codigo:
package com.example.vendedores;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText etVend,etVend2,etVend3,etVend4;
EditText ETProducto,ETProducto1,ETProducto2,ETProducto3,ETProducto4,ETProducto5,ETProducto6,ETProducto7
,ETProducto8,ETProducto9,ETProducto10,ETProducto11;
TextView resulVendedorMasVendio,resulVendedorMenosVendio,resulProductMasVendido,resulProductMenosVendido,
NombreVen1,NombreVen2,NombreVen3,NombreVen4,Proven1,Proven2,Proven3,Proven4;
String [] Vendedores = new String[4];
int [][] Productos = new int[4][4];
int [] Sumas = new int[4];
int [] SumaCantProduct = new int [4];
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//EditText
etVend = (EditText) findViewById(R.id.etVend);
etVend2 = (EditText) findViewById(R.id.etVend2);
etVend3 = (EditText) findViewById(R.id.etVend3);
etVend4 = (EditText) findViewById(R.id.etVend4);
ETProducto = (EditText)findViewById(R.id.ETProducto);
ETProducto1 = (EditText)findViewById(R.id.ETProducto1);
ETProducto2 = (EditText)findViewById(R.id.ETProducto2);
ETProducto3 = (EditText)findViewById(R.id.ETProducto3);
ETProducto4 = (EditText)findViewById(R.id.ETProducto4);
ETProducto5 = (EditText)findViewById(R.id.ETProducto5);
ETProducto6 = (EditText)findViewById(R.id.ETProducto6);
ETProducto7 = (EditText)findViewById(R.id.ETProducto7);
ETProducto8 = (EditText)findViewById(R.id.ETProducto8);
ETProducto9 = (EditText)findViewById(R.id.ETProducto9);
ETProducto10 = (EditText)findViewById(R.id.ETProducto10);
ETProducto11 = (EditText)findViewById(R.id.ETProducto11);
//TextView
resulVendedorMasVendio = (TextView) findViewById(R.id.resulVendedorMasVendio);
resulVendedorMenosVendio = (TextView) findViewById(R.id.resulVendedorMenosVendio);
resulProductMasVendido = (TextView) findViewById(R.id.resulProductMasVendido);
resulProductMenosVendido = (TextView) findViewById(R.id.resulProductMenosVendido);
NombreVen1 = (TextView) findViewById(R.id.NombreVen1);
NombreVen2 = (TextView) findViewById(R.id.NombreVen2);
NombreVen3 = (TextView) findViewById(R.id.NombreVen3);
NombreVen4 = (TextView) findViewById(R.id.NombreVen4);
Proven1 = (TextView) findViewById(R.id.Proven1);
Proven2 = (TextView) findViewById(R.id.Proven2);
Proven3 = (TextView) findViewById(R.id.Proven3);
Proven4 = (TextView) findViewById(R.id.Proven4);
}
public void CalcularVendedores(View view){
//Se llenan las pos del vector de vendedores
Vendedores[0] = etVend.getText().toString() ;
Vendedores[1] = etVend2.getText().toString() ;
Vendedores[2] = etVend3.getText().toString() ;
Vendedores[3] = etVend4.getText().toString() ;
//Se llenan las pos de la matriz
Productos[0][0] = Integer.parseInt(ETProducto.getText().toString()) ;
Productos[0][1] = Integer.parseInt(ETProducto1.getText().toString()) ;
Productos[0][2] = Integer.parseInt(ETProducto2.getText().toString()) ;
Productos[1][0] = Integer.parseInt(ETProducto3.getText().toString()) ;
Productos[1][1] = Integer.parseInt(ETProducto4.getText().toString()) ;
Productos[1][2] = Integer.parseInt(ETProducto5.getText().toString()) ;
Productos[2][0] = Integer.parseInt(ETProducto6.getText().toString()) ;
Productos[2][1] = Integer.parseInt(ETProducto7.getText().toString()) ;
Productos[2][2] = Integer.parseInt(ETProducto8.getText().toString()) ;
Productos[3][0] = Integer.parseInt(ETProducto9.getText().toString()) ;
Productos[3][1] = Integer.parseInt(ETProducto10.getText().toString()) ;
Productos[3][2] = Integer.parseInt(ETProducto11.getText().toString()) ;
//suma las filas de la matriz y se llena el vector de sumas
for (int i = 0; i < Productos.length ; i++){
int suma = 0;
for (int a = 0 ; a < Productos[i].length ; a++){
suma += Productos[i][a];
}
Sumas[i] = suma;
}
//calculamos el vendedor que mas vendio
int NumMayor = Sumas[0]; //Suponemos que el numero mayor se encuentra en la primera pos
int pos = 0; //
for (int b = 0; b < Sumas.length; b++){
int numeroActual = Sumas[b];
if (numeroActual > NumMayor){
NumMayor = numeroActual;
pos = b;
}
}
//Mostramos el nombre del vendedor
String VendedorMasVendio = String.valueOf(Vendedores[pos]) ;
resulVendedorMasVendio.setText(VendedorMasVendio);
//calculamos vendedor que menos vendio
int NumMenor = Sumas[0]; //Suponemos que el numero menor se encuentra en la primera pos
int pos2 = 0; //
for (int b = 0; b < Sumas.length; b++){
int numeroActual2 = Sumas[b];
if (numeroActual2 < NumMenor){
NumMenor = numeroActual2;
pos2 = b;
}
}
String VendedorMenosVendio = String.valueOf(Vendedores[pos2]) ;
resulVendedorMenosVendio.setText(VendedorMenosVendio);
//calculamos el nombre del producto que mas se vendio y que menos se vendio
int PosMax = 0;
//se llena el vector
for (int x = 0; x < Productos.length; x++) {
int SumaColums = 0;
for (int y = 0; y < Productos.length; y++) {
SumaColums += Productos[x][y];
}
SumaCantProduct[x] = SumaColums;
}
//Buscamos mayor del vector
int NumMayor2 = SumaCantProduct[0]; //Suponemos que el numero mayor se encuentra en la primera pos
for (int b = 0; b < SumaCantProduct.length; b++){
int numeroActual3 = SumaCantProduct[b];
if (numeroActual3 > NumMayor2){
NumMayor2 = numeroActual3;
PosMax = b;
}
}
//Se muestra el producto mas vendido
if(PosMax == 0){
resulProductMasVendido.setText("Leche");
}
else if (PosMax == 1){
resulProductMasVendido.setText("Pan");
}
else if (PosMax == 2){
resulProductMasVendido.setText("Arroz");
}
//Producto menos vendido
int PosMenos = 0;
int NumMenor2 = SumaCantProduct[0]; //Suponemos que el numero mayor se encuentra en la primera pos
for (int b = 0; b < SumaCantProduct.length; b++){
int numeroActual4 = SumaCantProduct[b];
if (numeroActual4 < NumMenor2){
NumMenor2 = numeroActual4;
PosMenos = b;
}
}
//Se muestra el producto mas vendido
if(PosMenos == 0){
resulProductMenosVendido.setText("Leche");
}
else if (PosMenos == 1){
resulProductMenosVendido.setText("Pan");
}
else if (PosMenos == 2){
resulProductMenosVendido.setText("Arroz");
}
double prompted1 = Sumas[0]/3;
double prompted2 = Sumas[1]/3;
double prompted3 = Sumas[2]/3;
double prompted4 = Sumas[3]/3;
//Mostramos los promedios
String Ven1 = String.valueOf("Promedio de: " + Vendedores[0]) ;
NombreVen1.setText(Ven1);
String Ven1pro = String.valueOf(prompted1) ;
Proven1.setText(Ven1pro);
String Ven2 = String.valueOf("Promedio de: " +Vendedores[1]) ;
NombreVen2.setText(Ven2);
String Ven2pro = String.valueOf(prompted2) ;
Proven2.setText(Ven2pro);
String Ven3 = String.valueOf("Promedio de: " +Vendedores[2]) ;
NombreVen3.setText(Ven3);
String Ven3pro = String.valueOf(prompted3) ;
Proven3.setText(Ven3pro);
String Ven4 = String.valueOf("Promedio de: " +Vendedores[3]) ;
NombreVen4.setText(Ven4);
String Ven4pro = String.valueOf(prompted4) ;
Proven4.setText(Ven4pro);
}
}

Comentarios
Publicar un comentario