-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh9Ex6.java
More file actions
71 lines (62 loc) · 1.6 KB
/
Ch9Ex6.java
File metadata and controls
71 lines (62 loc) · 1.6 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
import java.util.*;
public class Ch9Ex6
{
public static double operation(double uinput, String uAct) throws UnknownOperatorException
{
String oper = uAct.substring(0, 1);
if(!oper.equals("+") && !oper.equals("-") && !oper.equals("*") && !oper.equals("/") && !oper.equals("R") && !oper.equals("r"))
throw new UnknownOperatorException();
else if(oper.equals("R") || oper.equals("r"))
return uinput;
double num = Double.parseDouble(uAct.substring(1, uAct.length()));
System.out.print("result "+oper+num+" = ");
if(oper.equals("+"))
return uinput += num;
else if(oper.equals("-"))
return uinput -= num;
else if(oper.equals("*"))
return uinput *= num;
else
return uinput /= num;
}
public static void main(String args[])
{
Scanner input = new Scanner(System.in);
System.out.println("Calculator is on.");
while(true)
{
int loops=0;
double result = 0;
String oper = "";
System.out.println("result: " + result);
while(!oper.equals("R") && !oper.equals("r"))
{
try
{
oper = input.nextLine();
result = operation(result, oper);
loops++;
if(!oper.equals("R") && !oper.equals("r"))
{
System.out.println(result);
System.out.println("Result: "+ result);
}
}
catch(UnknownOperatorException e)
{
System.out.println("Try Again");
}
}
System.out.println("Result: " + result);
System.out.println("Again Y/N");
String end = input.nextLine();
if (end.equals("y") || end.equals("y"))
continue;
else
{
System.out.println("End of Program.");
break;
}
}
}
}