Computer Science Canada Need help with while condition |
Author: | Banished_Outlaw [ Tue Jan 22, 2008 8:25 pm ] | ||||
Post subject: | Need help with while condition | ||||
The program works perfectly but for some reason the condition in the while loop doesnt seem to work. Thats because the value assigned to grid[0][0] to grid[2][2] is always zero. But i did assign the variable num to fill grid[][] with whatever number the user typed. Then how come it doesnt do that in the main method?? Please Help I think you would get what im talking about if you run the program. |
Author: | HeavenAgain [ Tue Jan 22, 2008 9:06 pm ] | ||||
Post subject: | RE:Need help with while condition | ||||
![]() ![]()
look at the last line of that code, i hope you see where your mistake is, dont hurt yourself now ![]() ps. incase you dont see it, it should be grid[row][col] = num; no? oh and another thing i just spotted, you should include
PSS! also you might want to check if user enters the right input, for example in your program you say 1 to 9, but some jackass might come and enter 15, and it still works ![]() ![]() |
Author: | Banished_Outlaw [ Tue Jan 22, 2008 9:36 pm ] |
Post subject: | Re: Need help with while condition |
lol no no i had it right the first time, but the program wasnt working so i thought maybe if i declare it the other way ![]() and i also made a method checkSolution inside the Logic Class to calculate that but when i did that, it gave me an error saying something about static and non static syntax...?? but yeah once i finish this program ill add all those things and make it idiot proof |
Author: | Banished_Outlaw [ Tue Jan 22, 2008 9:38 pm ] | ||||
Post subject: | Re: Need help with while condition | ||||
this is what i had before:
and this is the error i get: 14 times ![]() |
Author: | HeavenAgain [ Tue Jan 22, 2008 9:44 pm ] | ||||
Post subject: | RE:Need help with while condition | ||||
heres an little example about statics
hmmm i hope it works, i just typed it up on notepad, it should work, and result should be expected! hope it will help you on your "sum" thingy edit: now i saw your 2nd post, hmmm it is because you are trying to access to method variables, im sure you learned about scooping, so inorder to access to those variable, you have to make them a class variable, or an instance variable, see the little example above :p |
Author: | Banished_Outlaw [ Tue Jan 22, 2008 9:57 pm ] |
Post subject: | Re: Need help with while condition |
hmm i declared the variables row1, row2 etc in the Logic class as public static row1, row2 etc; and it still gives me the same error :S....thats y i had to move that whole counter thingy to the main method....and i think i called the logic class properly too....@_@ lol i kno u must be getting tired of helping me out.....but i got no clue.... ![]() |
Author: | HeavenAgain [ Tue Jan 22, 2008 10:00 pm ] | ||
Post subject: | RE:Need help with while condition | ||
if you make them static, then you call to them by going like
|
Author: | Banished_Outlaw [ Tue Jan 22, 2008 10:11 pm ] |
Post subject: | Re: Need help with while condition |
ok thats genius....so i dont get any errors anymore but now im back to square one because the loop doesnt work wen its supposed to and i printed the values of row1 or row 2 to debug my programming and their values is always zero which means that they are not being assigned the value that the user types.....*sigh*....any ideas?? ![]() ![]() |
Author: | HellblazerX [ Wed Jan 23, 2008 2:10 pm ] | ||||||||||
Post subject: | Re: Need help with while condition | ||||||||||
Do not make your variables static. It makes it so they belong to the Class, not the instance of the class you create, and it becomes a mess trying to access and alter the values. Going back to your original problem with your do while loop:
You need change the values inside the do while loop, otherwise they will stay the same, and you will never exit the loop. Secondly:
You have two different sets of grid [] [], one that receives input from the user, and the other is actually used in the calculations. In other words, the grid that changes in value isn't used, and the grid used doesn't change in value. You need to use the grid inside your Grid class, not the one in your main method. So instead of this:
you should have this:
Once you've fixed this, you should put that block of code inside your do while loop. |
Author: | Banished_Outlaw [ Wed Jan 23, 2008 4:30 pm ] |
Post subject: | Re: Need help with while condition |
thanks HellBlazerX, the things you pointed out were true, the program is working now altho the while condition is still not glitch proof, but i think ill be able to handle that....thanks a lot u guys, ill definitely mention u guys in my program comments ![]() |