Created
February 16, 2013 16:59
-
-
Save anonymous/4967693 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import acm.program.*; | |
public class Calculator4 extends ConsoleProgram { | |
private double first; | |
private double second; | |
private double result; | |
public void run() { | |
while (result <= 1337) { | |
print("First operand: "); | |
first = readDouble(); | |
print("Second operand: "); | |
second = readDouble(); | |
print("Operator: "); | |
String operator = readLine(); | |
performOperation(operator); | |
println("The result of " + first | |
+ operator + second + " is "+ result); | |
} | |
println("The result was over 1337, so I stopped!"); | |
} | |
} | |
private void performOperation(String operator) { | |
if (operator.equals("+")) { | |
result = first + second; | |
} else if (operator.equals("-")) { | |
result = first - second; | |
} else if (operator.equals("/")) { | |
result = first / second; | |
} else if (operator.equals("*")) { | |
result = first * second; | |
} else { | |
result = 0; | |
} | |
} | |
import acm.program.ConsoleProgram; | |
public class hei extends ConsoleProgram{ | |
public void run(){ | |
double first=readDouble("First Operand: "); | |
double second= readDouble("Second Operand: "); | |
String operator= readLine("Operator: "); | |
char third= operator.charAt(0); | |
double answer= | |
calculatorOperator(first,second,third); | |
print("the result of "+first+" "+third+" “ | |
+second+" = "+answer ); | |
} | |
} | |
} | |
public double calculatorOperator(double first,double | |
second, char third){ | |
double answer = 0; | |
switch(third){ | |
case '+': answer=first + second; | |
break; | |
case '/': answer=first/second; | |
break; | |
case '*': answer= first*second; | |
break; | |
case '-': answer= first-second; | |
break; | |
default: print("u fucked up"); | |
break; | |
} | |
return answer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment