-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainLesson4_3.java
More file actions
66 lines (51 loc) · 1.83 KB
/
MainLesson4_3.java
File metadata and controls
66 lines (51 loc) · 1.83 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
package ru.MylearnCh1J1L1;
wimport java.util.LinkedList;
import java.util.Scanner;
public class MainLesson4_3 {
public static void main(String[] args) {
LinkedList<Double> results = new LinkedList<>();
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
results.add(sc.nextDouble());
sc.nextLine();
while(true) {
System.out.print("Enter operator (+, -, *, /, Cancel): ");
String op = sc.nextLine();
if ("".equals(op)) break;
else if ("Cancel".equals(op) || "cancel".equals(op)) {
results.removeFirst();
if (results.size() == 0) results.add(0.0);
}
else {
System.out.print("Enter second number: ");
double secNumber = sc.nextDouble();
process(results, op, secNumber);
sc.nextLine();
}
System.out.println("Result: " + results.getFirst());
}
sc.close();
}
public static void process(LinkedList<Double> list, String op, double number) {
switch (op) {
case "+":
list.add(0, list.getFirst() + number);
break;
case "-":
list.add(0, list.getFirst() - number);
break;
case "*":
list.add(0, list.getFirst() * number);
break;
case "/":
if (number == 0) {
System.out.println("Cannot divide by zero!");
break;
}
list.add(0, list.getFirst() / number);
break;
default:
System.out.println("Wrong operator!");
}
}
}