Guest User

Untitled

a guest
Nov 13th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  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. }
Add Comment
Please, Sign In to add comment