zh_stoqnov

InstructionSet

Jan 30th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. package InstructionSet;
  2. import java.util.Scanner;
  3.  
  4. public class InstructionSet{
  5. public static void main(String[] args) {
  6. Scanner input = new Scanner(System.in);
  7. boolean check = true; // Created boolean which we use as a condition for out while loop.
  8.  
  9. while (check == true) { //Changed the condition.
  10. String opCode = input.nextLine();//Inserted opCode inside Loop, so each time we can enter a new value for our string.
  11. if(opCode.equals("END")) {
  12. check = false; // Used the boolean value so whenever the input is END we can leave the loop.
  13. } else {
  14. String[] codeArgs = opCode.split(" ");
  15. long result = 0;
  16. switch (codeArgs[0]) {
  17. case "INC": {
  18. long operandOne = Integer.parseInt(codeArgs[1]);
  19. result = ++operandOne; // Changed from operand++ to ++operand, otherwise we get invalid output.
  20. break;
  21. }
  22. case "DEC": {
  23. long operandOne = Integer.parseInt(codeArgs[1]);//Switched to long so we can get our tests running.
  24. result = --operandOne;// Changed from operand-- to --operand, otherwise we get invalid output.
  25. break;
  26. }
  27. case "ADD": {
  28. long operandOne = Integer.parseInt(codeArgs[1]);//Switched to long so we can get our tests running.
  29. long operandTwo = Integer.parseInt(codeArgs[2]);//Switched to long so we can get our tests running.
  30. result = operandOne + operandTwo;
  31. break;
  32. }
  33. case "MLA": {
  34. long operandOne = Integer.parseInt(codeArgs[1]);//Switched to long so we can get our tests running.
  35. long operandTwo = Integer.parseInt(codeArgs[2]);//Switched to long so we can get our tests running.
  36. result = (long)(operandOne * operandTwo);
  37. break;
  38. }
  39. default:
  40. break;
  41. }
  42.  
  43. System.out.println(result);
  44. }
  45. }
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment