[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2011-03-24
3
Medium Priority
?
435 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 

Author Comment

by:waltbaby315
ID: 35212123
hi
0
 
LVL 9

Accepted Solution

by:
TBK-Consulting earned 2000 total points
ID: 35212310
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
ID: 35234565
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Guide to the PMT, FV, IPMT and PPMT Functions In MS Excel we have the PMT, FV, IPMT and PPMT functions, which do a fantastic job for interest rate calculations.  But what if you don't have Excel ? This article is for programmers looking to re…
This article covers the basics of data encryption, what it is, how it works, and why it's important. If you've ever wondered what goes on when you "encrypt" data, you can look here to build a good foundation for your personal learning.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

649 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