Solved

How to write a program using this statement, int n = (int)Math.floor(Math.random()*100000+1);?

Posted on 2011-03-24
3
401 Views
Last Modified: 2012-05-11
Math
How to write a program using this statement, int n = (int)Math.floor(Math.random()*100000+1);?



lass GuessingGame {  

     public static void main(String[] args) {      

         int n = (int)Math.floor(Math.random()*100000+1);  

         int sum = 0;  

         int digit;  

         number = x;  

         digit = x % 10;  

         sum += digit;  

         x = x/10;  

         digit = x % 10;  

         sum += digit;  

         x = x/10;  

         digit = x % 10;  

         sum += digit;  

         x = x/10;  

         digit = x % 10;  

         sum += digit;  

         System.out.println("Given number= "+number+"; sum of digits = "+sum);  

     }  

 }
0
Comment
Question by:waltbaby315
3 Comments
 

Author Comment

by:waltbaby315
Comment Utility
hi
0
 
LVL 9

Accepted Solution

by:
TBK-Consulting earned 500 total points
Comment Utility
what exactly are you trying to accomplish?  Have the computer guess a number that the user randomly chose between 1 and 100,000??? if so - I'd have it do the following - (pseudo-coded as I am not good with languages)

Get Random Number between 2 and 100,000 and store in Guess
Set Low Guess = 0
Set High Guess = 100001
Start Loop
     Ask User if Guess is correct and ask for lower or higher
     If higher
          move guess to low guess
          if guess + 100 <  high guess
               add 100 to guess goto start loop
          else if guess +10 < high guess
              add 10 to guess goto start loop
              else add 1 to guess goto start loop
     if lower
          move guess to high guess
          if guess - 100 > low guess
               subtract 100 from guess goto start loop
          else if guess - 10 > low guess
               subtract 10 from guess goto start loop
               else subtract 1 from guess goto start loop
End Loop


NOTE - the more nested if's you put in the faster the program should get to the correct number.
0
 
LVL 37

Expert Comment

by:TommySzalapski
Comment Utility
Your program is fine (well, it has errors), but you should really use a loop. Like this:
   public static void main(String[] args) 
   {      
 
         int n = (int)Math.floor(Math.random()*100000+1);  

         int sum = 0;  

         int digit;  

         int x = n;  

         while(x > 0)
         {
            digit = x % 10;  
            x /= 10;
            sum += digit;  
         }
         System.out.println("Given number= "+number+"; sum of digits = "+sum);  

     }  

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever thought of installing a power system that generates solar electricity to power your house? Some may say yes, while others may tell me no. But have you noticed that people around you are now considering installing such systems in their …
Lithium-ion batteries area cornerstone of today's portable electronic devices, and even though they are relied upon heavily, their chemistry and origin are not of common knowledge. This article is about a device on which every smartphone, laptop, an…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now