Clase Arrays:
package PackArray;
import java.io.*;
public class Arrays {
public static InputStreamReader Leer = new InputStreamReader(System.in);
public static BufferedReader Teclado = new BufferedReader(Leer);
private int []a=new int[5];
public Arrays() {
this.a[0]=0;}
public void Llenar()throws IOException{
int b;
for(int i=0;i<5;i++){
this.a[i]=Integer.parseInt(Teclado.readLine());}
}
public void Mostrar(){
int b;
for(int i=0;i<5;i++){
System.out.println(+this.a[i]);}
}
public int Suma(){
int r=0;
for(int i=0;i<5;i++){
r+=this.a[i];
}
return r;
}
}
Clase Principal
package PackArray;
import java.io.*;
public class Principal {
public static InputStreamReader Leer = new InputStreamReader(System.in);
public static BufferedReader Teclado = new BufferedReader(Leer);
public static void main(String[] args) throws IOException
{ int a;
Arrays Obj = new Arrays();
System.out.println("Ingrese 5 numeros");
Obj.Llenar();
System.out.println("Los numeros ingresados fueron");
Obj.Mostrar();
a=Obj.Suma();
System.out.println("La suma es "+a);
}
}
Ejecucion:
No hay comentarios:
Publicar un comentario