NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
January 23, 2021, 03:52:47 pm

"Make priority, take action and start now." ...Wisdom

 Pages: [1]   Go Down
 Author Topic: how to have a loop in codes to check ?  (Read 7210 times) 0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message on: April 20, 2011, 09:45:12 am » posted from:SINGAPORE,SINGAPORE,SINGAPORE

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, 10:07:31 am by lookang » Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3086

 « Embed this message Reply #1 on: April 20, 2011, 10:01:19 pm » posted from:Taipei,T'ai-pei,Taiwan

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?
 Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #2 on: April 21, 2011, 06:58:00 am » posted from:SINGAPORE,SINGAPORE,SINGAPORE

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, 07:00:54 am by lookang » Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3086

 « Embed this message Reply #3 on: April 21, 2011, 11:13:52 am » posted from:Taipei,T'ai-pei,Taiwan

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.
 Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #4 on: April 21, 2011, 04:47:04 pm » posted from:SINGAPORE,SINGAPORE,SINGAPORE

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!
 Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3086

 « Embed this message Reply #5 on: April 21, 2011, 05:12:07 pm » posted from:Taipei,T'ai-pei,Taiwan

I just provided the hits and you can solve the problem by yourself. Very good!
 Logged
lookang
Hero Member

Offline

Posts: 1796

http://weelookang.blogspot.com

 « Embed this message Reply #6 on: April 21, 2011, 05:58:27 pm » posted from:Singapore,,Singapore

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, 06:00:02 pm by lookang » Logged
csesuvra
Newbie

Offline

Posts: 1

 « Embed this message Reply #7 on: June 02, 2011, 01:37:44 pm » posted from:Dhaka,Dhaka,Bangladesh

Great discussion and I am also like getting feedback. Thank you.
 Logged
 Pages: [1]   Go Up
"Make priority, take action and start now." ...Wisdom