Need help with while condition
Author |
Message |
Banished_Outlaw
![](http://compsci.ca/v3/uploads/user_avatars/1352570945f49e05dadcb.jpg)
|
Posted: Tue Jan 22, 2008 8:25 pm Post subject: Need help with while condition |
|
|
code: | public class MagicalSquares {
public static void main (String [] args) {
int row1, row2, row3, col1, col2, col3, diag1, diag2;
int[][] grid = new int[3][3];
Logic Grid = new Logic();
//Grid.checkSolution();
Grid.displayGrid();
row1 = grid[0][0] + grid[0][1] + grid[0][2];
row2 = grid[1][0] + grid[1][1] + grid[1][2];
row3 = grid[2][0] + grid[2][1] + grid[2][2];
col1 = grid[0][0] + grid[1][0] + grid[2][0];
col2 = grid[0][1] + grid[1][1] + grid[2][1];
col3 = grid[0][2] + grid[1][2] + grid[2][2];
diag1 = grid[0][0] + grid[1][1] + grid[2][2];
diag2 = grid[2][0] + grid[1][1] + grid[0][2];
do {
Grid.userInput();
Grid.displayGrid();
System.out.println(grid[0][0]);
}
while (row1 != 15 && row2 != 15 && row3 != 15 && col1 != 15 && col2 != 15 && col3 != 15 && diag1 != 15 && diag2 != 15);
System.out.println ("Thank you for playing this game");
}
} |
code: |
import java.util.Scanner;
public class Logic {
public int num, row, col;
Scanner input = new Scanner(System.in);
public int[][] grid = new int[3][3];
public Logic() {
num = 0;
row = 0;
col = 0;
}
public void displayGrid() {
final int given = 5;
grid[1][1] = given;
for(int i = 0; i < 3; i++) {
for (int k = 0; k < 3; k++) {
System.out.print("[" + grid[i][k] + "]");
}
System.out.println();
}
}
public void userInput() {
System.out.print("Enter row number (0, 1, 2): ");
row = input.nextInt();
System.out.print("Enter column number (0, 1, 2): ");
col = input.nextInt();
System.out.print("Enter a number between 1 to 9");
num = input.nextInt();
num = grid[row][col];
}
// public void checkSolution() {
// }
}
|
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. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
HeavenAgain
![](http://compsci.ca/v3/uploads/user_avatars/139122102045e603120b143.jpg)
|
Posted: Tue Jan 22, 2008 9:06 pm Post subject: RE:Need help with while condition |
|
|
you know whats funny? in your "logic" class, its... hmmm... not TOO logic, ironic anyways
code: |
public void userInput() {
System.out.print("Enter row number (0, 1, 2): ");
row = input.nextInt();
System.out.print("Enter column number (0, 1, 2): ");
col = input.nextInt();
System.out.print("Enter a number between 1 to 9");
num = input.nextInt();
num = grid[row][col]; |
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 code: | row1 = grid[0][0] + grid[0][1] + grid[0][2];
row2 = grid[1][0] + grid[1][1] + grid[1][2];
row3 = grid[2][0] + grid[2][1] + grid[2][2];
col1 = grid[0][0] + grid[1][0] + grid[2][0];
col2 = grid[0][1] + grid[1][1] + grid[2][1];
col3 = grid[0][2] + grid[1][2] + grid[2][2];
diag1 = grid[0][0] + grid[1][1] + grid[2][2];
diag2 = grid[2][0] + grid[1][1] + grid[0][2]; | inside your do-while loop (or else row1-3 and col1-3 etc, will always be 0, because you are not checking it again), or better yet, put them into the logic class, and make a method to calculate them, maybe even use a for-loop to get the sum
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 thats it for now... i hope... ![Rolling Eyes Rolling Eyes](images/smiles/icon_rolleyes.gif) |
|
|
|
|
![](images/spacer.gif) |
Banished_Outlaw
![](http://compsci.ca/v3/uploads/user_avatars/1352570945f49e05dadcb.jpg)
|
Posted: 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 |
|
|
|
|
![](images/spacer.gif) |
Banished_Outlaw
![](http://compsci.ca/v3/uploads/user_avatars/1352570945f49e05dadcb.jpg)
|
Posted: Tue Jan 22, 2008 9:38 pm Post subject: Re: Need help with while condition |
|
|
this is what i had before:
code: | import java.util.Scanner;
public class MagicalSquares {
int row1, row2, row3, col1, col2, col3, diag1, diag2;
public static void main (String [] args) {
Logic ab = new Logic();
ab.checkSolution();
while (row1 != row2 && row2 != row3 && row3 != col1 && col1 != col2 && col2 != col3 && col3 != diag1 && diag1 != diag2) {
ab.displayGrid();
ab.userInput();
//ab.displayGrid();
}
System.out.println ("Thank you for playing this game");
}
} |
code: |
import java.util.Scanner;
public class Logic {
int num, row, col;
//final int given = 5;
Scanner input = new Scanner(System.in);
int[][] grid = new int[3][3];
//grid[1][1] = given;
public void displayGrid() {
for(int i = 0; i < 3; i++) {
for (int k = 0; k < 3; k++) {
System.out.print("[" + grid[i][k] + "]");
}
System.out.println();
}
}
public void userInput() {
System.out.print("Enter row number (0, 1, 2): ");
row = input.nextInt();
System.out.print("Enter column number (0, 1, 2): ");
col = input.nextInt();
System.out.print("Enter a number between 1 to 9");
num = input.nextInt();
grid[row][col] = num;
}
public void checkSolution() {
int row1, row2, row3, col1, col2, col3, diag1, diag2;
row1 = grid[0][0] + grid[0][1] + grid[0][2];
row1 = grid[1][0] + grid[1][1] + grid[1][2];
row1 = grid[2][0] + grid[2][1] + grid[2][2];
col1 = grid[0][0] + grid[1][0] + grid[2][0];
col2 = grid[0][1] + grid[1][1] + grid[2][1];
col3 = grid[0][2] + grid[1][2] + grid[2][2];
diag1 = grid[0][0] + grid[1][1] + grid[2][2];
diag2 = grid[2][0] + grid[1][1] + grid[0][2];
}
} |
and this is the error i get: 14 times "non-static variable row1 cannot be referenced from a static context" i get all these errors inside the while loop....i dunno what i did wrong?? |
|
|
|
|
![](images/spacer.gif) |
HeavenAgain
![](http://compsci.ca/v3/uploads/user_avatars/139122102045e603120b143.jpg)
|
Posted: Tue Jan 22, 2008 9:44 pm Post subject: RE:Need help with while condition |
|
|
heres an little example about statics
code: |
class Test
{
public static int numberOne = 1;
public int numberTwo = 2;
private int numberThree =3;
public Test
{
numberOne = 1;
numberTwo = 2;
numberThree =3;
}
public int getNumberThree()
{
return numberThree;
}
} |
code: | class TestMain
{
public static void main (String[] args)
{
System.out.println(Test.numberOne);
// System.out.println(Test.numberTwo); //gives you an error
//but now
Test testing = new Test();
System.out.println(testing.numberTwo);
// System.out.println(testing.numberThree); // gives another error, because it is private
//but now
System.out.println(testing.getNumberThree());
}
} |
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 |
|
|
|
|
![](images/spacer.gif) |
Banished_Outlaw
![](http://compsci.ca/v3/uploads/user_avatars/1352570945f49e05dadcb.jpg)
|
Posted: 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....![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
HeavenAgain
![](http://compsci.ca/v3/uploads/user_avatars/139122102045e603120b143.jpg)
|
Posted: 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 etc you dont use ab to get them anymore |
|
|
|
|
![](images/spacer.gif) |
Banished_Outlaw
![](http://compsci.ca/v3/uploads/user_avatars/1352570945f49e05dadcb.jpg)
|
Posted: 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?? ![Rolling Eyes Rolling Eyes](images/smiles/icon_rolleyes.gif) |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
HellblazerX
![](http://www.plamania.co.kr/shopimages/plmtest/2910040000213.jpg)
|
Posted: 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:
code: | do {
Grid.userInput();
Grid.displayGrid();
System.out.println(grid[0][0]);
}
while (row1 != 15 && row2 != 15 && row3 != 15 && col1 != 15 && col2 != 15 && col3 != 15 && diag1 != 15 && diag2 != 15);
|
You need change the values inside the do while loop, otherwise they will stay the same, and you will never exit the loop.
Secondly:
code: | public static void main (String [] args) {
int row1, row2, row3, col1, col2, col3, diag1, diag2;
int[][] grid = new int[3][3]; |
code: | public class Logic {
public int num, row, col;
Scanner input = new Scanner(System.in);
public int[][] grid = new int[3][3]; |
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:
code: |
row1 = grid[0][0] + grid[0][1] + grid[0][2]; |
you should have this:
code: | row1 = Grid.grid[0][0] + Grid.grid[0][1] + Grid.grid[0][2]; |
Once you've fixed this, you should put that block of code inside your do while loop. |
|
|
|
|
![](images/spacer.gif) |
Banished_Outlaw
![](http://compsci.ca/v3/uploads/user_avatars/1352570945f49e05dadcb.jpg)
|
Posted: 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 ![Smile Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
|
|