Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Ejercicio3;
- import java.io.IOException;
- import Ejercicio2.Consola;
- public class OpArray {
- int [] enteros;
- int n;
- OpArray (int n ){
- enteros=new int [n];
- }
- void cargarArray () throws IOException{
- int b,num;
- System.out.println("Introduce numero:");
- num=Consola.leeInt();
- for( b=0;b< enteros.length && num!=0;b++){
- enteros[b]=new Integer( num );
- System.out.println("Introduce numero:");
- num=Consola.leeInt();
- }
- n=b;
- }
- void ver (){
- for (int i=0 ;i<n;i++)
- System.out.println("enteros[i]");
- }
- public int sustituir (int n) throws IOException{
- int cont=0;
- for (int i=0; i<enteros.length;i++){
- if (enteros[i]==n)
- System.out.print("Introduce numero");
- enteros[i]=Consola.leeInt();
- cont++;
- }
- return cont;
- }
- public String toString ()
- {
- String cadena="";
- for (int i=0; i<enteros.length;i++){
- cadena=cadena+enteros[i];
- }
- return cadena;
- }
- public boolean esPrimo (int num){
- int cont=0;
- for (int d=2;d <=n/2;d++){
- if (num%d==0){
- cont++;
- }
- }
- if (cont==0)
- return true;
- else
- return false;
- }
- public int indicePrimo (){
- boolean parar=false;
- int i=0;
- while ( i<enteros.length && !parar){
- if(esPrimo (enteros[i]) ){
- parar=true;
- }
- }
- if (parar)
- return i;
- else
- return -1;
- }
- public static void main(String[] args) throws IOException {
- // TODO Auto-generated method stub
- OpArray elem= new OpArray(21);
- elem.cargarArray();
- elem.ver();
- elem.sustituir(20);
- elem.toString();
- elem.indicePrimo();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment