-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
95 lines (67 loc) · 2.61 KB
/
Calculator.java
File metadata and controls
95 lines (67 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.exercises.easy.calculator;
import java.util.Scanner;
/**
* Crie um programa que seja capaz de realizar as seguintes operações matemáticas:
* adição (a), subtração (s), multiplicação (m), divisão (d), potenciação (p) e raiz quadrada (r).
* <p>
* O programa deve aguardar como entrada a letra correspondente à operação desejada, os valores necessários
* para a operação e, em seguida, printar o resultado na ultima linha seguindo o formato "Resultado: %.2f".
*/
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("a. adição");
System.out.println("s. subtração");
System.out.println("m. multiplicação");
System.out.println("d. divisão");
System.out.println("p. potenciação");
System.out.println("r. raiz quadradda");
String op = scanner.next();
if (op.equals("a")) {
System.out.print("a: ");
double a = scanner.nextDouble();
System.out.print("b: ");
double b = scanner.nextDouble();
System.out.println();
System.out.printf("Resultado: %.2f", a + b);
}
if (op.equals("s")) {
System.out.print("a: ");
double a = scanner.nextDouble();
System.out.print("b: ");
double b = scanner.nextDouble();
System.out.println();
System.out.printf("Resultado: %.2f", a - b);
}
if (op.equals("m")) {
System.out.print("a: ");
double a = scanner.nextDouble();
System.out.print("b: ");
double b = scanner.nextDouble();
System.out.println();
System.out.printf("Resultado: %.2f", a * b);
}
if (op.equals("d")) {
System.out.print("a: ");
double a = scanner.nextDouble();
System.out.print("b: ");
double b = scanner.nextDouble();
System.out.println();
System.out.printf("Resultado: %.2f", a / b);
}
if (op.equals("p")) {
System.out.print("a: ");
double a = scanner.nextDouble();
System.out.print("b: ");
double b = scanner.nextDouble();
System.out.println();
System.out.printf("Resultado: %.2f", Math.pow(a, b));
}
if (op.equals("r")) {
System.out.print("a: ");
double a = scanner.nextDouble();
System.out.println();
System.out.printf("Resultado: %.2f", Math.sqrt(a));
}
}
}