Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
==== i am trying to figure out why, when i click the spin button my code doesn't run. it freeze.

int num1;
           int num2;
           int num3;
           Random RndNumber = new Random();
           double cash;
           string addCash;
           for (; ; )
           {
               cash = Convert.ToDouble(txtAdd.Text);
               addCash = cash.ToString();
               label2.Text = addCash;
               if (label2.Text == "")
               {
                   MessageBox.Show("YOU NEED TO ADD MORE BEFORE PLAING");
                   break;
               }
               else
               {
                   num1 = RndNumber.Next(0, 10);
                   num2 = RndNumber.Next(0, 10);
                   num3 = RndNumber.Next(0, 10);

                   btnOne.Text = num1.ToString();
                   btnTwo.Text += num2.ToString();
                   btnThree.Text += num3.ToString();

                   if((btnOne.Text==btnTwo.Text)&&(btnTwo.Text==btnThree.Text))
                   {
                       cash++;
                       MessageBox.Show("Congrats! you win");
                   }
               }


What I have tried:

i am new to coding so i tried debugging.
Posted
Updated 13-May-23 4:22am
Comments
Member 15627495 13-May-23 9:43am    
Hello !

what are you expecting with the "for loop" ?
Kenneth Haugland 13-May-23 9:45am    
I think you have some logical flaws in your code. Why not check if the num1 == num2 and num2 == num3 instead?
0x01AA 13-May-23 9:51am    
Looks like you only have to wait long enough until the random number num1, num2 and num3 will be the same...
Member 15627495 13-May-23 9:59am    
your code is not freezing, but "busy state".

add the 'exit loop test' in your for( ; ; ) or use while(1){.....}

to see your program ending, you have to reach '' var1 == var 2 == var 3 'it will not happen because of strings and + operator'

but for label 2 and 3, you're adding String to your button values.

review the all label.text, to fix your code.

1 solution

C#
          long long cash = 0 ;
          Random RndNumber = new Random();

if (txtAdd.Text == "")
{
   MessageBox.Show("YOU NEED TO ADD MORE cucumber BEFORE PLAYING");
   break;
}
else
{
  while(1){ // infinite loop will proceed until the 3 output values are equal.
           btnOne.Text = RndNumber.Next(0, 10).toString();
           btnTwo.Text = RndNumber.Next(0, 10).toString();
           btnThree.Text = RndNumber.Next(0, 10).toString();

           if( btnOne.Text == btnTwo.Text && btnTwo.Text == btnThree.Text )
           {
              cash  += 120000 ; // random gain
              MessageBox.Show("Congrats! You Win");
              return 0 ;
           }

        }

 }
 
Share this answer
 
v2
Comments
0x01AA 13-May-23 10:27am    
Oh yes the "+=" i did not recognize. My 5.
Mohamed Jawo 16-May-23 10:12am    
thanks for the help, I got it figure out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900