Difficulty in array

Asked

Viewed 43 times

-1

I am trying to make a code but error in the array. If someone can give me a light.

Master 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 I have to invoke the method/tank array but I don’t know/understand how yet...

  • "but error in array" - What error ? Which array ? On which line of code ? Invoke which method ?

1 answer

0

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

In that 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 these concepts and their respective statements.
2. She does not belong to that class. Since the Tank class is the representation of a tank, its tank array must be elsewhere, 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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.