Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1.  
  2. import java.io.IOException;
  3. import java.util.Scanner;
  4.  
  5. public class SwitchCalc {
  6.  
  7. static int resultOfOperation (int firstDigit,int secondDigit, String operation) {
  8. int result = 0;
  9. switch (operation){
  10. case "+":
  11. result = firstDigit + secondDigit;
  12. break;
  13. case "-":
  14. result = firstDigit - secondDigit;
  15. break;
  16. case "*":
  17. result = firstDigit * secondDigit;
  18. break;
  19. case "/":
  20. if (secondDigit==0)
  21. result = 0;
  22. else
  23. result = firstDigit / secondDigit;
  24. break;
  25. default:
  26. System.out.println("Wrong operation");
  27. }
  28. return result;
  29. }
  30. static double resultOfOperation (double firstDigit,double secondDigit, String operation) {
  31. double result = 0;
  32. switch (operation){
  33. case "+":
  34. result = firstDigit + secondDigit;
  35. break;
  36. case "-":
  37. result = firstDigit - secondDigit;
  38. break;
  39. case "*":
  40. result = firstDigit * secondDigit;
  41. break;
  42. case "/":
  43. if (secondDigit==0)
  44. result = 0;
  45. else
  46. result = firstDigit / secondDigit;
  47. break;
  48. default:
  49. System.out.println("Wrong operation");
  50. }
  51. return result;
  52. }
  53. public static void main(String[] args) throws IOException {
  54. double sum;
  55. Scanner reader = new Scanner(System.in);
  56. System.out.println("Entering value: ");
  57. System.out.print("Enter the first value: ");
  58. String str = reader.nextLine();
  59. String strParse1 = str.replace(',', '.');
  60. double inputNum1 = Double.parseDouble(strParse1);
  61. System.out.print("Enter the second value: ");
  62. String str3 = reader.nextLine();
  63. String strParse2 = str3.replace(',', '.');
  64. double inputNum2 = Double.parseDouble(strParse2);
  65. System.out.println("Result: ");
  66. System.out.println("A comma in the first value is in position: " + str.indexOf(','));
  67. System.out.println("A comma in the second value is in position: " + str3.indexOf(','));
  68.  
  69. sum = SwitchCalc.resultOfOperation (inputNum1,inputNum2, "+");
  70. System.out.println(strParse1 + " + " + strParse2 + " = " + sum);
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement