Cornelius Eanes's Programming Portal
Assignments (Do-While Loops 2)
[107] Baby Calculator

Goal: Create a basic calculator with do-while loops.

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Baby Calculator
/// File Name: BabyCalculator.java
/// Date Finished: Feb 29, 2016

import java.util.Scanner;

public class BabyCalculator {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        double a, b, c;
        String op;

        do {
            System.out.print("> ");
            a = keyboard.nextDouble();
            op = keyboard.next();
            b = keyboard.nextDouble();

            if (op.equals("+")) {
                c = a + b;
            } else if (op.equals("-")) {
                c = a - b;
            } else if (op.equals("*")) {
                c = a * b;
            } else if (op.equals("/")) {
                c = a / b;
            } else {
                System.out.println("Undefined operator: '" + op + "'.");
                c = 0;
            }
            System.out.println(c);

        } while (a != 0);
    }
}

Output