Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Untitled

a guest Nov 13th, 2018 93 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. public class squart {
  2.   public static void main( String[] args ) {
  3.     try {
  4.       //checks the command line for the argument to input into the function
  5.       System.out.println("Input a number:");
  6.       java.util.Scanner sc = new java.util.Scanner( System.in);
  7.       while (sc.hasNext()){
  8.         String x = sc.next();
  9.  
  10.         //find the squareroot of the parameter
  11.         double ans = Math.sqrt( Integer.parseInt(x) );
  12.  
  13.         //formats it to have two decimals
  14.         String numberAsString = String.format ("%.2f", ans);
  15.  
  16.         //prints ans
  17.         System.out.println("Math.sqrt(" + x + ")=" + numberAsString );
  18.  
  19.         //exits after the answer is returned
  20.         System.exit(0);
  21.       }
  22.     }
  23.     catch (Exception e) {
  24.       /* This is a generic Exception handler which means it can handle
  25.       * all the exceptions. This will execute if the exception is not
  26.       * handled by previous catch blocks.
  27.       */
  28.       System.out.println("Exception occurred. Make sure it is a number!");
  29.     }
  30.   }
  31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top