Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class squart {
- public static void main( String[] args ) {
- try {
- //checks the command line for the argument to input into the function
- System.out.println("Input a number:");
- java.util.Scanner sc = new java.util.Scanner( System.in);
- while (sc.hasNext()){
- String x = sc.next();
- //find the squareroot of the parameter
- double ans = Math.sqrt( Integer.parseInt(x) );
- //formats it to have two decimals
- String numberAsString = String.format ("%.2f", ans);
- //prints ans
- System.out.println("Math.sqrt(" + x + ")=" + numberAsString );
- //exits after the answer is returned
- System.exit(0);
- }
- }
- catch (Exception e) {
- /* This is a generic Exception handler which means it can handle
- * all the exceptions. This will execute if the exception is not
- * handled by previous catch blocks.
- */
- System.out.println("Exception occurred. Make sure it is a number!");
- }
- }
- }
Add Comment
Please, Sign In to add comment