-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavaCalc.java
138 lines (125 loc) · 4.74 KB
/
JavaCalc.java
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import java.util.Scanner;
/**
* Java calculator.
*
* This application is a demonstration of some basic Java functionality.
*
* @author aliasdhacker
*
*/
public class JavaCalc {
static String xS = new String(), yS = new String(), operS = new String();
static Scanner s = new Scanner(System.in);
/**
* int values are initialized to 0 by default, (not complex types like
* Integer, they are initialized to null.) See this page for a reference for
* information on primitive value initial types. {@link http
* ://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html}
*/
static int oper, xI, yI;
public static void main(String[] args) {
System.out
.println("Simple Java 2 Operator Command Line Calculator!\n\tBy:Andrew Carr\n\nCTRL+C to quit");
while (!xS.equalsIgnoreCase("quit") || !yS.equalsIgnoreCase("quit")
|| !operS.equalsIgnoreCase("quit")) {
while (xS.equals("")) {
System.out.print("Enter Var 1? ");
xS = s.nextLine().trim();
}
while (yS.equals("")) {
System.out.print("Enter Var 2? ");
yS = s.nextLine().trim();
}
while (operS.equalsIgnoreCase("")) {
System.out.println("\nvar1=" + xS + " var2=" + yS + "\n"
+ "\tSelect an oper:" + "\n\t\t1 = +\n"
+ "\n\t\t2 = -\n" + "\n\t\t3 = /\n" + "\n\t\t4 = *\n"
+ "\n\t\t5 = %\n" + "\n\tChoice? ");
operS = s.nextLine();
}
if (operS.equalsIgnoreCase("quit"))
continue;
System.out.println("Parsing...");
try {
xI = Integer.valueOf(xS);
yI = Integer.valueOf(yS);
oper = Integer.valueOf(operS);
} catch (Exception e) {
System.out.println("Values entered are bad, try again..."
+ e.getMessage());
xS="";
yS="";
operS="";
continue;
}
switch (oper) {
case 1:
// addition
try {
System.out.println("\n\n" + xI + " + " + yI + " = "
+ String.valueOf(xI + yI));
System.out.println("Press enter to continue...");
s.nextLine();
} catch (Exception e) {
System.out.println("Error performing calculation -- "
+ e.getMessage());
}
break;
case 2:
// sub
try {
System.out.println("\n\n" + xI + " - " + yI + " = "
+ String.valueOf(xI - yI));
System.out.println("Press enter to continue...");
s.nextLine();
} catch (Exception e) {
System.out.println("Error performing calculation -- "
+ e.getMessage());
}
break;
case 3:
// div
try {
System.out.println("\n\n" + xI + " / " + yI + " = "
+ String.valueOf(xI / yI));
System.out.println("Press enter to continue...");
s.nextLine();
} catch (Exception e) {
System.out.println("Error performing calculation -- "
+ e.getMessage());
}
break;
case 4:
// mult
try {
System.out.println("\n\n" + xI + " * " + yI + " = "
+ String.valueOf(xI * yI));
System.out.println("Press enter to continue...");
s.nextLine();
} catch (Exception e) {
System.out.println("Error performing calculation -- "
+ e.getMessage());
}
break;
case 5:
// mod
try {
System.out.println("\n\n" + xI + " % " + yI + " = "
+ String.valueOf(xI % yI));
System.out.println("Press enter to continue...");
s.nextLine();
} catch (Exception e) {
System.out.println("Error performing calculation -- "
+ e.getMessage());
}
break;
default:
System.out.println("Invalid choice...");
}
operS = "";
xS = "";
yS = "";
}
s.close();
}
}