Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.29 KB | None | 0 0
  1. package questão1_8;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Questão1_8 {
  6.  
  7.      public static void main(String[] args) {
  8.    
  9.         Scanner dado = new Scanner (System.in);
  10.         int qtdAlunos;
  11.        
  12.         do{
  13.            
  14.             System.out.print("Digite a quantidade de alunos da turma: ");
  15.             qtdAlunos = dado.nextInt();
  16.            
  17.         }while(qtdAlunos <= 0);
  18.        
  19.         String nomes [] = new String [qtdAlunos];
  20.         double notas [][] = new double[qtdAlunos][3];
  21.         preencherDados(nomes,notas);
  22.         ordenarLista(nomes,notas);
  23.         exibir(nomes,notas);
  24.         buscarAluno(nomes,notas);
  25.  
  26.     }
  27.     public static void preencherDados(String nomes[], double notas[][]){
  28.        
  29.         Scanner dado = new Scanner(System.in);
  30.         int i,j;
  31.        
  32.         for(i=0; i < nomes.length; i++){
  33.            
  34.             System.out.print("Digite o nome do aluno " + (i+1) + ": ");
  35.             nomes[i] = dado.nextLine();
  36.            
  37.             for(j=0;j < notas[i].length;j++){
  38.                    
  39.                 if( j < notas[0].length -1){
  40.                     System.out.print("Digite a nota do(a) " + nomes[i] + " da prova " + (j+1) + ": ");
  41.                     notas[i][j] = dado.nextDouble();
  42.                 }else if( j == notas[0].length-1) {
  43.                        
  44.                      notas[i][j] = (notas[i][0] + notas[i][1]) /2;
  45.                        
  46.                 }      
  47.             }
  48.             dado.nextLine();
  49.         }
  50.     }
  51.     public static void exibir(String nomes[],double notas[][]){
  52.        
  53.         int i,j,w;
  54.        
  55.         for(i=0; i < nomes.length; i++){
  56.            
  57.             System.out.print(nomes[i] + " ");
  58.              
  59.             for(j=0; j < notas[0].length; j++){
  60.                    
  61.                 System.out.print(notas[i][j] + " ");
  62.            }
  63.            
  64.             System.out.println();
  65.         }
  66.     }
  67.     public static void ordenarLista(String nomes[],double notas[][]){
  68.        
  69.         int i,j,w;
  70.         String ponte;
  71.         double ponte2 [] = new double[3];
  72.        
  73.         int fim = nomes.length - 2;
  74.         int pos = 0;
  75.         boolean troca = true;
  76.      
  77.        
  78.         while (troca == true) {
  79.            
  80.             troca = false;
  81.            
  82.             for (i = 0; i <= fim; i++) {
  83.            
  84.                 if (nomes[i].compareToIgnoreCase(nomes[i+1]) > 0  ) {
  85.                    
  86.                     ponte = nomes[i];
  87.                     nomes[i] = nomes[i+1];
  88.                     nomes[i+1] = ponte;
  89.                     pos = i;
  90.                     troca = true;
  91.                    
  92.                                            
  93.                     for(j=0; j < notas[0].length;j++){
  94.                            
  95.                         ponte2[j] = notas[i][j];
  96.                         notas[i][j] = notas[i+1][j];
  97.                         notas[i+1][j] = ponte2[j];
  98.                     }
  99.                 }
  100.             }
  101.             fim = pos-1;
  102.        
  103.         }
  104.     }
  105.     public static void buscarAluno(String nomes[],double notas[][]){
  106.        
  107.         int inicio = 0;
  108.         int meio = 0;
  109.         int fim = nomes.length -1;
  110.         int achou = 0;
  111.         int i;
  112.         int resposta=0;
  113.         int compare = 0;
  114.         Scanner dado = new Scanner(System.in);
  115.        
  116.         String name;
  117.        
  118.                
  119.         while(resposta!=2){
  120.            
  121.             do{
  122.                 System.out.print("1-Se quiser pesquisar algum nome\n2-Se quiser parar\n");
  123.                 System.out.print("Digite sua resposta: ");
  124.                 resposta = dado.nextInt();
  125.            
  126.             }while(resposta < 1 && resposta > 2);
  127.            
  128.             if(resposta !=2){    
  129.                
  130.                 dado.nextLine();
  131.                 System.out.print("Digite o nome que voce queira pesquisar: ");
  132.                 name = dado.nextLine();
  133.  
  134.                 do {
  135.                     meio = (inicio + fim) / 2;
  136.                     compare = name.compareToIgnoreCase(nomes[meio]);
  137.  
  138.                     if (compare == 0) {
  139.  
  140.                         achou = 1;
  141.                         break;
  142.                     }
  143.                     else if (compare > 0) {
  144.  
  145.                         fim = meio -1;
  146.                     }
  147.                     else {
  148.                         inicio = meio + 1;
  149.                     }
  150.  
  151.                 } while (inicio <= fim);
  152.  
  153.                 if (achou == 1){
  154.  
  155.                     System.out.println("Notas do aluno(a) " + name);
  156.                     for(i=0; i < notas[0].length;i++){
  157.  
  158.                         if(i<=1){
  159.  
  160.                             System.out.println("Nota da prova " + (i+1) + " " + notas[meio][i]);
  161.  
  162.                         }else{
  163.                             System.out.print("Media das duas provas " + notas[meio][i] );
  164.                         }
  165.                     }
  166.                         meio = 0;
  167.                         fim = nomes.length -1;
  168.                         System.out.println();
  169.                
  170.                 }else{
  171.                     System.out.println("Não existem ninguem com esse nome.");
  172.                     meio = 0;
  173.                     fim = nomes.length -1;
  174.                 }
  175.             }    
  176.         }    
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement