Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. package ucsal.br;
  2. //Escreva um programa para ler um array A de 5 elementos, e um array B de 8
  3. //elementos. Crie um array C intercalando A e B. Imprima todos os arrays.
  4. public class Questao18 {
  5.  
  6. public static void main(String[] args) {
  7. gerarvetor_A();
  8. }
  9.  
  10. public static int[] gerarvetor_A() {
  11. int[] vet_A = new int[5];
  12. int acum = 2;
  13. for (int i = 0; i < vet_A.length; i++) {
  14. vet_A[i] = acum;
  15. acum += 2;
  16. }
  17. gerarvetor_B(vet_A);
  18. return vet_A;
  19. }
  20.  
  21. public static int[] gerarvetor_B(int[] vet_A) {
  22. int[] vet_B = new int[8];
  23. int acum = 1;
  24. for (int i = 0; i < vet_B.length; i++) {
  25. vet_B[i] = acum;
  26. acum += 3;
  27. }
  28. gerarvetor_C(vet_A,vet_B);
  29. return vet_B;
  30. }
  31.  
  32. public static void gerarvetor_C(int[] vet_A, int[] vet_B) {
  33. int[] vet_C = new int[13];
  34. int num = 0;
  35. int cont_B = 0;
  36. int cont_A = 0;
  37. while(num < vet_C.length) {
  38. if(num < 8 && cont_B < 8) {
  39. vet_C[num] = vet_B[cont_B];
  40. cont_B++;
  41. num++;
  42. } else {
  43. vet_C[num] = vet_A[cont_A];
  44. num++;
  45. cont_A++;
  46. }
  47. }
  48. for (int i = 0; i < vet_C.length; i++) {
  49. System.out.println(vet_C[i] + " ");
  50. }
  51. }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement