-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart2.java
More file actions
66 lines (62 loc) · 2.29 KB
/
Part2.java
File metadata and controls
66 lines (62 loc) · 2.29 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
/*
* Name: Adam Mason
* Date: 2/12/2019
* Project Description: Allow the user to enter one operand then and operator then another operand to get a result.
*/
import java.util.Scanner;
public class Part2
{
public static void main()
{
while( true )
{
Scanner scan = new Scanner( System.in );
char operator = ' ', progEnd = ' ';
String stringInput = " ";
//Get first operand
System.out.print("Enter a value for the left operand: ");
int left_op = scan.nextInt();
scan.nextLine();
//Get operator ( +, -, *, / )
System.out.print("Enter an operator: ");
operator = scan.nextLine().toLowerCase().charAt(0);
//Get second operand
System.out.print("Enter a value for the right operand: ");
int right_op = scan.nextInt();
//Perform operation depending one what operator was entered.
if( operator == '+' )
{
int result = left_op + right_op;
System.out.println( left_op + " + " + right_op + " = " + result );
}
else if( operator == '-' )
{
int result = left_op - right_op;
System.out.println( left_op + " - " + right_op + " = " + result );
}
else if( operator == '*' )
{
int result = left_op * right_op;
System.out.println( left_op + " * " + right_op + " = " + result );
}
else if( operator == '/' )
{
int result = left_op / right_op;
System.out.println( left_op + " / " + right_op + " = " + result );
}
scan.nextLine();
while( progEnd != 'y' && progEnd != 'n')
{
System.out.print("Run again (Y/N): ");
stringInput = scan.nextLine().toLowerCase();
if( stringInput.length() == 0 )
progEnd = ' ';
else if ( stringInput.length() > 0 )
progEnd = stringInput.charAt(0);
}
if( stringInput.equals("n"))
break;
System.out.println();
}
}
}