![]() |
| | #1 |
| Registered User Join Date: Mar 2006
Posts: 12
| random to int? Anyway, a friend advised me just to start writing simple C# programs and then eventually I'll "know" C#. (You know what I'm talking about) My first simple program I'm trying to write is a guessing game where the computer thinks of a random number between 1 and 100 and the human tries to guess it. Here's my code so far: Code: using System;
using System.Collections.Generic;
using System.Text;
namespace Guessing_Game
{
class Program
{
static void Main(string[] args)
{
string guess;
Console.WriteLine("Welcome to Adam Henderson's C# Guessing Game!\n");
Console.WriteLine("Guess a number from 1 to 100.");
guess = Console.ReadLine();
Random random = new Random(); // C#'s way of giving you a random number
random.Next(1, 100);
int guess2 = Int32.Parse(guess);
if (random < guess2)
Console.WriteLine("Oh! You guessed too high! Guess again!");
else if (random > guess2)
Console.WriteLine("Oh! You guessed too low! Guess again!");
else
{
Console.WriteLine("You guessed correct! The answer was {0}!", guess2);
}
}
}
}
|
| psyadam is offline | |
| | #2 |
| Registered User Join Date: Mar 2006
Posts: 12
| I think I fixed it *blush*: Code: using System;
using System.Collections.Generic;
using System.Text;
namespace Guessing_Game
{
class Program
{
static void Main(string[] args)
{
string guess;
Console.WriteLine("Welcome to Adam Henderson's C# Guessing Game!\n");
Console.WriteLine("Guess a number from 1 to 100.");
guess = Console.ReadLine();
Random random = new Random(); // C#'s way of giving you a random number
int rand = random.Next(1, 100);
int guess2 = Int32.Parse(guess);
if (rand < guess2)
Console.WriteLine("Oh! You guessed too high! Guess again!");
else if (rand > guess2)
Console.WriteLine("Oh! You guessed too low! Guess again!");
else
{
Console.WriteLine("You guessed correct! The answer was {0}!", guess2);
}
}
}
}
|
| psyadam is offline | |
| | #3 |
| Registered User Join Date: Mar 2006
Posts: 12
| Looks like I was able to complete the random number guessing game in C#. Wow, I'm spent! (Just kidding!) Anyone have an idea for the next small program I should try to make? edit: oh yeah, I noticed that I used the word "new" when I made the random number. I think my friend said that C# has a garbage collector. Does this mean I can use new without having to worry about memory leaks? |
| psyadam is offline | |
| | #4 |
| Registered User Join Date: Jun 2003
Posts: 90
| I'll give you an assignment I got in my first semester at Uni... Create a program that asks a user to answer 10 math questions. The questions should just be a simple +, -, / or x question. When the user has finished, output the unmber of questions answered correctly and give a score. At the end, ask if they'd like to go again. Suggestion for layout and a nice example: Code: 9: What is 4 x 3? 12 Correct! 10: What is 7-2? 3 Wrong! Your total score was 6/10 Would you like to play again? (imagine blinking cursor here!)
__________________ He who asks is a fool for five minutes, but he who does not ask remains a fool forever. The fool wonders, the wise man asks. - Benjamin Disraeli There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz |
| DanFraser is offline | |
| | #5 | |
| Registered User Join Date: Mar 2006
Posts: 12
| Quote:
edit: windows programming assignments would be very helpful as well edit: thinking more along the lines of pointers, writing a class for a singly linked list sounds like a good exercise Last edited by psyadam; 07-15-2008 at 12:46 PM. | |
| psyadam is offline | |
| | #6 |
| Registered User Join Date: Jun 2003
Posts: 90
| Sorry, I was looking at the code you were working on and just stepped that up a little ![]() As you're experienced with C++, I think you'll find the C# specification very easy to read and skim through! Boxing and unboxing is a thing where you learn what it does, then avoid doing it. An example is an ArrayList of a class you've made. It'll get stored as an object type in the arraylist, and you have to convert it out to it's original if you want to use it, this is where generics come in very handy. Advanced function stuff to me is pretty much the same as C++ from what I can see (not personally done that much C++ myself, but I can do it). Delegates, I've no clue either!
__________________ He who asks is a fool for five minutes, but he who does not ask remains a fool forever. The fool wonders, the wise man asks. - Benjamin Disraeli There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz |
| DanFraser is offline | |
| | #7 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| If I understand correctly, C# does not believe in pointers. So that may make life a little tricky. Maybe dealing with generics? You should never write a singly linked list in C++ (unless you're taking data structures in school, or for other abstruse purposes), and I'm guessing you should never write a singly linked list in C# either. |
| tabstop is offline | |
| | #8 |
| Registered User Join Date: Jun 2004
Posts: 9
| C# does have pointers, but they should be placed in unsafe blocks and compiled with a special flag. Delegates are C#'s rough equivalent to C/C++ function pointers. |
| Azimuth is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| memory leak | aruna1 | C++ Programming | 3 | 08-17-2008 10:28 PM |
| Function argument assignment between types "unsigned int*" and "unsigned long*" | nadeer78 | C Programming | 8 | 03-10-2008 11:57 AM |
| Working with random like dice | SebastionV3 | C++ Programming | 10 | 05-26-2006 09:16 PM |
| getting a headache | sreetvert83 | C++ Programming | 41 | 09-30-2005 05:20 AM |
| How do you search & sort an array? | sketchit | C Programming | 30 | 11-03-2001 05:26 PM |