Instantly share code, notes, and snippets.

# anonymous/AutoCalc.java Created Sep 9, 2012

What would you like to do?
AutoCalc by joshellis625
 // ********************************************** // Created by: Joshua Ellis (joshellis625) // // Solve multiple simple math problems with two // user inputted numbers // ********************************************** import java.util.Scanner; public class AutoCalc { //Declare variables public static int X, Y; public static int SUM; public static int DIFF; public static float DIV; public static int MULT; public static void main(String[] args) { getInput(); System.out.println(); System.out.println("Let's do some math using the numbers " + X + " and " + Y + "!"); System.out.println(); System.out.println("The sum of " + X + " and " + Y + " is " + getSum()); System.out.println("The difference between " + X + " and " + Y + " is " + getDiff()); System.out.println(X + " divided by " + Y + " is " + getDivide()); System.out.println(X + " times " + Y + " is " + getMultiply()); } //create new Scanner input and convert String to Int public static void getInput() { while(true) { try { Scanner input = new Scanner(System.in); //Apply String input to variables xString and yString System.out.print("Input first number: "); String X_STRING = input.next(); System.out.print("Input second number: "); String Y_STRING = input.next(); //Convert Strings to Integers X = Integer.parseInt(X_STRING); Y = Integer.parseInt(Y_STRING); break; } catch (NumberFormatException nfe) { System.err.println("I said enter numbers..."); } } } public static int getSum() { SUM = X + Y; return(SUM); } public static int getDiff() { DIFF = X - Y; return(DIFF); } public static float getDivide() { DIV = (float)X / (float)Y; return(DIV); } public static int getMultiply() { MULT = X * Y; return(MULT); } }
to join this conversation on GitHub. Already have an account? Sign in to comment