Results 1 to 15 of 15
  1. #1
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default [SOLVED] for loop?

    Hello to all. I wrote this code: It prints out a list of numbers, but does anybody know how they can be multiplied?

    class Testnoo

    {

    public static void main (String [] args)

    {

    int foundAt = 3;
    int x = 365 - foundAt;
    float q = 365;
    float p;
    double product;


    for (p = 365; p >= x; p --)
    {


    product = p/q;




    System.out.println(product);



    }



    }//end main

    }//end class

  2. #2
    sirwiggles's Avatar
    sirwiggles is offline Member
    Join Date
    Feb 2009
    Posts
    31
    Rep Power
    0

    Default

    how the numbers printed out can be multiplied? Is that what your asking?

    If so you could put the 'product' value into an array, this would collect the list of numbers.

    You could then create another for loop which cycles the array and multiplies the values within.

  3. #3
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    ... whats so hard about multiplying?
    a = b * c;
    System.out.println(a);
    please be more specific. And another tip is give your variables meaningful names, 1 letter names that are completely irrelevant make code very hard to read.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  4. #4
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    Yes its multiplying the numbers that are printed out.

    MK12. a = b*c

    what would be the values for a b and c? I'm confused.

  5. #5
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    What do you want them to be? What's the purpose of this?
    ex. double a = 5.648458; double b = Math.PI; System.out.print(a*b);
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  6. #6
    sirwiggles's Avatar
    sirwiggles is offline Member
    Join Date
    Feb 2009
    Posts
    31
    Rep Power
    0

    Default

    It depends what you are actually wanting to multiply..

    I don't understand what you are trying to do, what is the purpose of the program, why do you want to multiply the numbers, what is your disired output..

    It's hard to help when you give so little information :|

  7. #7
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    The numbers are probabilities. I have to multiply them together.

    the multiplied numbers just give a combined probability

    if you run the program above it prints out four numbers.

    you know like when you have a dice and you are trying to get the probability that the second roll will be different from the first.

    the first roll will be probability 6/6

    the second roll will be probability 5/6 that this number is different from the first

    the third roll will be probability 4/6 that this number is different from the first two

    e.t.c - you have to multiply them all together and subtract from 1

  8. #8
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    So is your question just how to do the math? There's nothing to change it when you put in program except multiplication becomes * and division becomes /.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  9. #9
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    giving the probability that x rolls of the die will share the same number

  10. #10
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    I wanted the program to multiply the numbers at it went through the loop. I just dunno how to do it .

  11. #11
    MK12's Avatar
    MK12 is offline Senior Member
    Join Date
    Jan 2009
    Posts
    185
    Rep Power
    6

    Default

    Which numbers do you want it to multiply? It would be helpful If you reposted your code in Code tags to preserve format so that we can read it, and maybe show the parts you need help at in different colours.
    -MK12
    Tell me if you want a cool Java logo avatar like mine and I'll make you one.

  12. #12
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    i figured it out.

    boy am i a dumbass. lol. thanks for your help.

  13. #13
    sirwiggles's Avatar
    sirwiggles is offline Member
    Join Date
    Feb 2009
    Posts
    31
    Rep Power
    0

    Default

    This may be your solution, check over it, make sure you understand it.

    Java Code:
    class Testnoo
    
    {
    
        public static void main (String[] args)
    
        {
    
            int foundAt = 3;
            int x = 365 - foundAt;
            float q = 365;
            float p;
            double currentProduct = 1.00;
            double previousProduct = 1.00;
            double multiProduct;
    
            for (p = 365; p >= x; p --)
            {
                //calculate the individual probability
                currentProduct = p/q;
                
                //output the individual probability
                System.out.println("Probability: " + currentProduct);
                
                //calculate the overall probability
                multiProduct = currentProduct * previousProduct;
    
                //output the overall probability
                System.out.println("Overall Probability: " + multiProduct);
                
                //set the current probability to previous 
                previousProduct = currentProduct;
    
            }
    
        }
    
    }

  14. #14
    sirwiggles's Avatar
    sirwiggles is offline Member
    Join Date
    Feb 2009
    Posts
    31
    Rep Power
    0

    Default

    Cool you figured it out, post the solution and check that the thread is solved please :)

    ( To set the thread as solved, go to, thread tools > solved )

  15. #15
    hawaiifiver is offline Member
    Join Date
    Jan 2009
    Posts
    21
    Rep Power
    0

    Default

    Java Code:
    class Testno
    
    {
    
    public static void main (String [] args)
    
    {
    
    	int foundAt = 2;
    	int x = 365 - foundAt;
    	float q = 365;
    	float p;
    	double product = 1;
    
    
    	for (p = 365; p >= x; p --)
    	{
    
    		
    		product = product*p/q;
    
    	
    	}
    
    	 double r = 1 - product;
    
    	System.out.println(r);
    
    }//end main
    
    }//end class

Similar Threads

  1. Help w/ loop
    By iWonder in forum New To Java
    Replies: 17
    Last Post: 11-20-2008, 01:39 AM
  2. Need help with While Loop
    By mrdestroy in forum New To Java
    Replies: 14
    Last Post: 10-20-2008, 03:29 PM
  3. while loop
    By Unknown1369 in forum New To Java
    Replies: 5
    Last Post: 07-08-2008, 11:15 AM
  4. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 11:20 AM
  5. While loop
    By leebee in forum New To Java
    Replies: 1
    Last Post: 07-18-2007, 04:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •