### Author Topic: how to have a loop in codes to check ?  (Read 7486 times)

#### lookang

• Hero Member
• Posts: 1772
• http://weelookang.blogspot.com
##### how to have a loop in codes to check ?
« on: April 20, 2011, 10:45:12 am »
Hi Prof!
i want to check for phase difference in multiple of integers, if the answer is
phi = 6.28
student can key in 0, 6.28, 6.28*2,6.28*3 etc until a large number

my initial codes are here

if (Math.abs(phi-2*0*Math.PI-enterphi) < 0.1||Math.abs(phi-2*1*Math.PI-enterphi) < 0.1||Math.abs(phi-2*2*Math.PI-enterphi) < 0.1||Math.abs(phi-2*3*Math.PI-enterphi) < 0.1)
{

phicorrect = " Yes";
phicorrectbackground = java.awt.Color.GREEN;
}
else
{
phicorrect = " No";
phicorrectbackground = java.awt.Color.RED;

}

My question is how to do a loop?

i try
for (int counter=0; counter<100 /* Iterations */ ; counter++) {
if (Math.abs(phi-2*counter*Math.PI-enterphi) < 0.1) {
phicorrect = " Yes";
phicorrectbackground = java.awt.Color.GREEN;
}
else {
phicorrect = " No";
phicorrectbackground = java.awt.Color.RED;

}

but it did not check the phi is multiple of counter to accept answer as correct.
how to make the codes elegant?
thanks!

quite silly to write
if (Math.abs(phi-2*0*Math.PI-enterphi) < 0.1||Math.abs(phi-2*1*Math.PI-enterphi) < 0.1||Math.abs(phi-2*2*Math.PI-enterphi) < 0.1||Math.abs(phi-2*3*Math.PI-enterphi) < 0.1||Math.abs(phi+2*1*Math.PI-enterphi) < 0.1||Math.abs(phi+2*2*Math.PI-enterphi) < 0.1||Math.abs(phi+2*3*Math.PI-enterphi) < 0.1) { // valid only for 3 rounds left and right
phicorrect = "  Yes";
phicorrectbackground = java.awt.Color.GREEN;
}
else {
phicorrect = "  No";
phicorrectbackground = java.awt.Color.RED;

}
« Last Edit: April 20, 2011, 11:07:31 am by lookang »

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: how to have a loop in codes to check ?
« Reply #1 on: April 20, 2011, 11:01:19 pm »
I do not really undestand what you really need!
Do you mean student can key in 0, 6.28, 12.56,18.84,... etc
But it is incorrect if the input value is one of 6.27, 6.29,12.55,12.57, 11.0,... etc?

#### lookang

• Hero Member
• Posts: 1772
• http://weelookang.blogspot.com
##### Re: how to have a loop in codes to check ?
« Reply #2 on: April 21, 2011, 07:58:00 am »
Yes.
Phase lead is correct for phi+ 2.counter.pi where counter is integer n zero
http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=2178msg8065;topicseen#msg8065
Has my implementation phi
« Last Edit: April 21, 2011, 08:00:54 am by lookang »

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: how to have a loop in codes to check ?
« Reply #3 on: April 21, 2011, 12:13:52 pm »
if v is the input value(double), define pi2=2*Math.PI

then v%pi2 will give you remainder.

i.e.
3.2 % 2.0 will give you 1.2
6.5 % 2.0 will give you 1.5
6.29 % 6.28 will give you 0.01
6.27 % 6.28 will give you 6.27
12.57 % 6.28 will give you 0.01

If the remainder is less than 0.01 or larger than 6.27 will be good value for your case.
You do not need to use loop.

I hope this will help.

#### lookang

• Hero Member
• Posts: 1772
• http://weelookang.blogspot.com
##### Re: how to have a loop in codes to check ?
« Reply #4 on: April 21, 2011, 05:47:04 pm »
fantastic tip prof!

if (Math.abs(phi%(2*Math.PI)-enterphi%(2*Math.PI)) < 0.1){
phicorrect = " Yes";
phicorrectbackground = java.awt.Color.GREEN;
}
else {
phicorrect = " No";
phicorrectbackground = java.awt.Color.RED;

}

it is working very well http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=2178msg8065;topicseen#msg8065

thanks!

#### Fu-Kwun Hwang

• Hero Member
• Posts: 3062
##### Re: how to have a loop in codes to check ?
« Reply #5 on: April 21, 2011, 06:12:07 pm »
I just provided the hits and you can solve the problem by yourself. Very good!

#### lookang

• Hero Member
• Posts: 1772
• http://weelookang.blogspot.com
##### Re: how to have a loop in codes to check ?
« Reply #6 on: April 21, 2011, 06:58:27 pm »
U r the master of skillful facilitation and simulation making.
At lot of people in the world are benefiting from your wisdom and work like me.
Hope to be like u some day u r a great teacher !

Thanks !!
« Last Edit: April 21, 2011, 07:00:02 pm by lookang »

#### csesuvra

• Newbie
• Posts: 1
##### Re: how to have a loop in codes to check ?
« Reply #7 on: June 02, 2011, 02:37:44 pm »
Great discussion and I am also like getting feedback. Thank you.