Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. package rekenmachine;
  2.  
  3. import java.util.Scanner;
  4.  
  5. //Program used to calculate small sums
  6.  
  7. /**
  8. * @author Yannick Sminia
  9. */
  10. public class Rekenmachine {
  11.  
  12. //Validating the operator
  13. public static boolean isValidOperator(char operator) {
  14. return operator == '-' || operator == '+' || operator == '*' || operator == '/' || operator == '%';
  15. }
  16.  
  17. //Answer input and loop
  18. public static void main(String[] args) {
  19.  
  20. Scanner input = new Scanner(System.in);
  21.  
  22. char operator = 1;
  23.  
  24. //Input loop
  25. while (operator != 's') {
  26. System.out.print("Operator (S = stoppen) : ");
  27. operator = input.next().charAt(0);
  28.  
  29. if (isValidOperator(operator)) {
  30.  
  31. System.out.print("Eerste getal: ");
  32. double number1 = input.nextDouble();
  33. System.out.print("Tweede getal: ");
  34. double number2 = input.nextDouble();
  35.  
  36. printSum(operator, number1, number2);
  37.  
  38. } else {
  39. //Invalid operator error
  40. if(operator != 's'){
  41. System.out.println("Operator is ongeldig\n"
  42. + "");
  43. }
  44. }
  45. }
  46.  
  47. }
  48.  
  49. //Calculting and printing the answer
  50. public static void printSum(char operator, double number1, double number2) {
  51.  
  52. //Logic to see which operator to use
  53. double answer = 1;
  54. if (operator == '-') {
  55. answer = number1 - number2;
  56. }
  57. if (operator == '+') {
  58. answer = number1 + number2;
  59. }
  60. if (operator == '*') {
  61. answer = number1 * number2;
  62. }
  63. if (operator == '/' || operator == '%') {
  64. answer = number1 / number2;
  65. }
  66.  
  67. //Printing sum with answer
  68. System.out.println(number1 + " " + operator + " " + number2 + " = " + answer + "\n"
  69. + "");
  70. }
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement