| | |
Beginner having trouble with nested if statements
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2008
Posts: 2
Reputation:
Solved Threads: 0
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.
and the test program
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.
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace __Shells_Game { public partial class cupsGameParentForm : Form { // This is where I declare all of my background variables and assign them values int highestWinningStreakInt = 0; int highestLosingStreakInt = 0; int winsInt; int lossesInt; int turnsInt; int gameOverInt; int currentHighestWinningStreakInt; int currentHighestLosingStreakInt; int redCupAlreadySelectedInt; int greenCupAlreadySelectedInt; int blueCupAlreadySelectedInt; int randomNumberInt; public cupsGameParentForm() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } // This is the button used to select a New Game. private void newGameButton_Click(object sender, EventArgs e) { // This resets all the pictures and labels to the "New Game" state. redCupPicture.Image = __Shells_Game.Properties.Resources.Cup_Red2; greenCupPicture.Image = __Shells_Game.Properties.Resources.Cup_Green2; blueCupPicture.Image = __Shells_Game.Properties.Resources.Cup_Blue2; redCupLiftedPicture.Image = __Shells_Game.Properties.Resources.Main_Form_Background_Colour; greenCupLiftedPicture.Image = __Shells_Game.Properties.Resources.Main_Form_Background_Colour; blueCupLiftedPicture.Image = __Shells_Game.Properties.Resources.Main_Form_Background_Colour; winLossLabel.Text = " "; instructionLabel.Text = "Select a Cup"; // This is where the game selects a random numberfrom 1 to 3. Random generateRandom = new Random(); randomNumberInt = generateRandom.Next(1, 4); // This code assigns values to all the integer values. currentHighestWinningStreakInt = int.Parse(highestWinningStreakLabel.Text); currentHighestLosingStreakInt = int.Parse(highestLosingStreakLabel.Text); winsInt = int.Parse(winsLabel.Text); lossesInt = int.Parse(lossesLabel.Text); turnsInt = 0; gameOverInt = 0; redCupAlreadySelectedInt = 0; greenCupAlreadySelectedInt = 0; blueCupAlreadySelectedInt = 0; } private void redCupPicture_Click(object sender, EventArgs e) { if (gameOverInt == 0); { redCupLiftedPicture.Image = __Shells_Game.Properties.Resources.Lifted_Cup_Red1; if (turnsInt == 0); { if (randomNumberInt == 1); { redCupPicture.Image = __Shells_Game.Properties.Resources.Pea; redCupAlreadySelectedInt = redCupAlreadySelectedInt + 1; turnsInt = turnsInt + 1; winsInt = winsInt + 1; gameOverInt = gameOverInt + 1; highestWinningStreakInt = highestWinningStreakInt + 1; highestLosingStreakInt = 0; winLossLabel.Text = "You Win!"; winLossLabel.ForeColor = Color.Green; instructionLabel.Text = "Game Over"; } if (randomNumberInt != 1) ; { redCupPicture.Image = __Shells_Game.Properties.Resources.No_Pea; redCupAlreadySelectedInt++; turnsInt++; highestWinningStreakInt = 0; winLossLabel.Text = "You are Wrong"; winLossLabel.ForeColor = Color.Orange; } } if (turnsInt == 1) ; { if (redCupAlreadySelectedInt == 0) ; { if (randomNumberInt == 1) ; { redCupPicture.Image = __Shells_Game.Properties.Resources.Pea; redCupAlreadySelectedInt++; gameOverInt++; turnsInt++; highestWinningStreakInt = 0; highestLosingStreakInt = 0; winLossLabel.Text = "You Got It"; winLossLabel.ForeColor = Color.Green; instructionLabel.Text = "Game Over"; } if (randomNumberInt != 1) ; { redCupPicture.Image = __Shells_Game.Properties.Resources.No_Pea; redCupAlreadySelectedInt++; turnsInt++; gameOverInt++; lossesInt++; highestWinningStreakInt = 0; highestLosingStreakInt++; winLossLabel.Text = "You Lose"; winLossLabel.ForeColor = Color.Red; instructionLabel.Text = "Game Over"; } } } } if (highestWinningStreakInt > currentHighestWinningStreakInt) { highestWinningStreakLabel.Text = highestWinningStreakInt.ToString(); } if (highestLosingStreakInt > currentHighestLosingStreakInt) { highestLosingStreakLabel.Text = highestLosingStreakInt.ToString(); } winsLabel.Text = winsInt.ToString(); lossesLabel.Text = lossesInt.ToString(); } private void greenCupPicture_Click(object sender, EventArgs e) { if (gameOverInt == 0); { greenCupLiftedPicture.Image = __Shells_Game.Properties.Resources.Lifted_Cup_Green1; if (turnsInt == 0); { if (randomNumberInt == 1); { greenCupPicture.Image = __Shells_Game.Properties.Resources.Pea; greenCupAlreadySelectedInt++; turnsInt++; winsInt++; gameOverInt++; highestWinningStreakInt++; highestLosingStreakInt = 0; winLossLabel.Text = "You Win!"; winLossLabel.ForeColor = Color.Green; instructionLabel.Text = "Game Over"; } if (randomNumberInt > 1); { greenCupPicture.Image = __Shells_Game.Properties.Resources.No_Pea; greenCupAlreadySelectedInt++; turnsInt++; highestWinningStreakInt = 0; winLossLabel.Text = "You are Wrong"; winLossLabel.ForeColor = Color.Orange; } } if (turnsInt == 1); { if (greenCupAlreadySelectedInt == 0); { if (randomNumberInt == 1); { greenCupPicture.Image = __Shells_Game.Properties.Resources.Pea; greenCupAlreadySelectedInt++; gameOverInt++; turnsInt++; highestWinningStreakInt = 0; highestLosingStreakInt = 0; winLossLabel.Text = "You Got It"; winLossLabel.ForeColor = Color.Green; instructionLabel.Text = "Game Over"; } if (randomNumberInt > 1); { greenCupPicture.Image = __Shells_Game.Properties.Resources.No_Pea; greenCupAlreadySelectedInt++; turnsInt++; gameOverInt++; lossesInt++; highestWinningStreakInt = 0; highestLosingStreakInt++; winLossLabel.Text = "You Lose"; winLossLabel.ForeColor = Color.Red; instructionLabel.Text = "Game Over"; } } } } if (highestWinningStreakInt > currentHighestWinningStreakInt) { highestWinningStreakLabel.Text = highestWinningStreakInt.ToString(); } if (highestLosingStreakInt > currentHighestLosingStreakInt) { highestLosingStreakLabel.Text = highestLosingStreakInt.ToString(); } winsLabel.Text = winsInt.ToString(); lossesLabel.Text = lossesInt.ToString(); } private void blueCupPicture_Click(object sender, EventArgs e) { if (gameOverInt == 0); { blueCupLiftedPicture.Image = __Shells_Game.Properties.Resources.Lifted_Cup_Blue1; if (turnsInt == 0); { if (randomNumberInt == 1); { blueCupPicture.Image = __Shells_Game.Properties.Resources.Pea; blueCupAlreadySelectedInt++; turnsInt++; winsInt++; gameOverInt++; highestWinningStreakInt++; highestLosingStreakInt = 0; winLossLabel.Text = "You Win!"; winLossLabel.ForeColor = Color.Green; instructionLabel.Text = "Game Over"; } if (randomNumberInt > 1); { blueCupPicture.Image = __Shells_Game.Properties.Resources.No_Pea; blueCupAlreadySelectedInt++; turnsInt++; highestWinningStreakInt = 0; winLossLabel.Text = "You are Wrong"; winLossLabel.ForeColor = Color.Orange; } } if (turnsInt == 1); { if (blueCupAlreadySelectedInt == 0); { if (randomNumberInt == 1); { blueCupPicture.Image = __Shells_Game.Properties.Resources.Pea; blueCupAlreadySelectedInt++; gameOverInt++; turnsInt++; highestWinningStreakInt = 0; highestLosingStreakInt = 0; winLossLabel.Text = "You Got It"; winLossLabel.ForeColor = Color.Green; instructionLabel.Text = "Game Over"; } if (randomNumberInt > 1) ; { blueCupPicture.Image = __Shells_Game.Properties.Resources.No_Pea; blueCupAlreadySelectedInt++; turnsInt++; gameOverInt++; lossesInt++; highestWinningStreakInt = 0; highestLosingStreakInt++; winLossLabel.Text = "You Lose"; winLossLabel.ForeColor = Color.Red; instructionLabel.Text = "Game Over"; } } } } if (highestWinningStreakInt > currentHighestWinningStreakInt) { highestWinningStreakLabel.Text = highestWinningStreakInt.ToString(); } if (highestLosingStreakInt > currentHighestLosingStreakInt) { highestLosingStreakLabel.Text = highestLosingStreakInt.ToString(); } winsLabel.Text = winsInt.ToString(); lossesLabel.Text = lossesInt.ToString(); } private void redCupLiftedPicture_Click_1(object sender, EventArgs e) { } private void greenCupLiftedPicture_Click(object sender, EventArgs e) { } private void blueCupLiftedPicture_Click(object sender, EventArgs e) { } private void instructionsButton_Click(object sender, EventArgs e) { } private void aboutMeButton_Click(object sender, EventArgs e) { } private void randomInfoButton_Click(object sender, EventArgs e) { } private void exitButton_Click(object sender, EventArgs e) { this.Close(); } } }
and the test program
C# Syntax (Toggle Plain Text)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace Testing_Logic { public partial class Form1 : Form { int winssInt = 0; int lossessInt = 0; int winsInt; int lossesInt; int turnsInt; int ggInt; int cwinssInt; int clossessInt; int rcselectInt; int gcselectInt; int bcselectInt; int rndmInt; public Form1() { InitializeComponent(); } private void exit_Click(object sender, EventArgs e) { this.Close(); } private void newgame_Click(object sender, EventArgs e) { Random generateRandom = new Random(); rndmInt = generateRandom.Next(1, 4); cwinssInt = int.Parse(cwinssl.Text); clossessInt = int.Parse(clossessl.Text); winsInt = int.Parse(winsl.Text); lossesInt = int.Parse(lossesl.Text); turnsInt = 0; ggInt = 0; rcselectInt = 0; gcselectInt = 0; bcselectInt = 0; winsl.Text = winsInt.ToString(); lossesl.Text = lossesInt.ToString(); rndm.Text = rndmInt.ToString(); gg.Text = ggInt.ToString(); rcselect.Text = rcselectInt.ToString(); gcselect.Text = gcselectInt.ToString(); bcselect.Text = bcselectInt.ToString(); turns.Text = turnsInt.ToString(); clossess.Text = clossessInt.ToString(); cwinss.Text = cwinssInt.ToString(); winss.Text = winssInt.ToString(); lossess.Text = lossessInt.ToString(); } private void red_Click(object sender, EventArgs e) { if (ggInt == 0); { if (turnsInt == 0); { if (rndmInt == 1) ; { rcselectInt++; turnsInt++; winsInt++; ggInt++; winssInt++; lossessInt = 0; } if (rndmInt != 1) ; { rcselectInt++; turnsInt++; winssInt = 0; } } if (turnsInt == 1) ; { if (rcselectInt == 0) ; { if (rndmInt == 1) ; { rcselectInt++; ggInt++; turnsInt++; winssInt = 0; winssInt = 0; } if (rndmInt != 1) ; { rcselectInt++; turnsInt++; ggInt++; lossesInt++; winssInt = 0; lossessInt++; } } } } if (winssInt > cwinssInt) { cwinssl.Text = winssInt.ToString(); } if (lossessInt > clossessInt) { clossessl.Text = lossessInt.ToString(); } winsl.Text = winsInt.ToString(); lossesl.Text = lossesInt.ToString(); rndm.Text = rndmInt.ToString(); gg.Text = ggInt.ToString(); rcselect.Text = rcselectInt.ToString(); gcselect.Text = gcselectInt.ToString(); bcselect.Text = bcselectInt.ToString(); turns.Text = turnsInt.ToString(); clossess.Text = clossessInt.ToString(); cwinss.Text = cwinssInt.ToString(); winss.Text = winssInt.ToString(); lossess.Text = lossessInt.ToString(); } private void green_Click(object sender, EventArgs e) { if (ggInt == 0) ; { if (turnsInt == 0) ; { if (rndmInt == 1) ; { gcselectInt++; turnsInt++; winsInt++; ggInt++; winssInt++; lossessInt = 0; } if (rndmInt != 1) ; { gcselectInt++; turnsInt++; winssInt = 0; } } if (turnsInt == 1) ; { if (gcselectInt == 0) ; { if (rndmInt == 1) ; { gcselectInt++; ggInt++; turnsInt++; winssInt = 0; winssInt = 0; } if (rndmInt != 1) ; { gcselectInt++; turnsInt++; ggInt++; lossesInt++; winssInt = 0; lossessInt++; } } } } if (winssInt > cwinssInt) { cwinssl.Text = winssInt.ToString(); } if (lossessInt > clossessInt) { clossessl.Text = lossessInt.ToString(); } winsl.Text = winsInt.ToString(); lossesl.Text = lossesInt.ToString(); rndm.Text = rndmInt.ToString(); gg.Text = ggInt.ToString(); rcselect.Text = rcselectInt.ToString(); gcselect.Text = gcselectInt.ToString(); bcselect.Text = bcselectInt.ToString(); turns.Text = turnsInt.ToString(); clossess.Text = clossessInt.ToString(); cwinss.Text = cwinssInt.ToString(); winss.Text = winssInt.ToString(); lossess.Text = lossessInt.ToString(); } private void blue_Click(object sender, EventArgs e) { if (ggInt == 0) ; { if (turnsInt == 0) ; { if (rndmInt == 1) ; { bcselectInt++; turnsInt++; winsInt++; ggInt++; winssInt++; lossessInt = 0; } else if (rndmInt != 1); { bcselectInt++; turnsInt++; winssInt = 0; } } if (turnsInt == 1) ; { if (bcselectInt == 0) ; { if (rndmInt == 1) ; { bcselectInt++; ggInt++; turnsInt++; winssInt = 0; winssInt = 0; } if (rndmInt != 1) ; { bcselectInt++; turnsInt++; ggInt++; lossesInt++; winssInt = 0; lossessInt++; } } } } if (winssInt > cwinssInt) { cwinssl.Text = winssInt.ToString(); } if (lossessInt > clossessInt) { clossessl.Text = lossessInt.ToString(); } winsl.Text = winsInt.ToString(); lossesl.Text = lossesInt.ToString(); rndm.Text = rndmInt.ToString(); gg.Text = ggInt.ToString(); rcselect.Text = rcselectInt.ToString(); gcselect.Text = gcselectInt.ToString(); bcselect.Text = bcselectInt.ToString(); turns.Text = turnsInt.ToString(); clossess.Text = clossessInt.ToString(); cwinss.Text = cwinssInt.ToString(); winss.Text = winssInt.ToString(); lossess.Text = lossessInt.ToString(); } } }
•
•
Join Date: Apr 2008
Posts: 2
Reputation:
Solved Threads: 0
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;
}
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;
}
![]() |
Other Threads in the C# Forum
- Previous Thread: Back Up of SQL database in C#
- Next Thread: casting&math class in pow
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom customactiondata database datagrid datagridview dataset date/time datetime datetimepicker degrees development dll draganddrop drawing encryption enum event excel file filename files form format forms function gdi+ gis gtk hash httpwebrequest image index input install java label list listbox mandelbrot math mouseclick mysql operator outlook2003 path photoshop picturebox pixelinversion pixelminversion post programming radians regex remote remoting richtextbox server sleep socket sql statistics stream string table tables tcp text textbox thread time timer tutorial update usercontrol usercontrols validation visualstudio webbrowser webcam wia windows winforms wpf xml





