|
2 | 2 | import java.util.ArrayList;
|
3 | 3 | import java.util.List;
|
4 | 4 |
|
5 |
| -// Utilizado o OpenJDK 17.0.3 |
| 5 | +// Utilized OpenJDK 17.0.3 |
6 | 6 | /*
|
7 | 7 | * @author Gabriel Nascimento. GitHub: Gabrielxdf
|
8 | 8 | *
|
9 | 9 | */
|
10 |
| -public class SomaDoisNumeros { |
| 10 | +public class AddTwoNumbers { |
11 | 11 | /**
|
12 |
| - * Para cada elemento da lista de valores, o método percorre o restante da lista verificando se há |
13 |
| - * algum número cuja some de ambos seja o valor esperado. |
| 12 | + * This program takes a list of numbers and checks if there are two numbers whose sum is equal to |
| 13 | + * the expected value. |
| 14 | + * |
| 15 | + * For each element in the list of values, this method goes through the list, |
| 16 | + * checking if there is any number whose sum of both is the expected value. |
14 | 17 | *
|
15 |
| - * @param valores Lista de valores que será verificada. |
16 |
| - * @param esperado Valor esperado pela soma de dois números da lista valores. |
| 18 | + * @param values List of values that will be checked. |
| 19 | + * @param expected Expected value from the sum of two numbers from the values list. |
17 | 20 | */
|
18 |
| - public static void verificaSeExisteSoma(List<Integer> valores, int esperado) { |
19 |
| - boolean resultado = false; |
20 |
| - for (int i = 0; i < valores.size() - 1; i++) { |
21 |
| - for (int j = i + 1; j < valores.size(); j++) { |
22 |
| - if (valores.get(i) + valores.get(j) == esperado) { |
23 |
| - resultado = true; |
| 21 | + public static void checkIfThereSumExists(List<Integer> values, int expected) { |
| 22 | + boolean result = false; |
| 23 | + for (int i = 0; i < values.size() - 1; i++) { |
| 24 | + for (int j = i + 1; j < values.size(); j++) { |
| 25 | + if (values.get(i) + values.get(j) == expected) { |
| 26 | + result = true; |
24 | 27 | System.out.println(
|
25 | 28 | MessageFormat.format(
|
26 |
| - "Verdadeiro - {0} + {1} = {2}", valores.get(i), valores.get(j), esperado)); |
| 29 | + "True - {0} + {1} = {2}", values.get(i), values.get(j), expected)); |
27 | 30 | break;
|
28 | 31 | }
|
29 | 32 | }
|
30 |
| - if (resultado) { |
| 33 | + if (result) { |
31 | 34 | break;
|
32 | 35 | }
|
33 | 36 | }
|
34 |
| - if (!resultado) { |
| 37 | + if (!result) { |
35 | 38 | System.out.println(
|
36 |
| - MessageFormat.format("Falso - Não há dois números cuja soma seja {0}", esperado)); |
| 39 | + MessageFormat.format("False - There are no two numbers whose sum is {0}", expected)); |
37 | 40 | }
|
38 | 41 | }
|
39 | 42 |
|
40 | 43 | public static void main(String[] args) {
|
41 |
| - List<Integer> valores = new ArrayList<Integer>(List.of(12, 33, 5, 9, 54, 100)); |
42 |
| - System.out.println("------Deve ser verdadeiro------"); |
43 |
| - verificaSeExisteSoma(valores, 133); |
44 |
| - verificaSeExisteSoma(valores, 42); |
45 |
| - verificaSeExisteSoma(valores, 66); |
46 |
| - System.out.println("------Deve ser falso------"); |
47 |
| - verificaSeExisteSoma(valores, 13); |
48 |
| - verificaSeExisteSoma(valores, 100); |
49 |
| - verificaSeExisteSoma(valores, 60); |
| 44 | + List<Integer> values = new ArrayList<Integer>(List.of(12, 33, 5, 9, 54, 100)); |
| 45 | + System.out.println("------It must be true------"); |
| 46 | + checkIfThereSumExists(values, 133); |
| 47 | + checkIfThereSumExists(values, 42); |
| 48 | + checkIfThereSumExists(values, 66); |
| 49 | + System.out.println("------It must be false------"); |
| 50 | + checkIfThereSumExists(values, 13); |
| 51 | + checkIfThereSumExists(values, 100); |
| 52 | + checkIfThereSumExists(values, 60); |
50 | 53 | }
|
51 | 54 | }
|
0 commit comments