Dúvida na montagem de um código em java

4 respostas
P

Devo mostrar vetor na ordem original e invertida... O máximo que fiz esta em baixo:

Exercicio.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exercicio;

import java.util.Scanner;

/**
 *
 * @author Aluno
 */
public class Exercicio {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int[] vet = new int[10];
        int val1 = 0, val2 = 0;
        
        for(int x = 0; x < 10; x++)
        {
            System.out.print("vet["+x+"] = ");
            vet[val1] = sc.nextInt();
            val1 = val1 - 1;
        }
        
        for(int x = 0; x < 10; x++)
        {
            System.out.print(vet[val2]+" ");
            val2 = val2 + 1;
        }  
    }
}

4 Respostas

vitor_franca

Nem usei o método “Scanner”, só fazer invertido agora.

public class exercicioforum {

public static void main(String[] args) {  

	int[] vet = {1,2,3,4,5,6,7,8,9,10}; 

	for(int x = 0; x < vet.length; x++)  {

		int valor = vet[x]; 

		System.out.println(valor );
L
Scanner sc = new Scanner(System.in);
        
        int[] vet = new int[10];
        int val1 = 0, val2 = 10;
        
        for(int x = 0; x < 10; x++)
        {
            System.out.print("vet["+x+"] = ");
            vet[val1] = sc.nextInt();
            val1 = val1 - 1;
        }
        //Invertido
        for(int x = 10; x>0; x--)
        {
            System.out.print(vet[x]+" ");
         }
P
lucas93lange:
Scanner sc = new Scanner(System.in);
        
        int[] vet = new int[10];
        int val1 = 0, val2 = 10;
        
        for(int x = 0; x < 10; x++)
        {
            System.out.print("vet["+x+"] = ");
            vet[val1] = sc.nextInt();
            val1 = val1 - 1;
        }
        //Invertido
        for(int x = 10; x>0; x--)
        {
            System.out.print(vet[x]+" ");
         }

Está dando erro.

L
Scanner sc = new Scanner(System.in);  
          
        int[] vet = new int[10];  
                  
        for(int x = 0; x < vet.length; x++)  
        {  
            System.out.print("vet["+x+"] = ");
            vet[x] = sc.nextInt();         
        }  
        //Invertido  
        for(int x = vet.length-1; x >=0; x--)  
        {  
            System.out.print(vet[x]+" ");  
         }
Criado 3 de junho de 2015
Ultima resposta 5 de jun. de 2015
Respostas 4
Participantes 3