Beginner having trouble with nested if statements

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 2
Reputation: I3loodTeeth is an unknown quantity at this point 
Solved Threads: 0
I3loodTeeth I3loodTeeth is offline Offline
Newbie Poster

Beginner having trouble with nested if statements

 
0
  #1
Apr 13th, 2008
Hi all,
I am learning C# at uni, first year and we are required to make a shell game for our first assignment. I'm not looking for someone to help me complete the whole assignment, but rather just to help me with a little problem I have. When i run this program I expect the appropriate calculations from the if statements I have supplied, but instead ALL of the statements in ALL of the if statements are calculated and I dont know why. I checked and double checked curly braces etc. but nothing fixes this problem! Please feel free to check the code below and have a chuckle about my "beginnerness". Thank you.

I3loodTeeth

P.S. to see what was going on with all the values I made another program that displays values as labels.Text. I will also include this code to make things a little bit easier to follow.
  1.  
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace __Shells_Game
  11. {
  12. public partial class cupsGameParentForm : Form
  13. {
  14.  
  15. // This is where I declare all of my background variables and assign them values
  16. int highestWinningStreakInt = 0;
  17. int highestLosingStreakInt = 0;
  18. int winsInt;
  19. int lossesInt;
  20. int turnsInt;
  21. int gameOverInt;
  22. int currentHighestWinningStreakInt;
  23. int currentHighestLosingStreakInt;
  24. int redCupAlreadySelectedInt;
  25. int greenCupAlreadySelectedInt;
  26. int blueCupAlreadySelectedInt;
  27. int randomNumberInt;
  28.  
  29. public cupsGameParentForm()
  30. {
  31. InitializeComponent();
  32.  
  33. }
  34.  
  35. private void Form1_Load(object sender, EventArgs e)
  36. {
  37.  
  38. }
  39.  
  40. // This is the button used to select a New Game.
  41. private void newGameButton_Click(object sender, EventArgs e)
  42. {
  43. // This resets all the pictures and labels to the "New Game" state.
  44. redCupPicture.Image = __Shells_Game.Properties.Resources.Cup_Red2;
  45. greenCupPicture.Image = __Shells_Game.Properties.Resources.Cup_Green2;
  46. blueCupPicture.Image = __Shells_Game.Properties.Resources.Cup_Blue2;
  47. redCupLiftedPicture.Image = __Shells_Game.Properties.Resources.Main_Form_Background_Colour;
  48. greenCupLiftedPicture.Image = __Shells_Game.Properties.Resources.Main_Form_Background_Colour;
  49. blueCupLiftedPicture.Image = __Shells_Game.Properties.Resources.Main_Form_Background_Colour;
  50. winLossLabel.Text = " ";
  51. instructionLabel.Text = "Select a Cup";
  52.  
  53. // This is where the game selects a random numberfrom 1 to 3.
  54.  
  55. Random generateRandom = new Random();
  56. randomNumberInt = generateRandom.Next(1, 4);
  57.  
  58. // This code assigns values to all the integer values.
  59. currentHighestWinningStreakInt = int.Parse(highestWinningStreakLabel.Text);
  60. currentHighestLosingStreakInt = int.Parse(highestLosingStreakLabel.Text);
  61. winsInt = int.Parse(winsLabel.Text);
  62. lossesInt = int.Parse(lossesLabel.Text);
  63. turnsInt = 0;
  64. gameOverInt = 0;
  65. redCupAlreadySelectedInt = 0;
  66. greenCupAlreadySelectedInt = 0;
  67. blueCupAlreadySelectedInt = 0;
  68. }
  69.  
  70. private void redCupPicture_Click(object sender, EventArgs e)
  71. {
  72. if (gameOverInt == 0);
  73. {
  74. redCupLiftedPicture.Image = __Shells_Game.Properties.Resources.Lifted_Cup_Red1;
  75.  
  76. if (turnsInt == 0);
  77. {
  78. if (randomNumberInt == 1);
  79. {
  80. redCupPicture.Image = __Shells_Game.Properties.Resources.Pea;
  81. redCupAlreadySelectedInt = redCupAlreadySelectedInt + 1;
  82. turnsInt = turnsInt + 1;
  83. winsInt = winsInt + 1;
  84. gameOverInt = gameOverInt + 1;
  85. highestWinningStreakInt = highestWinningStreakInt + 1;
  86. highestLosingStreakInt = 0;
  87. winLossLabel.Text = "You Win!";
  88. winLossLabel.ForeColor = Color.Green;
  89. instructionLabel.Text = "Game Over";
  90. }
  91. if (randomNumberInt != 1) ;
  92. {
  93. redCupPicture.Image = __Shells_Game.Properties.Resources.No_Pea;
  94. redCupAlreadySelectedInt++;
  95. turnsInt++;
  96. highestWinningStreakInt = 0;
  97. winLossLabel.Text = "You are Wrong";
  98. winLossLabel.ForeColor = Color.Orange;
  99. }
  100. }
  101.  
  102. if (turnsInt == 1) ;
  103. {
  104. if (redCupAlreadySelectedInt == 0) ;
  105. {
  106. if (randomNumberInt == 1) ;
  107. {
  108. redCupPicture.Image = __Shells_Game.Properties.Resources.Pea;
  109. redCupAlreadySelectedInt++;
  110. gameOverInt++;
  111. turnsInt++;
  112. highestWinningStreakInt = 0;
  113. highestLosingStreakInt = 0;
  114. winLossLabel.Text = "You Got It";
  115. winLossLabel.ForeColor = Color.Green;
  116. instructionLabel.Text = "Game Over";
  117. }
  118. if (randomNumberInt != 1) ;
  119. {
  120. redCupPicture.Image = __Shells_Game.Properties.Resources.No_Pea;
  121. redCupAlreadySelectedInt++;
  122. turnsInt++;
  123. gameOverInt++;
  124. lossesInt++;
  125. highestWinningStreakInt = 0;
  126. highestLosingStreakInt++;
  127. winLossLabel.Text = "You Lose";
  128. winLossLabel.ForeColor = Color.Red;
  129. instructionLabel.Text = "Game Over";
  130. }
  131. }
  132. }
  133. }
  134.  
  135. if (highestWinningStreakInt > currentHighestWinningStreakInt)
  136. {
  137. highestWinningStreakLabel.Text = highestWinningStreakInt.ToString();
  138. }
  139.  
  140. if (highestLosingStreakInt > currentHighestLosingStreakInt)
  141. {
  142. highestLosingStreakLabel.Text = highestLosingStreakInt.ToString();
  143. }
  144.  
  145. winsLabel.Text = winsInt.ToString();
  146. lossesLabel.Text = lossesInt.ToString();
  147.  
  148. }
  149.  
  150. private void greenCupPicture_Click(object sender, EventArgs e)
  151. {
  152. if (gameOverInt == 0);
  153. {
  154. greenCupLiftedPicture.Image = __Shells_Game.Properties.Resources.Lifted_Cup_Green1;
  155.  
  156. if (turnsInt == 0);
  157. {
  158. if (randomNumberInt == 1);
  159. {
  160. greenCupPicture.Image = __Shells_Game.Properties.Resources.Pea;
  161. greenCupAlreadySelectedInt++;
  162. turnsInt++;
  163. winsInt++;
  164. gameOverInt++;
  165. highestWinningStreakInt++;
  166. highestLosingStreakInt = 0;
  167. winLossLabel.Text = "You Win!";
  168. winLossLabel.ForeColor = Color.Green;
  169. instructionLabel.Text = "Game Over";
  170. }
  171. if (randomNumberInt > 1);
  172. {
  173. greenCupPicture.Image = __Shells_Game.Properties.Resources.No_Pea;
  174. greenCupAlreadySelectedInt++;
  175. turnsInt++;
  176. highestWinningStreakInt = 0;
  177. winLossLabel.Text = "You are Wrong";
  178. winLossLabel.ForeColor = Color.Orange;
  179. }
  180. }
  181.  
  182. if (turnsInt == 1);
  183. {
  184. if (greenCupAlreadySelectedInt == 0);
  185. {
  186. if (randomNumberInt == 1);
  187. {
  188. greenCupPicture.Image = __Shells_Game.Properties.Resources.Pea;
  189. greenCupAlreadySelectedInt++;
  190. gameOverInt++;
  191. turnsInt++;
  192. highestWinningStreakInt = 0;
  193. highestLosingStreakInt = 0;
  194. winLossLabel.Text = "You Got It";
  195. winLossLabel.ForeColor = Color.Green;
  196. instructionLabel.Text = "Game Over";
  197. }
  198. if (randomNumberInt > 1);
  199. {
  200. greenCupPicture.Image = __Shells_Game.Properties.Resources.No_Pea;
  201. greenCupAlreadySelectedInt++;
  202. turnsInt++;
  203. gameOverInt++;
  204. lossesInt++;
  205. highestWinningStreakInt = 0;
  206. highestLosingStreakInt++;
  207. winLossLabel.Text = "You Lose";
  208. winLossLabel.ForeColor = Color.Red;
  209. instructionLabel.Text = "Game Over";
  210. }
  211. }
  212. }
  213. }
  214.  
  215. if (highestWinningStreakInt > currentHighestWinningStreakInt)
  216. {
  217. highestWinningStreakLabel.Text = highestWinningStreakInt.ToString();
  218. }
  219.  
  220. if (highestLosingStreakInt > currentHighestLosingStreakInt)
  221. {
  222. highestLosingStreakLabel.Text = highestLosingStreakInt.ToString();
  223. }
  224.  
  225. winsLabel.Text = winsInt.ToString();
  226. lossesLabel.Text = lossesInt.ToString();
  227. }
  228.  
  229. private void blueCupPicture_Click(object sender, EventArgs e)
  230. {
  231. if (gameOverInt == 0);
  232. {
  233. blueCupLiftedPicture.Image = __Shells_Game.Properties.Resources.Lifted_Cup_Blue1;
  234.  
  235. if (turnsInt == 0);
  236. {
  237. if (randomNumberInt == 1);
  238. {
  239. blueCupPicture.Image = __Shells_Game.Properties.Resources.Pea;
  240. blueCupAlreadySelectedInt++;
  241. turnsInt++;
  242. winsInt++;
  243. gameOverInt++;
  244. highestWinningStreakInt++;
  245. highestLosingStreakInt = 0;
  246. winLossLabel.Text = "You Win!";
  247. winLossLabel.ForeColor = Color.Green;
  248. instructionLabel.Text = "Game Over";
  249. }
  250. if (randomNumberInt > 1);
  251. {
  252. blueCupPicture.Image = __Shells_Game.Properties.Resources.No_Pea;
  253. blueCupAlreadySelectedInt++;
  254. turnsInt++;
  255. highestWinningStreakInt = 0;
  256. winLossLabel.Text = "You are Wrong";
  257. winLossLabel.ForeColor = Color.Orange;
  258. }
  259. }
  260.  
  261. if (turnsInt == 1);
  262. {
  263. if (blueCupAlreadySelectedInt == 0);
  264. {
  265. if (randomNumberInt == 1);
  266. {
  267. blueCupPicture.Image = __Shells_Game.Properties.Resources.Pea;
  268. blueCupAlreadySelectedInt++;
  269. gameOverInt++;
  270. turnsInt++;
  271. highestWinningStreakInt = 0;
  272. highestLosingStreakInt = 0;
  273. winLossLabel.Text = "You Got It";
  274. winLossLabel.ForeColor = Color.Green;
  275. instructionLabel.Text = "Game Over";
  276. }
  277. if (randomNumberInt > 1) ;
  278. {
  279. blueCupPicture.Image = __Shells_Game.Properties.Resources.No_Pea;
  280. blueCupAlreadySelectedInt++;
  281. turnsInt++;
  282. gameOverInt++;
  283. lossesInt++;
  284. highestWinningStreakInt = 0;
  285. highestLosingStreakInt++;
  286. winLossLabel.Text = "You Lose";
  287. winLossLabel.ForeColor = Color.Red;
  288. instructionLabel.Text = "Game Over";
  289. }
  290. }
  291. }
  292. }
  293. if (highestWinningStreakInt > currentHighestWinningStreakInt)
  294. {
  295. highestWinningStreakLabel.Text = highestWinningStreakInt.ToString();
  296. }
  297.  
  298. if (highestLosingStreakInt > currentHighestLosingStreakInt)
  299. {
  300. highestLosingStreakLabel.Text = highestLosingStreakInt.ToString();
  301. }
  302.  
  303. winsLabel.Text = winsInt.ToString();
  304. lossesLabel.Text = lossesInt.ToString();
  305. }
  306.  
  307. private void redCupLiftedPicture_Click_1(object sender, EventArgs e)
  308. {
  309.  
  310. }
  311.  
  312. private void greenCupLiftedPicture_Click(object sender, EventArgs e)
  313. {
  314.  
  315. }
  316.  
  317. private void blueCupLiftedPicture_Click(object sender, EventArgs e)
  318. {
  319.  
  320. }
  321.  
  322. private void instructionsButton_Click(object sender, EventArgs e)
  323. {
  324.  
  325. }
  326.  
  327. private void aboutMeButton_Click(object sender, EventArgs e)
  328. {
  329.  
  330. }
  331.  
  332. private void randomInfoButton_Click(object sender, EventArgs e)
  333. {
  334.  
  335. }
  336.  
  337. private void exitButton_Click(object sender, EventArgs e)
  338. {
  339. this.Close();
  340. }
  341. }
  342. }

and the test program

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace Testing_Logic
  10. {
  11. public partial class Form1 : Form
  12. {
  13. int winssInt = 0;
  14. int lossessInt = 0;
  15. int winsInt;
  16. int lossesInt;
  17. int turnsInt;
  18. int ggInt;
  19. int cwinssInt;
  20. int clossessInt;
  21. int rcselectInt;
  22. int gcselectInt;
  23. int bcselectInt;
  24. int rndmInt;
  25.  
  26. public Form1()
  27. {
  28. InitializeComponent();
  29. }
  30.  
  31. private void exit_Click(object sender, EventArgs e)
  32. {
  33. this.Close();
  34. }
  35.  
  36. private void newgame_Click(object sender, EventArgs e)
  37. {
  38. Random generateRandom = new Random();
  39. rndmInt = generateRandom.Next(1, 4);
  40. cwinssInt = int.Parse(cwinssl.Text);
  41. clossessInt = int.Parse(clossessl.Text);
  42. winsInt = int.Parse(winsl.Text);
  43. lossesInt = int.Parse(lossesl.Text);
  44. turnsInt = 0;
  45. ggInt = 0;
  46. rcselectInt = 0;
  47. gcselectInt = 0;
  48. bcselectInt = 0;
  49.  
  50. winsl.Text = winsInt.ToString();
  51. lossesl.Text = lossesInt.ToString();
  52. rndm.Text = rndmInt.ToString();
  53. gg.Text = ggInt.ToString();
  54. rcselect.Text = rcselectInt.ToString();
  55. gcselect.Text = gcselectInt.ToString();
  56. bcselect.Text = bcselectInt.ToString();
  57. turns.Text = turnsInt.ToString();
  58. clossess.Text = clossessInt.ToString();
  59. cwinss.Text = cwinssInt.ToString();
  60. winss.Text = winssInt.ToString();
  61. lossess.Text = lossessInt.ToString();
  62.  
  63. }
  64.  
  65. private void red_Click(object sender, EventArgs e)
  66. {
  67. if (ggInt == 0);
  68. {
  69. if (turnsInt == 0);
  70. {
  71. if (rndmInt == 1) ;
  72. {
  73. rcselectInt++;
  74. turnsInt++;
  75. winsInt++;
  76. ggInt++;
  77. winssInt++;
  78. lossessInt = 0;
  79. }
  80. if (rndmInt != 1) ;
  81. {
  82. rcselectInt++;
  83. turnsInt++;
  84. winssInt = 0;
  85. }
  86. }
  87.  
  88. if (turnsInt == 1) ;
  89. {
  90. if (rcselectInt == 0) ;
  91. {
  92. if (rndmInt == 1) ;
  93. {
  94. rcselectInt++;
  95. ggInt++;
  96. turnsInt++;
  97. winssInt = 0;
  98. winssInt = 0;
  99. }
  100.  
  101. if (rndmInt != 1) ;
  102. {
  103. rcselectInt++;
  104. turnsInt++;
  105. ggInt++;
  106. lossesInt++;
  107. winssInt = 0;
  108. lossessInt++;
  109. }
  110. }
  111. }
  112. }
  113.  
  114. if (winssInt > cwinssInt)
  115. {
  116. cwinssl.Text = winssInt.ToString();
  117. }
  118.  
  119. if (lossessInt > clossessInt)
  120. {
  121. clossessl.Text = lossessInt.ToString();
  122. }
  123.  
  124. winsl.Text = winsInt.ToString();
  125. lossesl.Text = lossesInt.ToString();
  126. rndm.Text = rndmInt.ToString();
  127. gg.Text = ggInt.ToString();
  128. rcselect.Text = rcselectInt.ToString();
  129. gcselect.Text = gcselectInt.ToString();
  130. bcselect.Text = bcselectInt.ToString();
  131. turns.Text = turnsInt.ToString();
  132. clossess.Text = clossessInt.ToString();
  133. cwinss.Text = cwinssInt.ToString();
  134. winss.Text = winssInt.ToString();
  135. lossess.Text = lossessInt.ToString();
  136. }
  137.  
  138. private void green_Click(object sender, EventArgs e)
  139. {
  140. if (ggInt == 0) ;
  141. {
  142. if (turnsInt == 0) ;
  143. {
  144. if (rndmInt == 1) ;
  145. {
  146. gcselectInt++;
  147. turnsInt++;
  148. winsInt++;
  149. ggInt++;
  150. winssInt++;
  151. lossessInt = 0;
  152. }
  153. if (rndmInt != 1) ;
  154. {
  155. gcselectInt++;
  156. turnsInt++;
  157. winssInt = 0;
  158. }
  159. }
  160.  
  161. if (turnsInt == 1) ;
  162. {
  163. if (gcselectInt == 0) ;
  164. {
  165. if (rndmInt == 1) ;
  166. {
  167. gcselectInt++;
  168. ggInt++;
  169. turnsInt++;
  170. winssInt = 0;
  171. winssInt = 0;
  172. }
  173.  
  174. if (rndmInt != 1) ;
  175. {
  176. gcselectInt++;
  177. turnsInt++;
  178. ggInt++;
  179. lossesInt++;
  180. winssInt = 0;
  181. lossessInt++;
  182. }
  183. }
  184. }
  185. }
  186.  
  187. if (winssInt > cwinssInt)
  188. {
  189. cwinssl.Text = winssInt.ToString();
  190. }
  191.  
  192. if (lossessInt > clossessInt)
  193. {
  194. clossessl.Text = lossessInt.ToString();
  195. }
  196.  
  197. winsl.Text = winsInt.ToString();
  198. lossesl.Text = lossesInt.ToString();
  199. rndm.Text = rndmInt.ToString();
  200. gg.Text = ggInt.ToString();
  201. rcselect.Text = rcselectInt.ToString();
  202. gcselect.Text = gcselectInt.ToString();
  203. bcselect.Text = bcselectInt.ToString();
  204. turns.Text = turnsInt.ToString();
  205. clossess.Text = clossessInt.ToString();
  206. cwinss.Text = cwinssInt.ToString();
  207. winss.Text = winssInt.ToString();
  208. lossess.Text = lossessInt.ToString();
  209. }
  210.  
  211. private void blue_Click(object sender, EventArgs e)
  212. {
  213. if (ggInt == 0) ;
  214. {
  215. if (turnsInt == 0) ;
  216. {
  217. if (rndmInt == 1) ;
  218. {
  219. bcselectInt++;
  220. turnsInt++;
  221. winsInt++;
  222. ggInt++;
  223. winssInt++;
  224. lossessInt = 0;
  225. }
  226. else if (rndmInt != 1);
  227. {
  228. bcselectInt++;
  229. turnsInt++;
  230. winssInt = 0;
  231. }
  232. }
  233.  
  234. if (turnsInt == 1) ;
  235. {
  236. if (bcselectInt == 0) ;
  237. {
  238. if (rndmInt == 1) ;
  239. {
  240. bcselectInt++;
  241. ggInt++;
  242. turnsInt++;
  243. winssInt = 0;
  244. winssInt = 0;
  245. }
  246.  
  247. if (rndmInt != 1) ;
  248. {
  249. bcselectInt++;
  250. turnsInt++;
  251. ggInt++;
  252. lossesInt++;
  253. winssInt = 0;
  254. lossessInt++;
  255. }
  256. }
  257. }
  258. }
  259.  
  260. if (winssInt > cwinssInt)
  261. {
  262. cwinssl.Text = winssInt.ToString();
  263. }
  264.  
  265. if (lossessInt > clossessInt)
  266. {
  267. clossessl.Text = lossessInt.ToString();
  268. }
  269.  
  270. winsl.Text = winsInt.ToString();
  271. lossesl.Text = lossesInt.ToString();
  272. rndm.Text = rndmInt.ToString();
  273. gg.Text = ggInt.ToString();
  274. rcselect.Text = rcselectInt.ToString();
  275. gcselect.Text = gcselectInt.ToString();
  276. bcselect.Text = bcselectInt.ToString();
  277. turns.Text = turnsInt.ToString();
  278. clossess.Text = clossessInt.ToString();
  279. cwinss.Text = cwinssInt.ToString();
  280. winss.Text = winssInt.ToString();
  281. lossess.Text = lossessInt.ToString();
  282. }
  283. }
  284. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 2
Reputation: I3loodTeeth is an unknown quantity at this point 
Solved Threads: 0
I3loodTeeth I3loodTeeth is offline Offline
Newbie Poster

Re: Beginner having trouble with nested if statements

 
0
  #2
Apr 13th, 2008
ok never mind guys,
after carefully examining my code and the text book, i finally found the problem. I was putting a ; after the if statement on that same line, thats what stuffed up my code . soo much for drumming into my head the need to put a ; at the end of every line.

solution for those who are interested

BAD IF STATEMENT
if (condition);
{
method;
}

GOOD IF STATEMENT

if (condition) //"no ; here"
{
method;
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC