Advertisement
LightProgrammer000

Notas_alunos em matriz

May 4th, 2020
2,215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. package PDEITEL_009;
  2.  
  3. import javax.swing.JOptionPane;
  4.  
  5. public class Exemplo_Notas
  6. {
  7.     public static void main(String[] args)
  8.     {
  9.         String str;
  10.         double n;
  11.         int nr_alunos = 0, nr_notas = 0;;
  12.  
  13.         while (nr_alunos <= 0)
  14.         {
  15.             str = JOptionPane.showInputDialog(null, "Numero de alunos").trim();
  16.            
  17.             if (str == null)
  18.             {
  19.                 System.exit(0);
  20.             }
  21.            
  22.             nr_alunos = Integer.parseInt(str);
  23.         }
  24.  
  25.        
  26.         while (nr_notas <= 0)
  27.         {
  28.             str = JOptionPane.showInputDialog(null, "Número de notas");
  29.            
  30.             if (str == null)
  31.             {
  32.                 System.exit(0);
  33.             }
  34.            
  35.             nr_notas = Integer.parseInt(str);
  36.         }
  37.  
  38.         double[][] notas = new double[nr_alunos][nr_notas];
  39.  
  40.         for (int lin = 0; lin < notas.length; lin++)
  41.         {
  42.             for (int col = 0; col < notas[lin].length; col++)
  43.             {
  44.                 notas[lin][col] = 1;
  45.             }
  46.         }
  47.  
  48.         str = "";
  49.         for (int lin = 0; lin < notas.length; lin++)
  50.         {
  51.             for (int col = 0; col < notas[lin].length; col++)
  52.             {
  53.                 while (true)
  54.                 {
  55.                     str = JOptionPane.showInputDialog(null, "Aluno " + (lin + 1) + ": nota" + (col + 1));
  56.                    
  57.                     if (str == null)
  58.                     {
  59.                         break;
  60.                     }
  61.                    
  62.                     n = Double.parseDouble(str);
  63.                     if (n >= 0 && n <= 10)
  64.                     {
  65.                         notas[lin][col] = n;
  66.                         break;
  67.  
  68.                     }
  69.  
  70.                 }
  71.  
  72.                 if (str == null)
  73.                 {
  74.                     break;
  75.                 }
  76.             }
  77.  
  78.             if (str == null)
  79.             {
  80.                 break;
  81.             }
  82.         }
  83.        
  84.         str = "Quadro de notas:";
  85.         for (int lin = 0; lin < notas.length; lin++)
  86.         {
  87.             if (notas[lin][nr_notas - 1] == -1)
  88.             {
  89.                 break;
  90.             }
  91.            
  92.             double soma = 0;
  93.            
  94.             str += "\nAluno" + (lin + 1) + ":    ";
  95.            
  96.             for (int col = 0; col < notas[lin].length; col++)
  97.             {
  98.                 soma += notas[lin][col];
  99.                 str += notas[lin][col] + " ";
  100.             }
  101.  
  102.             str += "  =   " + (soma / nr_notas);
  103.         }
  104.        
  105.         JOptionPane.showMessageDialog(null, str);
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement