Link to home
Start Free TrialLog in
Avatar of waltbaby315
waltbaby315

asked on

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

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);  

     }  

 }
Avatar of waltbaby315
waltbaby315

ASKER

hi
ASKER CERTIFIED SOLUTION
Avatar of TBK-Consulting
TBK-Consulting
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TommySzalapski
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