Translate-Traducir

9/10/16

vector ordenado java

Vector ordenado java

vectoroordenado
Vector ordenado

Desarrollar un programa donde se introduzca en un vector1  números impares y luego otro vector2 con números pares.

Comprobar al cargar los elementos cumplan la condición par e impar y ademas que al mostrar el resultado  por pantalla en un vector3  estén ordenados.

Usar interfaz visual swing .



import javax.swing.JOptionPane;

public class vectoresOrdenados {

    private static int[] valores1;
    private static int[] valores2;

    public void cargar() {
        int cant = 0;
        cant = Integer.parseInt(JOptionPane.showInputDialog(null,
                "Cuantos valores vas a introducir en cada vector    ",
                "N VALORES", JOptionPane.QUESTION_MESSAGE));

        valores1 = new int[cant];
        valores2 = new int[cant];
       
        for (int i = 0; i < valores2.length; i++) {
            valores2[i] = Integer.parseInt(JOptionPane.showInputDialog(null,
                    "Ingresa valores pares  "+ " "+(i+1), "N PARES",
                    JOptionPane.QUESTION_MESSAGE));
           
            while(valores2[i]%2==1){
            valores2[i] = Integer.parseInt(JOptionPane.showInputDialog(null,
                    "Error.ingresa solo valores pares   "+ " "+(i+1), "N PARES",
                    JOptionPane.QUESTION_MESSAGE));
            }
        }
       
        /////////////////////////////////////////////////////////////////
        for (int i = 0; i < valores1.length; i++) {
            valores1[i] = Integer.parseInt(JOptionPane.showInputDialog(null,
                    "Ingresa valores impares    "+ " "+(i+1), "N IMPARES",
                    JOptionPane.QUESTION_MESSAGE));
           
           
            while (valores1[i]%2==0){

            valores1[i] = Integer.parseInt(JOptionPane.showInputDialog(null,
                    "ERROR.Ingresa solo valores impares "+ " "+(i+1), "N IMPARES",
                    JOptionPane.QUESTION_MESSAGE));
           
            }
           
        }
       

    }
   
    public String ordenarimpares() {
        String impar;
        for(int k=0;k<valores1.length-1;k++) {
            for(int f=0;f<valores1.length-1-k;f++) {
                if (valores1[f]>valores1[f+1]) {
                    int aux;
                    aux=valores1[f];
                    valores1[f]=valores1[f+1];
                    valores1[f+1]=aux;
                }
           
        }
   
        }
        impar="[ ";
        for(int i=0; i<valores1.length;i++ ){
            impar=impar+" "+valores1[i]+" ";
        }
        impar=impar+" --";
        return impar;
       
    }
   
public String ordenarpares() {
    String par;
   
        for(int k=0;k<valores2.length-1;k++) {
            for(int f=0;f<valores2.length-1-k;f++) {
                if (valores2[f]>valores2[f+1]) {
                    int aux;
                    aux=valores2[f];
                    valores2[f]=valores2[f+1];
                    valores2[f+1]=aux;
                }
           
        }
   
        }
        par=" ";
        for(int i=0; i<valores2.length;i++ ){
            par=par+" "+valores2[i]+" ";
        }
        par=par+" ";
        return par;
    }
   
   
    public String mostrar(){
        String vector;
        vector=" ";
        for(int f=0;f<valores1.length;f++){
            vector=ordenarimpares()+" "+ordenarpares()+" ";
           
        }
        vector=vector+"]";
        return vector;
    }
   
    public void imprimir(){
        JOptionPane.showMessageDialog(null , "\nLos valores impares y pares  son "+mostrar(),"RESULTADO",
                   JOptionPane.INFORMATION_MESSAGE);
    }
    public static void main(String[]arg){
        vectoresOrdenados numeros;
        numeros=new vectoresOrdenados();
        numeros.cargar();
        numeros.imprimir();
       
       
    }
   
}





No olviden compartir