Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Calculator {
  3. public static final Scanner LUKIJA = new Scanner(System.in);
  4. private static final String SUMMAA = "add";
  5. private static final String EROTA = "diff";
  6. private static final String KERRO = "multi";
  7. private static final String JAA = "div";
  8. private static final String LOPETA = "quit";
  9.  
  10. public static String[] tarkista(String kasky) {
  11. String[] kaskyLista = kasky.split(" ");
  12. if (kaskyLista[0] != null) {
  13. if (kaskyLista.length == 2) {
  14. if (kaskyLista[0] == SUMMAA) {
  15. return kaskyLista;
  16. }
  17. else if (kaskyLista[0] == EROTA) {
  18. return kaskyLista;
  19. }
  20. else if (kaskyLista[0] == KERRO) {
  21. return kaskyLista;
  22. }
  23. else if (kaskyLista[0] == JAA) {
  24. return kaskyLista;
  25. }
  26. }
  27. else if (kaskyLista[0] == LOPETA) {
  28. return kaskyLista;
  29. }
  30. else {
  31. return null;
  32. }
  33. }
  34. else {
  35. return null;
  36. }
  37. }
  38.  
  39. public static void main(String[] args) {
  40. boolean jatkuu = true;
  41.  
  42. while (jatkuu) {
  43. System.out.println("Hello! I am, a simple calculator.");
  44. System.out.println("Please, enter a command:");
  45. String kasky = LUKIJA.nextLine();
  46.  
  47.  
  48. String[] laske = tarkista(kasky);
  49.  
  50. if (laske != null) {
  51. if (laske[0] == SUMMAA) {
  52. System.out.println(Integer.parseInt(laske[1]) + Integer.parseInt(laske[2]));
  53. }
  54. else if (laske[0] == EROTA) {
  55. System.out.println(Integer.parseInt(laske[1]) - Integer.parseInt(laske[2]));
  56. }
  57. else if (laske[0] == KERRO) {
  58. System.out.println(Integer.parseInt(laske[1]) * Integer.parseInt(laske[2]));
  59. }
  60. else if (laske[0] == JAA) {
  61. System.out.println((float)(Integer.parseInt(laske[1]) % Integer.parseInt(laske[2])));
  62. }
  63. else if (laske[0] == LOPETA) {
  64. jatkuu = false;
  65. }
  66. }
  67. else {
  68. System.out.println("Error!");
  69. }
  70. }
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement