Arrays

3 respostas
L

tenho um vector com os seguintes dados como exemplo:

porto 1 2 3 4 5 6 7 8
viseu 7 8 9 7 4 3 9 6
evora 1 2 3 4 5 6 2 3
preciso de somar os valor da coluna 4 ate a coluna 8 percorrendo as linhas.e preciso de guardar os valore somados num vector para determinar o maior. tenho o seguinte codigo:

maior=vecAux[0];

for(y=3;y<8;y++){

soma=0;

for(x=0;x<k++){  // k é o numero de linhas da matriz

soma=soma+mat[x][y];

vecAux[x]=soma;

if(vecAux[1]>maior){

maior=vecAux[1];

}

}
}  
                System.out.println("o partido que ganhou teve um total de:"+maior);

o que acontece é que o programa soma-me sempre so as duas primeiras linhas.agradeço kem m possa ajudar

3 Respostas

juniorsatanas

posta toda essa classe para eu ver aqui !

L

public class Main {

public static void main(String[] args) {
    Scanner entrada=new Scanner(System.in);
    int menu,input,i,j,soma,soma1,max,max1,maior,maior1,pos;
    String s;
    String texto[]=new String[18];
    int mat[][]=new int[18][10];
    int vecAux[]=new int[10];
    int vec1[]=new int[10];
    int linhas=0,x,y,z,colunas,l;
    int k=0;
    String linha;
    do
    {
        System.out.println("\n1-carregar distrito\n2- ver dados\n3-distrito com mais votos\n4-numero de votos do partido vencedor\n5-partido que ganhou em cada distrito\n0-exit");
        System.out.println("introduza a opçao k deseja");
        menu=entrada.nextInt();
        switch (menu) {
            case 1:
                JFileChooser fich=new JFileChooser();
                input=fich.showOpenDialog(null);
                if(input==JFileChooser.APPROVE_OPTION){
                    try {
                        Scanner ler=new Scanner(fich.getSelectedFile());                            
                        while(ler.hasNext()){
                            s=ler.nextLine();
                            System.out.print(s);
                            String vec[]=s.split("\\|");
                            for(i=0;i<vec.length;i++){
                                if(i==0){
                                    texto[k]=vec[i];
                                } else{
                                    mat[k][i-1]=Integer.parseInt(vec[i]);
                                }
                            }
                            k++;
                          
                        }
                    } catch (FileNotFoundException ex) {
                        ex.printStackTrace();
                    }
                    
                } else{
                    System.out.println("nao escolheu ficheiro");
                }
                break;
                
            case 2:
              
                
                for(x=0;x<k;x++) {
                    linha=texto[x]+"\t";
                      soma=0;
                    for(y=0;y<=7;y++){
                        linha+=mat[x][y]+"\t";
                         soma=soma+mat[x][y];
                     }
                    
                    System.out.print("\n"+linha+" "+soma);
                
                }
               break;

            case 3:
            maior=mat[0][9];
                for(x=0;x<k;x++){
                if(mat[x][9]>maior){
                    maior=mat[x][9];
                }
              }
            System.out.println(" o distrito com maior numero de votos foi:"+texto[maior]);
            break;
              
        
            case 4:
               maior=vecAux[0];
                for(y=3;y<8;y++){
                    soma=0;
                      for(x=0;x<=(k+1);x++){
                        soma=soma+mat[x][y];
                        vecAux[x]=soma;
                            if(vecAux[1]>maior){
                            maior=vecAux[1];
                        }
                    }
                
                }  
                System.out.println("o partido que ganhou teve um total de:"+maior);
              break;
            case 5:
            
                maior=vec1[0];  
                for(x=0;x<k;x++){
               soma=0;
                    for(y=1;y<3;y++){
                        soma=soma +mat[x][y];
                    
                    vec1[x]=soma;
                   
                    for(x=1;x<=2;x++){
                        if(vec1[x]>maior){
                            maior=vec1[x];
                        }
                    }
               
                }
                 //System.out.print(" "+soma);
                }
               System.out.println(texto[maior]);
            
            
            
            
            case 0:
                try {
                     Formatter saida=new Formatter(new File("distritos.txt"));
                     
                     for(x=0;x<k;x++){
                   linha=texto[x]+"\t";
                   for(y=0;y<8;y++){
                       linha+=mat[x][y]+"\t";
                    }
               saida.format("%n%s",linha);
                     }
                 saida.close();
                } catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                }
                
                break;
             
        }
    } while(menu!=0);
    
    
}

}

francislon

http://www.guj.com.br/posts/list/50115.java

Criado 24 de junho de 2007
Ultima resposta 24 de jun. de 2007
Respostas 3
Participantes 3