10 Replies - 340 Views - Last Post: 09 February 2013 - 12:10 PM Rate Topic: -----

#1 ViscountViktor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-February 13

how do i display the numbers

Posted 09 February 2013 - 10:01 AM

Hi, I want to create a program where 5 people enter there name, and the computer randomly picks a name and says they have won. I have tried it with a for loop, but I have never been good with for loops. I don not want an integer to appear, I want their input! This is my code:

string[] name = new string[4];

            Console.WriteLine("Please enter name 1");
            name[0] = Console.ReadLine();

            Console.WriteLine("Please enter name 2");

            name[1] = Console.ReadLine();

            Console.WriteLine("Please enter name 3");

            name[2] = Console.ReadLine();

            Console.WriteLine("Please enter name 4");

            name[3] = Console.ReadLine();

            Console.WriteLine("Please enter name 5");

            name[4] = Console.ReadLine();



            for (int i = 0; i < []name; i++) ;

            {
                Console.WriteLine("Well done" + name[i] + " , you are the winner");

            }
            Console.ReadLine();



Any improvements to the for loop to make it display the above wishes?

Is This A Good Question/Topic? 0
  • +

Replies To: how do i display the numbers

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9197
  • View blog
  • Posts: 34,551
  • Joined: 12-June 08

Re: how do i display the numbers

Posted 09 February 2013 - 10:07 AM

24	            for (int i = 0; i < []name; i++) ;
25	 
26	            {
27	                Console.WriteLine("Well done" + name[i] + " , you are the winner");
28	 
29	            }



Line 24 - you end with a semicolon - that's bad. See that means that for loop is done *THERE*.. and never gets to the body in 26-29. So remove that.

A for loop does what? It counts. So if you start with 0 and your condition is probably going to be the maximum number of elements in 'name'. I am not sure what you are doing with "[]name" but that does not get the _COUNT_ you are looking for. See what happens when you remove the '[]' and after 'name' put a period. Does anything in the intellisense look like a count?

Just a heads up - this for loop won't get a random person.. it will just print out everyone's name. There's a namespace 'random' you would want to use.
Was This Post Helpful? 0
  • +
  • -

#3 ViscountViktor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-February 13

Re: how do i display the numbers

Posted 09 February 2013 - 11:10 AM

I have created this program to display 1000 DIFFERENT numbers, however, I am getting 1000 numbers, but made up of only 3 different numbers. Could anyone suggest a way to improve the code so it shows more than 3 different numbers, but not necceserily 1000.


string[] student = new string[1000];

            
            
            for (int j = 0; j<=10; j++)
            
                
            {
                Random random = new Random();

                int x = random.Next(1, 100); 
            
                
                for (int i = 0; i <= 100; i++)
            
            {
                
                Console.WriteLine(x);
            
            }
           
            }
            Console.ReadLine();


Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9197
  • View blog
  • Posts: 34,551
  • Joined: 12-June 08

Re: how do i display the numbers

Posted 09 February 2013 - 11:19 AM

Read outloud what you have there..

Create a string array called 'student'.

Start a for loop(1) with 'j' at zero and go until j is less than or equal to 10

In each iteration of the loop 1:
Create a random object named 'random'.
create an integer X that gets a random number from 1 to 99

Start another for loop(2) with 'i' as 0 and go until i is less than or equal to 100
each iteration of loop 2:
print 'x'
end loop 2
end loop 1.


That's just whack, right? Why are you printing 'x' a hundred times in loop 2? It's going to be the same number over and over again.. a hundred times..

Why not have *ONE* forloop that goes to 1000 instead of two that do 10 loops, each doing 100?
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2113
  • View blog
  • Posts: 3,235
  • Joined: 21-June 11

Re: how do i display the numbers

Posted 09 February 2013 - 11:20 AM

You call Next once per iteration of the outer loop. Then you print the number returned by Next 100 times. Since the outer loop iterates 10 times, this means that you should get 10 random numbers, each repeated 100 times.

In addition to that you create a new Random object each time you call Next. This is bad. When a Random object is created without giving an argument to the constructor, the random number generated is seeded with the current time (in milliseconds, I believe). So if you create two Random objects within the same millisecond, both objects will produce the same sequence of random numbers. You should create only a single Random object outside of the loop and then just call Next on it inside the loop.
Was This Post Helpful? 0
  • +
  • -

#6 ViscountViktor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-February 13

Re: how do i display the numbers

Posted 09 February 2013 - 11:27 AM

I am only 13, could someone explain to me in simple terms what I should do?
Was This Post Helpful? 0
  • +
  • -

#7 ViscountViktor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 09-February 13

Re: how do i display the numbers

Posted 09 February 2013 - 11:47 AM

This code will create some random numbers, but how do I get it to display them, console.readline dosen't work.


 int[] numbers = new int[1000];

            Random random = new Random();

            for (int i = 0; i <= 99; i++)
            {
                numbers[i] = random.Next(1, 100);
                
            }

            Console.ReadLine();


Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is online

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

Reputation: 5513
  • View blog
  • Posts: 11,818
  • Joined: 02-June 10

Re: how do i display the numbers

Posted 09 February 2013 - 11:49 AM

LIne 9 - you shouldn't NOT keep creating a new instance of Random.
Just make one of these. Then you can get as many .Next as you want. But when you create a new random then suddenly get a new .Next with no time between you reduce the possibility of what random number will be picked. That's because of *how* random works by seeding the random number generator and the time between when it is created and the time when a number is requested.

Quote

I am only 13, could someone explain to me in simple terms what I should do?


You should stop trying to design a program until you learn more about the C# language. For now just concentrate on learning and doing exactly what the book you are following tells you to do.



My standard beginner resources post - Updated JAN 2013


Plan your study route:
There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

  • First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover.
  • Do a dozen on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.
  • Learn to plan before you type.
  • THEN you start designing software with a purpose.



I don't learn from reading books: I learn by doing.
Spoiler


Newbie/Rookie said:

I have little/no programming experience but I need to write a program by Friday that does XYZ.
Spoiler



Resources, references and suggestions for new programmers.
Spoiler

Was This Post Helpful? 1
  • +
  • -

#9 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: how do i display the numbers

Posted 09 February 2013 - 11:58 AM

Of course it doesn't. It Reads a line - doesn't write anything out.

Instead you want Console.WriteLine();

so in your loop you could simply do

Console.WriteLine(numbers[i]);


or construct another loop that iterates through the array afterwards and put the writeline there.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is online

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

Reputation: 5513
  • View blog
  • Posts: 11,818
  • Joined: 02-June 10

Re: how do i display the numbers

Posted 09 February 2013 - 11:58 AM

STOP

STOP

STOP

Kid, I love that you are trying to learn but you are nearly spamming the site with numerous thread all on the same thing.

Get a book. Follow it.

I'm going to merge all these threads.
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is online

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

Reputation: 5513
  • View blog
  • Posts: 11,818
  • Joined: 02-June 10

Re: how do i display the numbers

Posted 09 February 2013 - 12:10 PM

View PostViscountViktor, on 09 February 2013 - 12:47 PM, said:

This code will create some random numbers, but how do I get it to display them, console.readline dosen't work.


View PostRyano121, on 09 February 2013 - 12:58 PM, said:

Of course it doesn't. It Reads a line - doesn't write anything out.

Instead you want Console.WriteLine();


Viktor - We can't teach you every command in C#. You need to get a book and follow it, or go through a course at school or on-line. Its wonderful that you want to learn to code. And we all think its wonderful to see youngsters taking this on. We were the same way when we were your age. But we just don't have the resources to help 2 billion students learn it from day one, especially when there are books and other resources out there design for that.

As another expert here told another student (so well that he deserves credit for it):

View PostCurtis Rutland, on 09 July 2012 - 01:38 PM, said:

I hate to be the bearer of bad news, but there's a fundamental problem here: we're not a "intro to programming class" and we never can be. We can help you with specific problems, but what you're missing is the basic foundation. Go back to your instructor and explain to him/her that you don't understand and need help. Talk to your classmates. Find someone who can go over the book with you, in person, so you can understand what you are missing.

Because this will never work. You're throwing code at the wall to see what sticks. You make changes, but you don't know why, you're just trying and seeing what works and doesn't, and using us as an interactive compiler.

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1