Results 1 to 15 of 15
Thread: [SOLVED] for loop?
 02162009, 08:04 PM #1Member
 Join Date
 Jan 2009
 Posts
 21
 Rep Power
 0
[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
 02162009, 08:15 PM #2
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.
 02162009, 08:28 PM #3
... 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.
MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
 02162009, 08:34 PM #4Member
 Join Date
 Jan 2009
 Posts
 21
 Rep Power
 0
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.
 02162009, 08:39 PM #5
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);
MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
 02162009, 08:40 PM #6
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 :
 02162009, 08:48 PM #7Member
 Join Date
 Jan 2009
 Posts
 21
 Rep Power
 0
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
 02162009, 08:50 PM #8
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 /.
MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
 02162009, 08:51 PM #9Member
 Join Date
 Jan 2009
 Posts
 21
 Rep Power
 0
giving the probability that x rolls of the die will share the same number
 02162009, 08:53 PM #10Member
 Join Date
 Jan 2009
 Posts
 21
 Rep Power
 0
I wanted the program to multiply the numbers at it went through the loop. I just dunno how to do it .
 02162009, 08:54 PM #11
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.
MK12Tell me if you want a cool Java logo avatar like mine and I'll make you one.
 02162009, 09:24 PM #12Member
 Join Date
 Jan 2009
 Posts
 21
 Rep Power
 0
i figured it out.
boy am i a dumbass. lol. thanks for your help.
 02162009, 09:28 PM #13
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; } } }
 02162009, 09:30 PM #14
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 )
 02172009, 07:12 PM #15Member
 Join Date
 Jan 2009
 Posts
 21
 Rep Power
 0
Similar Threads

Help w/ loop
By iWonder in forum New To JavaReplies: 17Last Post: 11202008, 01:39 AM 
Need help with While Loop
By mrdestroy in forum New To JavaReplies: 14Last Post: 10202008, 03:29 PM 
while loop
By Unknown1369 in forum New To JavaReplies: 5Last Post: 07082008, 11:15 AM 
can you help me with this for loop?
By java_fun2007 in forum New To JavaReplies: 6Last Post: 12222007, 11:20 AM 
While loop
By leebee in forum New To JavaReplies: 1Last Post: 07182007, 04:11 PM
Bookmarks