public
Created — forked from /AutoCalc.java

AutoCalc by joshellis625

  • Download Gist
AutoCalc.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
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;
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.