Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Program to allow user to guess the square of a number that is entered

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 07-09-2012, 07:12 PM PM User | #1 laylo New Coder   Join Date: Jul 2011 Posts: 19 Thanks: 12 Thanked 0 Times in 0 Posts Program to allow user to guess the square of a number that is entered Now this is still early in a chapter of a java book im reading so i just know the basic concepts of programming. please keep your answers as simple as possible. in this question im not sure what the missing line of code is in the comment:- Code: ```import java.util.*; public class Iterationq7 public static void main(String[] args) { int num, square; Scanner keyboard = new Scanner(System.in); print"Enter a number"; num = keyboard.nextInt(); print"Enter the square of this number" square = keyboard.nextInt(); while(//test to be completed) { print"Wrong answer, try again"; square = keyboard.nextInt(); } print"Well done, right answer" } }```
 07-09-2012, 07:33 PM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,606 Thanks: 4 Thanked 2,598 Times in 2,567 Posts You need to use a comparative operation between the square of the num and square. The alternative (IMO better) would be to square the square and compare that to the num. For primitives, you can use direct comparisons. See this tutorial for more information: http://docs.oracle.com/javase/tutori...bolts/op2.html And for mathematics, see the Math class in java.lang.Math here: http://docs.oracle.com/javase/6/docs...lang/Math.html Note the accepted types and most particularly the return types of both the sqrt and the pow functions to make your comparisons. This is why I suggest comparing the square of your square instead of the square root of your number. Doing the square of your square guarantees an even integer cast on the result (for which you can even do a simple square * square as these are both integers already), whilst going the other way results in a double, even if the result is evenly divisible. So doing `(int)x * (int)x` is equivalent to doing `(int)Math.pow(x, 2);`.
 07-11-2012, 09:30 PM PM User | #3 michaelh73 New Coder   Join Date: Jun 2012 Posts: 15 Thanks: 0 Thanked 3 Times in 3 Posts Yea, so in your case you can do put in the while loop while((num*num)!=square){ .. } also I don't think print is a function that works alone... it should be System.out.println("text here"); __________________ Tech jobs and developer jobs at Headhuntable

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 05:16 AM.