Am I Ready?

Started by
42 comments, last by melloorr 12 years, 8 months ago
Thanks! I finally done it! Well im pretty sure i have :)

Woohoo! Good job! =)

A couple of things:

1.
for (int i = 0; i < s; i++)
{
Console.Write(name);
Console.ReadKey(true);
continue; // this is redundant
}

When the keyword [font="Courier New"]continue[/font] is encountered the program will execute the 'update statement' in the for-loop (in this case [font="Courier New"]'i++'[/font]) and jump to the beginning of the loop. In effect, the code below the continue statement will be skipped. In your case, however, there is no code below the continue statement which makes it redundant. Typically you use a continue statement when you do not want to process certain elements in a list/array:


for (int i = 0; i < s; i++)
{
if(Objects.paintMe==false)
continue;

PaintObject(Objects);
}


2.
int n = nameBack.Length;

for (int i = 0; i < n; i++)
{
Console.Write(nameBack[nameBack.Length -i -1]);
}

The idea of saving the length of the string at the beginning is to ensure you are always referencing the original length. Also, when you only have a single statement in your loop you don't need the curly brackets:


int n = nameBack.Length;

for (int i = 0; i < n; i++)
Console.Write(nameBack[n-i-1]); // notice that we are now using 'n' instead of 'nameBack.Length'

.. or, if you want to be really hardcore:


int n = nameBack.Length-1;

for(;n>=0;n--)
Console.Write(nameBack[n]);
Advertisement
I knew there would be things i didnt need, but i was just glad that i did it! :)

I have now done all those tasks (but i was unsure about the basic database, here is what I got though).

It may be a little rough around the edges but it does the job:

using System;
using System.IO;

class BasicProject1
{

static void Main()
{
string name, nameUp, nameBack, namePerson, nameNumber;
int n;


// Task One

Console.Write("Please enter your name: ");
name = Console.ReadLine();

nameUp = name.ToUpper();
nameBack = name;

Console.WriteLine("\nYour name in capitals is: " + nameUp);

// Task Two

int s = name.Length;

for (int i = 0; i < s; i++)
{
Console.Write(name);
Console.ReadKey(true);
}

// Task Three

Console.Write("\n\nYour name backwards is: ");

int b = nameBack.Length;

for (int i = 0; i < b; i++)
Console.Write(nameBack[b - i - 1]);

// Task Four

do
{
Console.Write("\nEnter your Name: ");
namePerson = Console.ReadLine();
} while (namePerson == "");

do
{
Console.Write("\nEnter your Number: ");
nameNumber = Console.ReadLine();
n = nameNumber.Length;
} while (n != 11);

Console.WriteLine("\nThe phone number you entered is: " + nameNumber);

// Task Five

StreamWriter writer;
writer = new StreamWriter(namePerson + ".txt");
writer.WriteLine("Your Name is: " + namePerson);
writer.WriteLine("Your phone number is: " + nameNumber);
writer.Close();

Console.WriteLine("\nYour info has now been saved to a file.");

Console.ReadLine();
}
}
Next, Have the program ask a question like "How is the weather today? [Good, Bad, Fair, The Sun Just Exploded]" and have the program respond in different ways based on one of those 4 conditions.

Ex)
How is the weather today?> Good
Im glad the weather is Good today.

How is the weather today?> Bad
Im sorry the weather is Bad today.

How is the weathe today?> The Sun Just Exploded
Oh my, well, it was nice knowing you.




Next, Have the program ask a question like "How is the weather today? [Good, Bad, Fair, The Sun Just Exploded]" and have the program respond in different ways based on one of those 4 conditions.

Ex)
How is the weather today?> Good
Im glad the weather is Good today.

How is the weather today?> Bad
Im sorry the weather is Bad today.

How is the weathe today?> The Sun Just Exploded
Oh my, well, it was nice knowing you.



Thanks :) Heres the code for that:
using System;
using System.IO;

class BasicProject1
{

static void Main()
{
string name, nameUp, nameBack, namePerson, nameNumber;
int n;


// Task One

Console.Write("Please enter your name: ");
name = Console.ReadLine();

nameUp = name.ToUpper();
nameBack = name;

Console.WriteLine("\nYour name in capitals is: " + nameUp);

// Task Two

int s = name.Length;

for (int i = 0; i < s; i++)
{
Console.Write(name);
Console.ReadKey(true);
}

// Task Three

Console.Write("\n\nYour name backwards is: ");

int b = nameBack.Length;

for (int i = 0; i < b; i++)
Console.Write(nameBack[b - i - 1]);

// Task Four

do
{
Console.Write("\nEnter your Name: ");
namePerson = Console.ReadLine();
} while (namePerson == "");

do
{
Console.Write("\nEnter your Number: ");
nameNumber = Console.ReadLine();
n = nameNumber.Length;
} while (n != 11);

Console.WriteLine("\nThe phone number you entered is: " + nameNumber);

// Task Five

StreamWriter writer;
writer = new StreamWriter(namePerson + ".txt");
writer.WriteLine("Your Name is: " + namePerson);
writer.WriteLine("Your phone number is: " + nameNumber);
writer.Close();

Console.WriteLine("\nYour info has now been saved to a file.");

// Extra Task

Console.WriteLine("\nHow is the weather today?");
Console.WriteLine("(g = Good, b = Bad, f = Fair, d = The Sun Has Just Exploded AKA Death)");
string weather = Console.ReadLine();

switch (weather)
{
case "g":
Console.WriteLine("I am glad the weather is Good today!");
break;

case "b":
Console.WriteLine("I am sorry the weather is Bad today");
break;

case "f":
Console.WriteLine("It could be worse!");
break;

case "d":
Console.WriteLine("Oh my, well it was nice knowing you.");
break;

default:
Console.WriteLine("Please enter a valid answer!");
break;
}

Console.ReadLine();
}
}
Good job. Lets try some Mathematics processing. Try finding all the prime numbers between 1 to 1000 have them print to the console on their own lines.
Cool, good job! :)

[update] Actually, i just realized we never really covered array concepts yet, so sorting is a biiiiit too early. So try this after Jeff's:

Create an array of 10 items, containing random integer numbers and print them to the screen.

Then iterate through that array and randomly choose an index of that array and swap the value at that spot with the current spot in the loop so you end up with the array all nice and messy and definitely out of order.


Next, after you've disordered the contents of the array, then try to re-sort them in order from lowest to highest. Then have your program spit those numbers out separated by commas or spaces in order from lowest to highest value.

Good job. Lets try some Mathematics processing. Try finding all the prime numbers between 1 to 1000 have them print to the console on their own lines.


Here we go:

using System;
using System.IO;

class BasicProject1
{

static void Main()
{
string name, nameUp, nameBack, namePerson, nameNumber;
int n;


// Task One

Console.Write("Please enter your name: ");
name = Console.ReadLine();

nameUp = name.ToUpper();
nameBack = name;

Console.WriteLine("\nYour name in capitals is: " + nameUp);

// Task Two

int s = name.Length;

for (int i = 0; i < s; i++)
{
Console.Write(name);
Console.ReadKey(true);
}

// Task Three

Console.Write("\n\nYour name backwards is: ");

int b = nameBack.Length;

for (int i = 0; i < b; i++)
Console.Write(nameBack[b - i - 1]);

// Task Four

do
{
Console.Write("\nEnter your Name: ");
namePerson = Console.ReadLine();
} while (namePerson == "");

do
{
Console.Write("\nEnter your Number: ");
nameNumber = Console.ReadLine();
n = nameNumber.Length;
} while (n != 11);

Console.WriteLine("\nThe phone number you entered is: " + nameNumber);

// Task Five

StreamWriter writer;
writer = new StreamWriter(namePerson + ".txt");
writer.WriteLine("Your Name is: " + namePerson);
writer.WriteLine("Your phone number is: " + nameNumber);
writer.Close();

Console.WriteLine("\nYour info has now been saved to a file.");

// Extra Task One

Console.WriteLine("\nHow is the weather today?");
Console.WriteLine("(g = Good, b = Bad, f = Fair, d = The Sun Has Just Exploded AKA Death)");
string weather = Console.ReadLine();

switch (weather)
{
case "g":
Console.WriteLine("I am glad the weather is Good today!");
break;

case "b":
Console.WriteLine("I am sorry the weather is Bad today");
break;

case "f":
Console.WriteLine("It could be worse!");
break;

case "d":
Console.WriteLine("Oh my, well it was nice knowing you.");
break;

default:
Console.WriteLine("Please enter a valid answer!");
break;
}

// Extra Task Two
bool isPrime = true;
for (int i = 0; i <= 1000; i++)
{
for (int j = 2; j <= 1000; j++)
{
if (i != j && i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
Console.WriteLine("Prime:" + i);
}
isPrime = true;
}

Console.ReadLine();
}

}
I decided to make it a little easier to use by using a switch to pick a method to run, instead of going through all of them.

Is there anything i could do to cut down on the code?

using System;
using System.IO;

class BasicProject1
{

static void Main()
{
Console.WriteLine("What do you want to run?");
Console.Write("(1 = Name in Upper Case, 2 = Name Chars Inputted Seperatly, 3 = Name Backwards, 4 = Output Number and Save Name + Num to File, ");
Console.Write("5 = Hows The Weather?, 6 = Prime Numbers)");
int answer = int.Parse(Console.ReadLine());

switch (answer)
{
case 1:
NameCapitals();
break;

case 2:
NameSeperate();
break;

case 3:
NameBackwards();
break;

case 4:
NameNumToFile();
break;

case 5:
Weather();
break;

case 6:
PrimeNumbers();
break;

default:
Console.WriteLine("Please enter a valid answer");
break;
}

Console.ReadLine();
}

// Task One
static void NameCapitals()
{
string nameUp, name, nameBack;

Console.Write("Please enter your name: ");
name = Console.ReadLine();
nameUp = name.ToUpper();
nameBack = name;

Console.WriteLine("\nYour name in capitals is: " + nameUp);
}

// Task Two
static void NameSeperate()
{
Console.Write("Please enter your name: ");
string name = Console.ReadLine();

int s = name.Length;

for (int i = 0; i < s; i++)
{
Console.Write(name);
Console.ReadKey(true);
}
}

// Task Three
static void NameBackwards()
{
string name, nameBack;

Console.Write("Please enter your name: ");
name = Console.ReadLine();

nameBack =name;

Console.Write("\n\nYour name backwards is: ");

int b = nameBack.Length;

for (int i = 0; i < b; i++)
Console.Write(nameBack[b - i - 1]);
}

// Task Four and Five
static void NameNumToFile()
{
string nameNumber, namePerson;
int n;

do
{
Console.Write("\nEnter your Name: ");
namePerson = Console.ReadLine();
} while (namePerson == "");

do
{
Console.Write("\nEnter your Number: ");
nameNumber = Console.ReadLine();
n = nameNumber.Length;
} while (n != 11);

Console.WriteLine("\nThe phone number you entered is: " + nameNumber);



StreamWriter writer;
writer = new StreamWriter(namePerson + ".txt");
writer.WriteLine("Your Name is: " + namePerson);
writer.WriteLine("Your phone number is: " + nameNumber);
writer.Close();

Console.WriteLine("\nYour info has now been saved to a file.");

}

// Extra Task One
static void Weather()
{
Console.WriteLine("\nHow is the weather today?");
Console.WriteLine("(g = Good, b = Bad, f = Fair, d = The Sun Has Just Exploded AKA Death)");
string weather = Console.ReadLine();

switch (weather)
{
case "g":
Console.WriteLine("I am glad the weather is Good today!");
break;

case "b":
Console.WriteLine("I am sorry the weather is Bad today");
break;

case "f":
Console.WriteLine("It could be worse!");
break;

case "d":
Console.WriteLine("Oh my, well it was nice knowing you.");
break;

default:
Console.WriteLine("Please enter a valid answer!");
break;
}
}

// Extra Task Two
static void PrimeNumbers()
{
bool isPrime = true;
for (int i = 0; i <= 1000; i++)
{
for (int j = 2; j <= 1000; j++)
{
if (i != j && i % j == 0)
{
isPrime = false;
break;
}
}
if (isPrime)
{
Console.WriteLine("Prime:" + i);
}
isPrime = true;
}

}

}
I think you should be ok. As a general rule i like to follow is 1st make it work, 2nd clean it up, 3rd make it run fast. 1 is the major piece, 2 is easy, and keeps stuff neat so you know what you did, and 3 only comes to play if you need it to (and alot of times you don't need speed, you need flexibility or simplicity or re-usability).

(sorry if this sounds patronizing, or you already know this :))

Another good practice (and you don't necessarily need to do it in these examples for us), but is get in the habit of commenting your code, even if it seems obvious while you're coding it. I cant tell you how many times my commenting has helped me down the line when i revisit a piece of code i wrote months beforehand. Another very useful practice is to preface all your methods with .NET's tripple slash comment blocks, so your .Net compiler can build the documentation at buildtime. Before any class or method or structure/class variable just hit "/" 3-times and it will create a comment block for you to fill in. They are very useful when you start creating libraries for your various projects as they will create those pop-up notes when you use your editor.


Then iterate through that array and randomly choose an index of that array and swap the value at that spot with the current spot in the loop so you end up with the array all nice and messy and definitely out of order.


What do you mean by this? I have got my random numbers but thats about it <_<:
static void RandomIntArray()
{
int[] intArray = new int[] { 922, 853, 517, 868, 291, 945, 115, 159, 168, 559 };
for (int i = 0; i < intArray.Length; i++)
{
Console.WriteLine(intArray);
}

}


I did try to use
Random random = new Random();
int ranNumber = random.Next(0, 1000);

to get my random numbers, but i couldnt find out how to put it into an array.

This topic is closed to new replies.

Advertisement