Skip to content

Commit 36dadbd

Browse files
committed
Exercício da soma de dois números em java
1 parent ae2915a commit 36dadbd

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
1.59 KB
Binary file not shown.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package soma_dois_numeros;
2+
3+
import java.text.MessageFormat;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
//Utilizado o OpenJDK 17.0.3
8+
public class SomaDoisNumeros {
9+
/*
10+
Para cada elemento da lista de valores, o método percorre o restante da lista
11+
verificando se há algum número cuja some de ambos seja o valor esperado.
12+
*/
13+
public static void verificaSeExisteSoma(List<Integer> valores, int esperado) {
14+
boolean resultado = false;
15+
for (int i = 0; i < valores.size() - 1; i++) {
16+
for (int j = i + 1; j < valores.size(); j++) {
17+
if (valores.get(i) + valores.get(j) == esperado) {
18+
resultado = true;
19+
System.out.println(MessageFormat.format("Verdadeiro - {0} + {1} = {2}", valores.get(i),
20+
valores.get(j), esperado));
21+
break;
22+
}
23+
}
24+
if (resultado) {
25+
break;
26+
}
27+
}
28+
if (!resultado) {
29+
System.out.println(MessageFormat.format("Falso - Não há dois números cuja soma seja {0}", esperado));
30+
}
31+
}
32+
33+
public static void main(String[] args) {
34+
List<Integer> valores = new ArrayList<Integer>(List.of(12, 33, 5, 9, 54, 100));
35+
System.out.println("------Deve ser verdadeiro------");
36+
verificaSeExisteSoma(valores, 133);
37+
verificaSeExisteSoma(valores, 42);
38+
verificaSeExisteSoma(valores, 66);
39+
System.out.println("------Deve ser falso------");
40+
verificaSeExisteSoma(valores, 13);
41+
verificaSeExisteSoma(valores, 100);
42+
verificaSeExisteSoma(valores, 60);
43+
}
44+
}

0 commit comments

Comments
 (0)