I started to learn Java some time ago and I thought that making a calculator that works in the terminal. Lately I added an Array list to store the history, and then something went wrong. Calculator program:
import java.util.Scanner;
import java.util.ArrayList;
public class calc_case {
public static void main(String[] args) {
System.out.println("Welcom to The Calculator!");
double a;
double b;
double c;
Scanner input0;
int input = 0;
ArrayList<Double> history = new ArrayList<Double>();
while (input != 6) {
try {Thread.sleep(2000);} catch(InterruptedException ex) {Thread.currentThread().interrupt();}
a = 0; b = 0; c = 0; input = 0;
System.out.println("#################################################");
System.out.println("How can I help you?");
System.out.println("1-Add\n2-Subtrackt\n3-Devide\n4-Multiply\n5-Show history\n6-Exit");
input0 = new Scanner(System.in);
input = input0.nextInt();
switch (input) {
case 1: //add
System.out.println("Input two numbers:");
input0 = new Scanner(System.in);
a = input0.nextDouble();
input0 = new Scanner(System.in);
b = input0.nextDouble();
c = a + b;
System.out.println("Calculating... \nThe answer is: " + c );
break;
case 2: //subtrackt
System.out.println("Input two numbers:");
input0 = new Scanner(System.in);
a = input0.nextDouble();
input0 = new Scanner(System.in);
b = input0.nextDouble();
c = a - b;
System.out.println("Calculating... \nThe answer is: " + c );
break;
case 3: //devide
System.out.println("Input two numbers:");
input0 = new Scanner(System.in);
a = input0.nextDouble();
input0 = new Scanner(System.in);
b = input0.nextDouble();
c = a/b;
System.out.println("Calculating... \nThe answer is: " + c );
break;
case 4: //multiply
System.out.println("Input two numbers:");
input0 = new Scanner(System.in);
a = input0.nextDouble();
input0 = new Scanner(System.in);
b = input0.nextDouble();
c = a*b;
System.out.println("Calculating... \nThe answer is: " + c );
break;
case 5: //history
for (int x = 0; x < history.size(); x++) {
System.out.println(history.get(x));
}
case 6: //exit
System.out.println("Goodbye!\n Killing process... " + " OK");
default:
history.add(c);
}
}
}
}
After adding case 5 as 'Show history' and moving case 5 'Exit' to case 6 'Exit' after choosing option 5 ('Show history') I get this:
Goodbye!
Killing process... OK
I tried recompiling the program, deleting the class file, and pasting the program to a new file. What is wrong with my program?
- OS: Ubuntu 16.04 Java version:
- Java version: openjdk version "1.8.0_91"