Array difficulty

-2

I am trying to make a code of the error in the array though. If anyone can give me a light.

Primary code:

import java.util.Scanner;

public class Tanque {

    int valor;
    private int kmTotal = 0;
    private int gasolinaTotal = 0;
    Scanner sc = new Scanner(System.in);

    public int kmLitro(final int Quilometros, final int Litros) {
        return Quilometros / Litros;
    }

    public int getValor() {
        return valor;
    }
    public void setValor(int valor) {
        this.valor = valor;
    }

    public int getKmTotal() {
        return kmTotal;
    }

    public void setKmTotal(int kmTotal) {
        this.kmTotal = kmTotal;
    }

    public int getGasolinaTotal() {
        return gasolinaTotal;
    }

    public void setGasolinaTotal(int gasolinaTotal) {
        this.gasolinaTotal = gasolinaTotal;
    }

    public int tanques[][] = new int [2][valor]; {
        for (int i=0;i<=valor-1;i++) {
        System.out.println("Digite os quilometros dirigidos do "+(i+1)+"° tanque.");
        tanques[0][i] = sc.nextInt();
        sc.nextLine();
        kmTotal =+ tanques[0][i];

        System.out.println("Digite os litros de gasolina gastos do "+(i+1)+"° tanque.");
        tanques[1][i] = sc.nextInt();
        sc.nextLine();
        gasolinaTotal =+ tanques[1][i];
        }
    }
}

And the test is:

public class TanqueTeste {  
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Tanque T = new Tanque();

        System.out.println("Quantos tanques foram testados?");
        T.setValor(sc.nextInt());       
        for (int j=0;j<=T.getValor()-1;j++) {
            System.out.println("A kilometragem por litro do "+(j+1)+"° Tanque é de "
                        + T.kmLitro(T.tanques[0][j],T.tanques[1][j]));
        }
        System.out.printf("A Quilometragem total é de: %d \n", T.getKmTotal());
        System.out.printf("A Quantidade de gasolina total é de %d \n",T.getGasolinaTotal());
        sc.close();
    }
}

I know that I have to invoke the method / array tank but I do not know / understood how yet ...

    
asked by anonymous 07.10.2018 / 04:19

1 answer

0

The Tank class should represent the attributes and behaviors of one and only one tank.

In this part of the code: public int tanques[][] = new int [2][valor]; {...} there are two problems:
1. The syntax is not correct. I did not understand if I was trying to make a variable or a method, I recommend that you review those concepts and their respective statements. 2. She does not belong to this class. Since the Tank class is the representation of a tank, your tank array must be somewhere else, in your case, in the main.

Eg.

int kmTotal = 0;
int gasolinaTotal = 0;
Scanner sc = new Scanner(System.in);

System.out.println("Quantos tanques foram testados?");
int quantidadeTanques = sc.nextInt();

Tanque[] tanques = new Tanque[quantidadeTanques];

for (int i = 0; i <= quantidadeTanques-1; i++) {
    tanques[i] = new Tanque();

    System.out.println("Digite os quilometros dirigidos do "+(i+1)+"° tanque.");
    tanques[i].setKmTotal(sc.nextInt());
    sc.nextLine();
    kmTotal =+ tanques[i].getKmTotal();

    System.out.println("Digite os litros de gasolina gastos do "+(i+1)+"° tanque.");
    tanques[i].setGasolinaTotal(sc.nextInt());
    sc.nextLine();
    gasolinaTotal += tanques[i].getGasolinaTotal();
}
    
07.10.2018 / 04:57