11 Replies - 1694 Views - Last Post: 28 March 2012 - 10:18 AM Rate Topic: -----

#1 snafu117  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-March 12

TRYING to display even numbers of 100 random numbers between 0 & 1

Posted 27 March 2012 - 12:19 PM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace randomNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            Random numb = new Random();      
            int n = numb.Next(1000);          
            string r;

            string results = "";              
            int counter = 0;

           


        do
        {
                n = numb.Next(1000);
                r = Convert.ToString(n);
            counter++;
            results += counter + ")\t" + r + "\t";
        }

            
                for (int i=2; i <= 1000; i=i+2) //THIS IS THE PART OF THE CODE THATS SUPPOSED TO DISPLAY THE EVEN
                Console.WriteLine(i);           // NUMBERS OF THE 100 RANDOMS, BUT I KEEP GETTING ERROrs
                                                // what am i doing wrong?
        
            
        while (counter < 100&&n<1000);

        

            MessageBox.Show(results, "Random Numbers" , MessageBoxButtons.OK);



        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: TRYING to display even numbers of 100 random numbers between 0 & 1

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9368
  • View blog
  • Posts: 35,195
  • Joined: 12-June 08

Re: TRYING to display even numbers of 100 random numbers between 0 & 1

Posted 27 March 2012 - 12:22 PM

Quote

//THIS IS THE PART OF THE CODE THATS SUPPOSED TO DISPLAY THE EVEN
// NUMBERS OF THE 100 RANDOMS, BUT I KEEP GETTING ERROrs
// what am i doing wrong?


It's always good to share your errors your are getting..
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4526
  • View blog
  • Posts: 7,894
  • Joined: 08-June 10

Re: TRYING to display even numbers of 100 random numbers between 0 & 1

Posted 27 March 2012 - 12:27 PM

"between 0 & 1"

...there are no numbers between 0 and 1.
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: TRYING to display even numbers of 100 random numbers between 0 & 1

Posted 27 March 2012 - 12:38 PM

Unless they mean 100 random decimal numbers between 0 and 1?

In which case you want to be using the .NextDouble method instead of .Next.

But then again they seem to be wanting to find all the even numbers, in which case using decimals wouldn't work.

This post has been edited by Ryano121: 27 March 2012 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9368
  • View blog
  • Posts: 35,195
  • Joined: 12-June 08

Re: TRYING to display even numbers of 100 random numbers between 0 & 1

Posted 27 March 2012 - 12:42 PM

Bah.. there are plenty of numbers between 0 and 1.. not many of them are WHOLE numbers but they are numbers regardless of their handicaps..
Was This Post Helpful? 0
  • +
  • -

#6 snafu117  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-March 12

Re: TRYING to display even numbers of 100 random numbers between 0 & 1

Posted 27 March 2012 - 02:23 PM

they are not decimals, they are integers. the code finds 100 random integers between 0 and 1000, this part of the code is done, now im trying to sort the even numbers into a separate column in the message box that appears after i run the code, i keep getting syntax errors, not exactly sure what the syntax errors are but there are an error for the for and while
Was This Post Helpful? 0
  • +
  • -

#7 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: TRYING to display even numbers of 100 random numbers between 0 & 1

Posted 27 March 2012 - 02:28 PM

What do you mean you don't know what the error messages are?

Copy and paste the errors you get when you try to compile in Visual Studio and post them here.
Was This Post Helpful? 0
  • +
  • -

#8 snafu117  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-March 12

Re: TRYING to display even numbers of 100 random numbers between 0 & 1

Posted 27 March 2012 - 02:37 PM

"The name 'MessageBox' does not exist in the current context"

"The name 'MessageBoxButtons' does not exist in the currentcontext"


looks like the errors for "for" and "while" have went away. anyway those are the messages that appear now.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,910
  • Joined: 02-June 10

Re: TRYING to display even numbers of 100 random numbers between 0 & 1

Posted 28 March 2012 - 06:37 AM

Start by cleaning up your code.

What's with lines 33 and 34 sitting between your DO and WHILE statements? If you frak up all the syntax and completely confuse the compiler you will get errors where you think you shouldn't.

Since you are using a random number to start with, just adding 2 to it doesn't guarantee its even. Use the modulus function to determin if it is even


x = myRandom.NextRandom();
if (x%2 == 0) // then its even

Was This Post Helpful? 0
  • +
  • -

#10 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: TRYING to display even numbers of 100 random numbers between 0 & 1

Posted 28 March 2012 - 08:01 AM

View Postsnafu117, on 27 March 2012 - 09:37 PM, said:

"The name 'MessageBox' does not exist in the current context"

Most of the time this error means you are missing a Using directive.

Right click on the red underlined "MessageBox" in your code, then you'll seeing an option called "Resolve" above refactor which should fill in the missing directive for you.

The other common one is trying to use something out of it's scope, for example trying to use a variable you declared in method in another separate method. (Instead of declaring it at class level so you can use it in both)

This post has been edited by negligible: 28 March 2012 - 08:04 AM

Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5571
  • View blog
  • Posts: 11,910
  • Joined: 02-June 10

Re: TRYING to display even numbers of 100 random numbers between 0 & 1

Posted 28 March 2012 - 08:21 AM

View Postnegligible, on 28 March 2012 - 09:01 AM, said:

View Postsnafu117, on 27 March 2012 - 09:37 PM, said:

"The name 'MessageBox' does not exist in the current context"

Most of the time this error means you are missing a Using directive.


There is no need for guessing about that. The OP does have the proper using directive.
05 using System.Windows.Forms;

Attached Image

Mind you he's trying to call it from a console app. I'm not sure that's even possible. Hmmm... Something to play with

Like I said... Fix your code. You can't have statements between the Do{} and While blocks.
Visual Studio is even telling you what it expected. Just follow the tooltips.

Attached Image

Yep, if you fix the stuff its telling you to fix, you are indeed able to use a MessageBox from a console app.

Attached Image
Was This Post Helpful? 0
  • +
  • -

#12 snafu117  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-March 12

Re: TRYING to display even numbers of 100 random numbers between 0 & 1

Posted 28 March 2012 - 10:18 AM

thank you all :] i think i have it figured out! ill be sure to + you guys! thanks a lot!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1