|
| 1 | +import java.util.List; |
| 2 | +import java.util.stream.Collectors; |
| 3 | +import java.util.stream.IntStream; |
| 4 | + |
| 5 | +public class JuanGuzmanG { |
| 6 | + |
| 7 | + public static void main(String args[]) { |
| 8 | + //arithmetic |
| 9 | + int additive = 1 + 1; |
| 10 | + int subtraction = 1 - 1; |
| 11 | + int multiplication = 1 * 1; |
| 12 | + float division = 1 / 2; |
| 13 | + float remainder = 1 / 2; |
| 14 | + |
| 15 | + /* |
| 16 | + Unary |
| 17 | + + indica valores positivos |
| 18 | + - indica negativos |
| 19 | + ++ incremente de 1 en 1 |
| 20 | + -- decrementa de 1 en 1 |
| 21 | +
|
| 22 | + */ |
| 23 | + |
| 24 | + //Operadores logicos |
| 25 | + /* |
| 26 | + || or |
| 27 | + && and |
| 28 | + ^ xor |
| 29 | + */ |
| 30 | + |
| 31 | + //operadores de comparación |
| 32 | + /* |
| 33 | + == igual a |
| 34 | + != distinto |
| 35 | + > mayor que |
| 36 | + < menos que |
| 37 | + => igual o mayor que |
| 38 | + <= igual o menor que |
| 39 | + */ |
| 40 | + |
| 41 | + /* |
| 42 | + = asignacion |
| 43 | + += suma el valor asignado a la variable |
| 44 | + -= resta el valor asignado a la variable |
| 45 | + int b = 9; |
| 46 | + b<<=3; |
| 47 | + System.out.print(b); |
| 48 | + */ |
| 49 | + //condicionales |
| 50 | + int condicion = 1; |
| 51 | + if(condicion==1){ |
| 52 | + System.out.print(1); |
| 53 | + } else if(condicion==2){ |
| 54 | + System.out.print(2); |
| 55 | + } else{ |
| 56 | + System.out.print("mas de 2"); |
| 57 | + } |
| 58 | + switch (condicion){ |
| 59 | + case 1 -> System.out.print(1); |
| 60 | + case 2 -> System.out.print(2); |
| 61 | + } |
| 62 | + switch (condicion){ |
| 63 | + case 1 -> { |
| 64 | + System.out.println(1); |
| 65 | + System.out.println("fin"); |
| 66 | + } |
| 67 | + } |
| 68 | + //iterativas |
| 69 | + System.out.println("while:"); |
| 70 | + int count = 2; |
| 71 | + while(count<3){ |
| 72 | + count+=1; |
| 73 | + System.out.println("while: "+ count); |
| 74 | + } |
| 75 | + System.out.println("==============="); |
| 76 | + int countdo = 6; |
| 77 | + do{ |
| 78 | + System.out.println("dowhile: "+ countdo); |
| 79 | + countdo++; |
| 80 | + }while(countdo<6); |
| 81 | + |
| 82 | + //for |
| 83 | + System.out.println("for:"); |
| 84 | + int i; |
| 85 | + for(i=0; i<2;i++){ |
| 86 | + System.out.println(i); |
| 87 | + } |
| 88 | + System.out.println("foreach:"); |
| 89 | + int[] numbers = {1,2,3,4,5}; |
| 90 | + for(int num : numbers){ |
| 91 | + System.out.println(num); |
| 92 | + if(num == 3){ |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + System.out.println("try catch:"); |
| 98 | + double trydivision; |
| 99 | + try{ |
| 100 | + trydivision = 20/0; |
| 101 | + System.out.println(trydivision); |
| 102 | + } catch(ArithmeticException e){ |
| 103 | + System.out.println("no se puede dividir por 0"); |
| 104 | + } finally{ |
| 105 | + System.out.println("fin de division"); |
| 106 | + } |
| 107 | + |
| 108 | + System.out.println("dificultad extra"); |
| 109 | + List<Integer> lista = IntStream.rangeClosed(10, 55) |
| 110 | + .boxed() |
| 111 | + .collect(Collectors.toList()); |
| 112 | + for(Integer num : lista){ |
| 113 | + if (num % 2 == 0 && num != 16 && num % 3 != 0) { |
| 114 | + System.out.println(num); |
| 115 | + } |
| 116 | + } |
| 117 | + System.out.println("con streams"); |
| 118 | + IntStream.rangeClosed(10,55) |
| 119 | + .filter(n -> n % 2 == 0) |
| 120 | + .filter(n -> n % 3 != 0) |
| 121 | + .filter(n -> n != 16) |
| 122 | + .forEach(System.out::println); |
| 123 | + |
| 124 | + } |
| 125 | +} |
0 commit comments