r/programmingbydoing • • Nov 21 '14

#60 Safe Square Root

Hi all, I am having a problem with this one. Sometimes it executes properly, other times its not coming out exactly how it was instructed. Can anyone take a look and give me some feedback?

Here's my code, thanks! :

import java.util.Scanner;

public class SafeSquareRoot {

public static void main(String[] args){
		Scanner keyboard = new Scanner(System.in);
		int negative = 0;
		int num1;
		
		
	  do {
			System.out.println("SQUARE ROOT!");
			System.out.print("Enter a number:");
			num1 = keyboard.nextInt();
			System.out.println("The square root of "+num1+ " is " + Math.sqrt(num1)+".");
	  }

	
	  
		 while (num1 >= negative); 
		 {
			    System.out.println("You can't take the square root of a negative number, silly");
				System.out.print("Try again:");
				  num1 = keyboard.nextInt();
				System.out.println("The square root of "+num1+ " is " + Math.sqrt(num1)+".");
	  }
		 
}

}

1 Upvotes

3 comments sorted by