Daniel Stuart wrote
So I made a really simple console command. I know its really simple but I actually had fun.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
String name1 = "Death";
String name2 = "famine";
String name3 = "War";
String weapon1 = "syth";
String weapon2 = "flies";
String weapon3 = "nukes";
Console.WriteLine("Name: " + name1 + " " + name2 + " " + name3);
Console.WriteLine("Choose your player:");
name1 = Console.ReadLine();
Console.WriteLine("Name: " + weapon1 + " " + weapon2 + " " + weapon3);
Console.WriteLine("Choose your weapon:");
weapon1 = Console.ReadLine();
}
}
}
Nice start. Allow me to make a few comments and offer you a challenge.
Comments:
1. I generally try to avoid using all sorts of
using directives for namespaces that I won't be needing. You can always include a
using ... as and when it becomes necessary. In this example it is really no big issue as the code is small and easy to follow but when you have large files of code it becomes difficult to see at a glance which namespaces are really necessary and which not. In this example however, there is no need to include any of the namespaces except for
System.
2. While breaking up your code into readable blocks is good, as you've done, try not to add too many superfluous empty lines. There's nothing wrong with it per se but it sometimes makes the code less readable if there is a large amount of empty space. Like I said, nothing wrong with your code, but I wouldn't have left such a big empty space between the section that reads the player name and the section that reads the weapon. One empty line would've been enough.
3. Coding is all about being precise. I've seen students spending hours trying to figure out why their code wouldn't compile or why it compiles but the runtime results are not what they expected. Ultimately it boiled down to the absence of a single semicolon or a misplaced quotation mark. Your syntax is quite correct, but I'm just mentioning this now, because I have a sneaky suspicion that you're going to be confronted with a certain lax of precision when it comes to the challenge below. I'm not saying more about it just yet. ?
4. I see you use the type
String (which of course is System.String) to declare your variables. The more correct usage would be to use
string (with a lower case s). Of course, string is simply an alias of System.String so it compiles in the CLR to exactly the same thing which is why your code compiles and works. However, the idea is to use C# keywords when writing C# code, as opposed to the general CLR primitive types.
The only case, that jumps to mind, where the primitive type is usually used is when referencing static methods such as String.Compare(), String.Format() or String.Empty.
Like I said, using the primitive type is not wrong but it is unusual and your code will look very different from that of your colleagues if you were to do it this way. There is one case thought where you absolutely must use the C# keywords, when defining the underlying type of an enumerated collection. This does not apply to
string of course as an enumerated type cannot be of underlying type
string. You can define an enumerated type of
int but not of
System.Int32 (which is the primitive type for which
int is the alias).
For example, the following piece of code would not compile:
public enum MyEnum : Int
{
One,
Two,
Three
}
But the following piece of code will compile:
public enum MyEnum : int
{
One,
Two,
Three
}
OK, but that's a lot of technical rambling. Don't break your head about it all. I'd just recommend you use the C# keywords as opposed to the primitive types.
Now for the challenge:
1. In your code, the user is presented by three names when prompted for his/hers, yet the user can enter ANY name after the prompt. Change your code so that the user is prompted again for his/her name until he/she enters one of the three valid names.
Tip: You might want to read up on the
while loop and the
do-while loop for this.
2. Similarly, change your code to allow only one of the three valid weapons to be chosen.
3. Once the user as chosen his/her name and weapon, write a message to the screen as follows:
"Good day
xxx and welcome to the arena. I see you have chosen
yyy as your weapon today. Good luck!"
Of course
xxx is replaced by the player's name and
yyy by the player's weapon.
Once you've done this, I'll present you with a further challenge ?