Jump to content
xisto Community
Sign in to follow this  
oval1405241520

Lesson #1 - Introduction To C# Console Application

Recommended Posts

This tutorial will help get you started with C# Sharp. These tutorials will assume that you are using the Visual Studio environment. If you are just curious to test out some code, then you can copy and paste this code into your CS file, however, before doing so, make sure to name your project the same as the namespace on the code. But since this first program is fairly easy, it will not hurt to type it out. It is fairly easy and short. Also, this program is highly commented so it should be easy to follow along. Comments will have 2 forward slashes before them '//' trailing with the comment.

 

Well before looking at the code, I just want to summarize what the following lines of code will do.

 

Console.WriteLine(); <----- Writes a string of characters on the console in it's own line.

 

So writing Console.WriteLine("Hello World"); would result in Hello World displaying on your console and have the cursor move to the next line down.

 

Console.Write("Hello World"); would also display Hello World however the cursor would be next to the word and not the line below.

 

Console.ReadLine(); <------ This nifty method will allow you to take in user input. Whether the user input is numeric or alphabetic, it will take the input as a string or text. So you cannot use it in any mathematical operations right away. That is if the user was to enter 5 and you want to add 4 to that 5, you could not, right off the bat anyway. However we will go into that later.

 

Okay, now observer the code below:

using System;

 

==================== BEGIN C# CODE ===================================== namespace testProg { 	 ///<summary> 	 /// Test Programming 	 ///</summary> 	 class helloProg 	 {  			[STAThread] 			static void Main(string[] args) 			{ 				 //Declare a String variable. Name it userName 				 string userName; 				 Console.Write("Console Asks - Please Enter your Name: "); 				 //Place the user input into the variable userName with 				 //Console.ReadLine() 				 userName = Console.ReadLine(); 				 //Skip a line for nicer formatting 				 Console.WriteLine(""); 				//On the next line, display the input. 				 //{0} is where the variable value will go. It 				 //is followed by a comma and variable name userName 				 Console.WriteLine("Hello {0}!", userName); 				 Console.WriteLine("Press Enter to Continue"); 				 //The Console.Readline will pause the program until you 				 //press [ENTER]. Then it will exit. 				 Console.ReadLine();   			} //end main	 } //end class } //end namespace=========================== END C# CODE ================================

Okay aside from the information I provided before the code, I think the comments explain pretty much what is going on within this program. However, I want to further elaborate how we placed the user input into the variable userName. By setting first we declared userName as a string (i.e. string userName;) Later in the program, we said userName is equal to what the user enters, userName = Console.ReadLine(); This is what did the trick. now we just output user name in one of our Console.WriteLine's and Voila. Modify the variables, add lines and variables and get acquainted with it yourself. I'll be back soon to provide another lesson.

 

By the way, I hope I am able to upload my attachment. The code on this one looks kinda sloppy. It's in .pdf format.

Share this post


Link to post
Share on other sites

c# console application question (card game)

Lesson #1 - Introduction To C#

 

I want to know about how to do about the following question.

 

 

Create a simple GUI application to simulate a two player Game. A detailed description of the requirements follows:

 

Player 1 makes a guess. It compares with a pseudo random number. If they match, player 1 wins and the game is over. In case of a mismatch, player 1 passes the random number to player 2 and a challenge begins. Player 2 also makes a guess and then compares the number with the random number it has received. If the numbers match, player 2 wins the challenge and the game is over. In a case of a mismatch player 2 discards the random number and generates another random number to compare with the guessed number. If they match player 2 wins the challenge and the game is over. In the case of a mismatch, player 2 passes the random number to player 1 and a challenge begins. The sequence is to be repeated till a winner emerges.

 

Create player 1 using .Net technology and player 2 using Java technology to simulate the game and use a XML file to establish the communication between the players. You should be able to display the current player in both interfaces with appropriate messages to display the winning player and to indicate the game is over.

 

 

 

-reply by Buddhika

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.