Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package rekenmachine;
- import java.util.Scanner;
- //Program used to calculate small sums
- /**
- * @author Yannick Sminia
- */
- public class Rekenmachine {
- //Validating the operator
- public static boolean isValidOperator(char operator) {
- return operator == '-' || operator == '+' || operator == '*' || operator == '/' || operator == '%';
- }
- //Answer input and loop
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- char operator = 1;
- //Input loop
- while (operator != 's') {
- System.out.print("Operator (S = stoppen) : ");
- operator = input.next().charAt(0);
- if (isValidOperator(operator)) {
- System.out.print("Eerste getal: ");
- double number1 = input.nextDouble();
- System.out.print("Tweede getal: ");
- double number2 = input.nextDouble();
- printSum(operator, number1, number2);
- } else {
- //Invalid operator error
- if(operator != 's'){
- System.out.println("Operator is ongeldig\n"
- + "");
- }
- }
- }
- }
- //Calculting and printing the answer
- public static void printSum(char operator, double number1, double number2) {
- //Logic to see which operator to use
- double answer = 1;
- if (operator == '-') {
- answer = number1 - number2;
- }
- if (operator == '+') {
- answer = number1 + number2;
- }
- if (operator == '*') {
- answer = number1 * number2;
- }
- if (operator == '/' || operator == '%') {
- answer = number1 / number2;
- }
- //Printing sum with answer
- System.out.println(number1 + " " + operator + " " + number2 + " = " + answer + "\n"
- + "");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement