Skip to content

Instantly share code, notes, and snippets.

@Noia
Forked from anonymous/AutoCalc.java
Created September 9, 2012 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noia/3681798 to your computer and use it in GitHub Desktop.
Save Noia/3681798 to your computer and use it in GitHub Desktop.
AutoCalc by joshellis625
import java.util.Scanner;
/**
* I have not compiled this, so I make no promises that I will work as expected...
*/
public class AutoCalc {
private int x = 0, y = 0;
private final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
new AutoCalc().read().print();
}
public AutoCalc print() {
System.out.printf("Let's do some math using the numbers %d and %d!%n",x,y);
System.out.println();
System.out.printf("The sum of %d and %d is %d%n",x,y,x+y);
System.out.printf("The difference between %d and %d is %d%n",x,y,x-y);
System.out.printf("%d divided by %d is %f%n",x,y,x/y);
System.out.printf("%d times %d is %d",x,y,x*y);
return this;
}
public AutoCalc read() {
while(true) {
try {
System.out.print("Input first number: ");
x = scanner.nextInt();
System.out.print("Input second number: ");
y = scanner.nextInt();
System.out.println();
return this;
} catch (InputMismatchException | NoSuchElementException e) {
System.out.println("Thats not a number...or something");
}
}
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment