My math random method does not work

0

I have a problem, I was trying to solve a problem where I was asked to calculate the annual water consumption of a floor of a department, the department has 8 floors of 4 houses each floor, I created the matrix where the rows are the months of the year, and the columns are the houses (house 1, house 2, etc). When I start the console, when I choose option 1, which is the one I have (the other 2 options do not take them into account), I do not fill the matrix randomly as I have indicated, since it always throws me when the consumption is 0, I would appreciate it if they could review it and see where the error is

import java.util.*;


public class JavaApplication1 {


    public static void main(String[] args) {
    int[][] consumo = new int[11][31];
    Scanner leer = new Scanner(System.in);
    llenar(consumo);  
    int opcn;
    System.out.println("1.- Consumo anual de un piso");
    System.out.println("2.- Consumo anual de cada piso");
    System.out.println("3.- Mes de mayor consumo");
    opcn = leer.nextInt();
    switch(opcn){
    case 1: conspiso(consumo); break;


    }
    }

    public static int[][] llenar (int [][]consumo){

    for (int x=0; x < consumo.length; x++) {
    for (int y=0; y < consumo.length; y++)
    consumo[x][y] = (int)(Math.random()*20)+1;//Suponiendo que cada departamento 
    no consume más de 20m3 por mes
    }
    return consumo;
    }


     public static void conspiso (int consumo[][]){
        Scanner leer = new Scanner(System.in);
        System.out.println("Que piso desea buscar su consumo anual? 1-8");
        int piso = leer.nextInt();
        if(piso >8 || piso < 1){
        System.out.println("Piso no valido, ingrese nuevamente");
        conspiso(consumo);
        }
        int suma =0;
        switch(piso){

        case 1:  
            for(int x=0; x==11;x++){
            for(int y=0; y==3; y++){
            suma = suma+ consumo[x][y];
            }
            }
            System.out.println("El consumo anual en el piso 1 es "+suma+"m3");
            break;
        case 2: 
            for(int x=0; x==11;x++){
            for(int y=4; y==7; y++){
            suma = suma+ consumo[x][y];

            }
            }
            System.out.println("El consumo anual en el piso 2 es "+suma+"m3");     
            break;
        case 3: 
            for(int x=0; x==11;x++){
            for(int y=8; y==11; y++){
            suma = suma+ consumo[x][y];
            }
            }
            System.out.println("El consumo anual en el piso 3 es "+suma+"m3");
            break;
        case 4:
            for(int x=0; x==11;x++){
            for(int y=12; y==15; y++){
            suma = suma+ consumo[x][y];
            }
            }
            System.out.println("El consumo anual en el piso 4 es "+suma+"m3");
            break;
        case 5: 
            for(int x=0; x==11;x++){
            for(int y=16; y==19; y++){
            suma = suma+ consumo[x][y];
            }
            }
            System.out.println("El consumo anual en el piso 5 es "+suma+"m3");
            break;
        case 6: 
            for(int x=0; x==11;x++){
            for(int y=20; y==23; y++){
            suma = suma+ consumo[x][y];
            }
            }
            System.out.println("El consumo anual en el piso 6 es "+suma+"m3");
            break;
        case 7: 
            for(int x=0; x==11;x++){
            for(int y=24; y==27; y++){
            suma = suma+ consumo[x][y];
            }
            }
            System.out.println("El consumo anual en el piso 7 es "+suma+"m3");
            break;
        case 8:
            for(int x=0; x==11;x++){
            for(int y=28; y==31; y++){
            suma = suma+ consumo[x][y];
            }
            }
            System.out.println("El consumo anual en el piso 8 es "+suma+"m3");
            break;
}
}         
}
    
asked by Christian 21.03.2018 в 16:17
source

2 answers

2

You have an error completing the matrix, if x e y you use to go through the indexes of the first and second dimensions respectively, then y must go to consumo[x].length :

public static int[][] llenar (int [][]consumo){

    for (int x=0; x < consumo.length; x++) {
        for (int y=0; y < consumo[x].length; y++)
            consumo[x][y] = (int)(Math.random()*20)+1;//Suponiendo que cada departamento no consume más de 20m3 por mes
    }
    return consumo;
}
    
answered by 21.03.2018 в 16:30
2

You have many errors in your loops:

In the fill method:

public static int[][] llenar (int [][]consumo){

  for (int x=0; x < consumo.length; x++) {
    for (int y=0; y < consumo.length; y++) //aqui debería ser consumo[x].length
      consumo[x][y] = (int)(Math.random()*20)+1;
  }
  return consumo;
}

In each of your case :

 for(int x=0; x==11;x++){ //X no es 11, por lo que no se ejecuta el bucle
     for(int y=8; y==11; y++){ //Y no es 11, por lo que no se ejecuta el bucle

should be like this:

 for(int x=0; x<11;x++){
    for(int y=8; y<11; y++){
    
answered by 21.03.2018 в 16:35