Created
January 26, 2015 18:32
-
-
Save SmileyVi/98c8686baa3abe6ac306 to your computer and use it in GitHub Desktop.
This file contains 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 java.util.Scanner; | |
public class newins { | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
String opCode = input.nextLine(); | |
String[] codeArgs = opCode.split(" "); | |
while (!opCode.equals("END")){// the while loop must be here, after we have our array, so it can check if there is "END" | |
long result=0; | |
switch (codeArgs[0]) { | |
case "INC": { | |
long operandOne = Integer.parseInt(codeArgs[1]); //here I changed the type from int to long because it's possible the result be upper than maxvalue of int | |
result = operandOne+1; // I have remove the operator ++, because it gave new value after itself, that's why in broken program the result was wrong (it was printing the value before ++) | |
break; | |
} | |
case "DEC": { | |
long operandOne = Integer.parseInt(codeArgs[1]); //here I changed the type from int to long because it's possible the result be upper than maxvalue of int | |
result = operandOne-1; // here changes are at the same reason like with ++ case in upper command. | |
break; | |
} | |
case "ADD": { | |
long operandOne = Integer.parseInt(codeArgs[1]); //here I changed the type from int to long because it's possible the result be upper than maxvalue of int | |
long operandTwo = Integer.parseInt(codeArgs[2]); | |
result = operandOne + operandTwo; | |
break; | |
} | |
case "MLA": { | |
long operandOne = Integer.parseInt(codeArgs[1]); //here I changed the type from int to long because it's possible the result be upper than maxvalue of int | |
long operandTwo = Integer.parseInt(codeArgs[2]); | |
result = (operandOne * operandTwo); // here we don't need type casting because result and operands are of the same type (long). I have change type of the all operands. | |
break;} | |
default:{ | |
break; | |
} | |
} | |
System.out.println(result); | |
opCode = input.nextLine(); // Those TWO rows I make here, because we have to get another command like new input | |
codeArgs = opCode.split(" "); // ^here^ | |
} | |
input.close(); // if the input is "END" we are out of the while loop, and we get here, where the input gets closed. | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment