|
| 1 | +package ListaAtividadeFormativa; |
| 2 | +import java.util.Random; |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +public class Exercicios { |
| 6 | + Scanner sc = new Scanner(System.in); |
| 7 | + Random rd = new Random(); |
| 8 | + |
| 9 | + public void exercicio1() { |
| 10 | + int matriz[][]; |
| 11 | + int linha=0; |
| 12 | + int coluna=0; |
| 13 | + |
| 14 | + System.out.println("Informe a quantidade de linhas da matriz"); |
| 15 | + linha = sc.nextInt(); |
| 16 | + System.out.println("Informe a quantidade de colunas da matriz"); |
| 17 | + coluna = sc.nextInt(); |
| 18 | + |
| 19 | + matriz = new int[linha][coluna]; |
| 20 | + |
| 21 | + for (int i = 0; i < matriz.length; i++) { |
| 22 | + for (int j = 0; j < matriz.length; j++) { |
| 23 | + matriz[i][j] = rd.nextInt(9); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + System.out.println("Matriz sorteada: "); |
| 29 | + |
| 30 | + for (int i = 0; i < matriz.length; i++) { |
| 31 | + System.out.print("| "); |
| 32 | + for (int j = 0; j < matriz.length; j++) { |
| 33 | + System.out.print(matriz[i][j] + " "); |
| 34 | + } |
| 35 | + System.out.println("|"); |
| 36 | + } |
| 37 | + |
| 38 | + for (int i = 0; i < matriz.length; i++) { |
| 39 | + for (int j = 0; j < matriz.length; j++) { |
| 40 | + if(i > j) { |
| 41 | + matriz[i][j] = 1; |
| 42 | + } |
| 43 | + else if(i == j) { |
| 44 | + matriz[i][j]=0; |
| 45 | + } |
| 46 | + else if(j > i) { |
| 47 | + matriz[i][j] = 2; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + System.out.println("==========================================================="); |
| 53 | + System.out.println("Matriz com as condições:"); |
| 54 | + for (int i = 0; i < matriz.length; i++) { |
| 55 | + System.out.print("| "); |
| 56 | + for (int j = 0; j < matriz.length; j++) { |
| 57 | + System.out.print(matriz[i][j] + " "); |
| 58 | + } |
| 59 | + System.out.println("|"); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public void exercicio2() { |
| 64 | + int cont=1; |
| 65 | + int nSorteado; |
| 66 | + int palpite = -1; |
| 67 | + |
| 68 | + nSorteado = rd.nextInt(1000); |
| 69 | + |
| 70 | + |
| 71 | + while(palpite != nSorteado) { |
| 72 | + System.out.println("Informe o seu palpite de n° sorteado: "); |
| 73 | + palpite = sc.nextInt(); |
| 74 | + cont++; |
| 75 | + if(palpite > nSorteado) { |
| 76 | + System.out.println("O n° que você digitou é diferente do sorteado e maior do que o sorteado. Tente novamente."); |
| 77 | + } |
| 78 | + else if(palpite < nSorteado) { |
| 79 | + System.out.println("O n° que você digitou é diferente do sorteado e menor do que o sorteado. Tente novamente."); |
| 80 | + } |
| 81 | + else if(palpite == nSorteado) { |
| 82 | + System.out.println("Parabéns! Você acertou o n° sorteado."); |
| 83 | + System.out.println("O n° de tentativas até o acerto foi de: " + cont); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + public void exercicio3() { |
| 89 | + int vetor[]; |
| 90 | + int tamanhoVetor = rd.nextInt(901) + 100; |
| 91 | + vetor = new int[tamanhoVetor]; |
| 92 | + int contadorPares=0; |
| 93 | + int contadorImpares=0; |
| 94 | + |
| 95 | + for (int i = 0; i < vetor.length; i++) { |
| 96 | + vetor[i] = rd.nextInt(100); |
| 97 | + } |
| 98 | + |
| 99 | + System.out.println("Números pares: "); |
| 100 | + |
| 101 | + for (int i = 0; i< vetor.length; i++) { |
| 102 | + if(vetor[i] % 2 == 0) { |
| 103 | + System.out.print(vetor[i] + "; "); |
| 104 | + } |
| 105 | + } |
| 106 | + System.out.println("==================================="); |
| 107 | + |
| 108 | + System.out.println("N°s ímpares: "); |
| 109 | + for (int i = 0; i < vetor.length; i++) { |
| 110 | + if(vetor[i] % 2 != 0) { |
| 111 | + System.out.print(vetor[i] + "; "); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + for (int i = 0; i < vetor.length; i++) { |
| 116 | + if(vetor[i] % 2 == 0 & i % 2 != 0){ |
| 117 | + contadorPares++; |
| 118 | + } |
| 119 | + else if(vetor[i] % 2 != 0 & i % 2 == 0 ){ |
| 120 | + contadorImpares++; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + System.out.println("==================================================================================="); |
| 125 | + |
| 126 | + System.out.println("N°s pares na posição ímpar: " + contadorPares); |
| 127 | + System.out.println("N°s ímpares na posição par: " + contadorImpares); |
| 128 | + } |
| 129 | +} |
0 commit comments