Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Ficha5;
- /**
- * Breve descrição do código
- *
- * @sid 2005
- * @aid 5.13
- */
- public class Ficha5_ex13 {
- public static void main(String[] args) {
- // begin_inputs
- int num = 3495; // a solicitar ao utilizador, de 4 digitos
- // end_inputs
- // Determinar os algarismos todos (depois vai ao método algarismoMaximo para perceber
- // qual é o maior) para ser usado no ciclo for.
- int unid1 = 0, unid2 = 0, unid3 = 0, unid4 = 0;
- unid4 = num % 10;
- unid3 = (num % 100 - unid4) / 10;
- unid2 = (num % 1000 - unid3) / 100;
- unid1 = num / 1000;
- // ns = nº de espaços
- int ns = 0;
- for (int i = 1; i <= algarismoMaximo(unid1, unid2, unid3, unid4); i++) { // vai de 1 a 9
- for (int j = 1; j <= 4; j++) {
- // nº de espaços para cada coluna
- if (j == 1)
- ns = algarismoMaximo(unid1, unid2, unid3, unid4) - unid1; // 9-3=6
- if (j == 2)
- ns = algarismoMaximo(unid1, unid2, unid3, unid4) - unid2; // =5
- if (j == 3)
- ns = algarismoMaximo(unid1, unid2, unid3, unid4) - unid3; // =0
- if (j == 4)
- ns = algarismoMaximo(unid1, unid2, unid3, unid4) - unid4; // =4
- // impressao em cd linha
- if (i <= ns) {
- System.out.print(" ");
- } else if (i > ns) {
- System.out.print("*");
- }
- }
- System.out.println();
- }
- }
- /**
- *
- * @param x
- * @return o algarismo maximo dos 4 digitos fornecidos.
- */
- private static int algarismoMaximo(int x1, int x2, int x3, int x4) {
- int max1 = Math.max(x1, x2);
- int max2 = Math.max(x3, x4);
- int maximo = Math.max(max1, max2);
- return maximo;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement