Advertisement
Inesqs

Untitled

Oct 17th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.60 KB | None | 0 0
  1. package Ficha5;
  2.  
  3. /**
  4.  * Breve descrição do código
  5.  *
  6.  * @sid 2005
  7.  * @aid 5.13
  8.  */
  9.  
  10. public class Ficha5_ex13 {
  11.  
  12.     public static void main(String[] args) {
  13.  
  14.         // begin_inputs
  15.         int num = 3495; // a solicitar ao utilizador, de 4 digitos
  16.         // end_inputs
  17.  
  18.         // Determinar os algarismos todos (depois vai ao método algarismoMaximo para perceber
  19.         // qual é o maior) para ser usado no ciclo for.
  20.         int unid1 = 0, unid2 = 0, unid3 = 0, unid4 = 0;
  21.         unid4 = num % 10;
  22.         unid3 = (num % 100 - unid4) / 10;
  23.         unid2 = (num % 1000 - unid3) / 100;
  24.         unid1 = num / 1000;
  25.  
  26.         // ns = nº de espaços
  27.         int ns = 0;
  28.  
  29.         for (int i = 1; i <= algarismoMaximo(unid1, unid2, unid3, unid4); i++) { // vai de 1 a 9
  30.  
  31.             for (int j = 1; j <= 4; j++) {
  32.                 // nº de espaços para cada coluna
  33.                 if (j == 1)
  34.                     ns = algarismoMaximo(unid1, unid2, unid3, unid4) - unid1; // 9-3=6
  35.                 if (j == 2)
  36.                     ns = algarismoMaximo(unid1, unid2, unid3, unid4) - unid2; // =5
  37.                 if (j == 3)
  38.                     ns = algarismoMaximo(unid1, unid2, unid3, unid4) - unid3; // =0
  39.                 if (j == 4)
  40.                     ns = algarismoMaximo(unid1, unid2, unid3, unid4) - unid4; // =4
  41.  
  42.                 // impressao em cd linha
  43.                 if (i <= ns) {
  44.                     System.out.print(" ");
  45.                 } else if (i > ns) {
  46.                     System.out.print("*");
  47.                 }
  48.  
  49.             }
  50.             System.out.println();
  51.         }
  52.     }
  53.  
  54.     /**
  55.      *
  56.      * @param x
  57.      * @return o algarismo maximo dos 4 digitos fornecidos.
  58.      */
  59.     private static int algarismoMaximo(int x1, int x2, int x3, int x4) {
  60.  
  61.         int max1 = Math.max(x1, x2);
  62.         int max2 = Math.max(x3, x4);
  63.         int maximo = Math.max(max1, max2);
  64.  
  65.         return maximo;
  66.  
  67.     }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement