How to calculate the subtraction of an array filled by the user from the last number to the first?

0

Could you help me with a problem please: Create an arrangement of the size that the user decides, fill the arrangement with numbers entered by the user, then calculate the subtraction from the last number entered until the first

package programa.pkg79;

import java.util.Scanner;

public class Programa79 {

    public static void main(String[] args) {
        Scanner lector = new Scanner(System.in);
        int tam, resta=0;
        System.out.println("Cuantos numeros quieres ingresar:");
        tam  = lector.nextInt();
        int arre[] = new int[tam];

        System.out.println("Ingrese los numeros:");
        for(int i=0;i<tam;i++){
            arre[i]=lector.nextInt();
        }

        //aquí estoy perdido no se como hacer la resta :(
    }    
}
    
asked by Ricardo Ramss Seplveda Becerra 26.11.2017 в 04:41
source

1 answer

0
Scanner sc = new Scanner(System.in);
int tamanio, resta = 0, aux = 0;
System.out.println("¿Que tamaño desea?");
tamanio = sc.nextInt();
int vector[] = new int[tamanio];

for(int i = 0; i < tamanio; i++){
    System.out.println(" posicion # " + i + "\nIngrese dato");
    vector[i] = sc.nextInt();
    if(i == (tamanio - 1)){
        aux = vector[i]; //guarda el ultimo dato ingresado
    }
} 

//resta del ultimo ingresado hasta el primero
for(int x = (tamanio - 2); x >= 0; x--) { //empieza desde el penultimo dato, recorriendo hasta el primero
    resta = aux - vector[x]; // se resta del ultimo ingresado al antepenultimo
    aux = resta; //toma el resultado anterior, para volverse a restar 
 }

System.out.println("La resta es"+resta);
    
answered by 26.11.2017 в 05:31