How can this switch statement be changed to a switch expression? - c#

public class ChineseZodiac
{
public static void Main(String[] args)
{
Console.Write("Enter a year: ");
var year = Convert.ToInt64(Console.ReadLine());
switch (year % 12)
{
case 0:
Console.WriteLine("monkey");
break;
case 1:
Console.WriteLine("rooster");
break;
case 2:
Console.WriteLine("dog");
break;
case 3:
Console.WriteLine("pig");
break;
case 4:
Console.WriteLine("rat");
break;
case 5:
Console.WriteLine("ox");
break;
case 6:
Console.WriteLine("tiger");
break;
case 7:
Console.WriteLine("rabbit");
break;
case 8:
Console.WriteLine("dragon");
break;
case 9:
Console.WriteLine("snake");
break;
case 10:
Console.WriteLine("horse");
break;
case 11:
Console.WriteLine("sheep");
break;
}
}
}
I'm trying to get the formula in the switch statement block to output a zodiac sign based on the user's input. I'm also supposed to have the switch statement converted to a switch expression with the same results. I was able to run it without any errors in VS Code as a switch statement but I'm confused about the difference between a switch statement and a switch expression.

Switch expressions return a value, so you couldn't do exactly what you're doing now (since the action is happening within the switch), but you could use it to return the string and then display it:
var yearMod12 = year % 12;
var yearOf = yearMod12 switch
{
0 => "monkey",
1 => "rooster",
...
};
Console.WriteLine(yearOf);
An enum or Dictionary<int, string> would be a good choice for this type of mapping as well.

I suggest extracting model (animals) into a collection; try keeping data and actions separatedly:
public class ChineseZodiac {
// Model (data): Zodiac animals
private static readonly IReadOnly<string> s_Animals = new string[] {
"monkey", "rooster", "dog",
"pig", "rat", "ox",
"tiger", "rabbit", "dragon",
"snake", "horse", "sheep",
};
// An action which is based on the model
public static void Main(String[] args) {
Console.Write("Enter a year: ");
Console.WriteLine(s_Animals[int.Parse(Console.ReadLine()) % s_Animals.Count]);
}
}

Related

How to use enum in switch , if i need to use a number entered by user? [duplicate]

This question already has answers here:
Get int value from enum in C#
(29 answers)
How do I cast int to enum in C#?
(32 answers)
Closed 2 years ago.
I simply need to use enum with entered number, but I don't know how to convert. Do I have to create an additional variable of int and than convert?
public enum Actions
{
delete,
add,
edit
};
Actions ac = Actions.Parse(Concole.Readline());
switch (ac)
{
case Actions.delete:
int k = int.Parse(Console.ReadLine());
Delete(flights[k]);
Console.WriteLine("");
break;
case 2:
Add();
Console.WriteLine("");
break;
case 3:
Edit();
Console.WriteLine("");
break;
default:
Console.WriteLine("Incorrect number!");
break;
}
You can take in a string and convert it to Enum using Enum.Parse
Actions ac = (Actions)Enum.Parse(typeof(Actions), Console.ReadLine());
// THen use switch on ac.
switch (ac)
{
case Actions.add:
// do something here
break;
case Actions.delete:
// do something here
break;
default:
throw new ApplicationException("Error");
}
Enums, by default, starts with 0. If you want to start with a 1, then assign the int values
public enum Actions
{
delete = 1,
add = 2,
edit = 3
};
from this answer: https://stackoverflow.com/a/29485/4394435
public enum Actions
{
delete, // 0
add, // 1
edit // 2, you can change those by setting them like this: edit = 2
};
class Program
{
static void Main(string[] args)
{
Actions ac = (Actions) Enum.Parse(typeof(Actions), Console.ReadLine());
switch (ac)
{
case Actions.delete:
int k = int.Parse(Console.ReadLine());
Delete(flights[k]);
Console.WriteLine("");
break;
case Actions.add:
Add();
Console.WriteLine("");
break;
case Actions.edit:
Edit();
Console.WriteLine("");
break;
default:
Console.WriteLine("Incorrect number!");
break;
}
}
}

console.clear placement within a looping program

Hi i have been looking for where to put my console.clear because i have made a looping program but i would like to clear after use. Could any one tell me where to put a console.clear or give me a push in the right direction at least. Sorry i am very new to C# and have an assignment due in pretty soon and i am panicing that i havent got my Console.Clear command sorted yet. Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_test
{
class Program
{
static void Main(string[] args)
{
Console.Clear();
//when creating the variable i would of used byte however it wasn't picking it up so i had to use int which i know isnt effective use of memory
int sUserChoice = 0;
do
{
//this makes the text yellow
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Welcome to Hit ‘n’ Miss Ltd");
Console.WriteLine("Please select an option:");
Console.WriteLine("\n\n 1 for Welcoming to the system");
Console.WriteLine("\n 2 for the mean of grades of the class");
Console.WriteLine("\n 3 for what month it is");
Console.WriteLine("\n 4 for adds numbers between -10 and +10 and adds them together");
Console.WriteLine("0 is quit");
Console.Write("\n Please enter a number: ");
int.TryParse(Console.ReadLine(), out sUserChoice);
 
 
 
 
 
 
switch (sUserChoice)
{
case 1:
//here i say that to start talking about the parameter that i shall be using
WelcomeToTheSystem();
break;
case 2:
// here i call apon the parameter used
Grades();
break;
case 3:
// here i call apon the parameter used
Months();
break;
case 4:
// here i call apon the parameter used
AddingNegitiveAndPossitiveNumbers();
break;
// here i call apon the parameter used
case 0:
Quit();
break;
}
} while (sUserChoice != 0);
 
 
Console.ReadKey();
}
//start of prodecdures and funtions
private static int DataVaildation()
{
//variables
bool bUserInput;
int iNumber;
//below is a loop that runs at least once. the loop continues
//iterating while the condition evaluates to true, otherwise it ends
//and control goes to the statement immediately after it.
do
{
Console.Write("Please enter a number: ");
//converts string into int
bUserInput = Int32.TryParse(Console.ReadLine(), out iNumber);
//this will be true if the user input could not be converted for instance a word is used
if (!bUserInput)
{
Console.WriteLine("Input could not be converted into an integer number");
continue;
}
//the validation so if the inputted number from the user it will reject int and do the console.writeline.
if (iNumber < -11 || iNumber < 11)
{
//the error message
Console.WriteLine("Your are out of range please stay between -10 and +10");
bUserInput = false;
}
//the number is in range
else
{
Console.WriteLine("Vaild number!");
//if bUserInput is true then the loop can end.
bUserInput = true;
}
} while (!bUserInput);//while this evaluates to true, the loop continues.
return iNumber;
}
//option 4
private static void AddingNegitiveAndPossitiveNumbers()
{
Console.WriteLine("Please give me 2 number between -10 and +10 and ill add them together\n");
//calls apon the private static int above
int iNumber1 = DataVaildation();
int iNumber2 = DataVaildation();
//the adding will be done here
int iResult = iNumber1 + iNumber2;
Console.WriteLine("The sum of {0} + {1} is {2}", iNumber1, iNumber2, iResult);
}
//data validation
//option 3
private static void Months()
{
Console.WriteLine("so pick a number between 1 and 12. 1 is january and 12 december");
//here i declare that int is a variable
int iMonths = 0;
//i then will tryparse if someone inputs an invalid number
int.TryParse(Console.ReadLine(), out iMonths);
Console.ReadKey();
//i start a switch statement
switch (iMonths)
{
//if the user selects a number it will display the month this is done by using case to get the users input
case 1:
Console.WriteLine("It's January");
break;
case 2:
Console.WriteLine("It's Febuary");
break;
case 3:
Console.WriteLine("It's March");
break;
case 4:
Console.WriteLine("It's April");
break;
case 5:
Console.WriteLine("It's May");
break;
case 6:
Console.WriteLine("It's June");
break;
case 7:
Console.WriteLine("It's July");
break;
case 8:
Console.WriteLine("It's August");
break;
case 9:
Console.WriteLine("It's September");
break;
case 10:
Console.WriteLine("It's October");
break;
case 11:
Console.WriteLine("It's Novemeber");
break;
case 12:
Console.WriteLine("It's December");
break;
}
}
//0
private static void Quit()
{
//this bit exits the code if you press 0
System.Environment.Exit(0);
}
//1
private static void WelcomeToTheSystem()
{
Console.WriteLine("Please enter a name: ");
//used string here because its text
string sName =
Console.ReadLine();
Console.WriteLine("Welcome to the system: " + sName);
Console.ReadKey();
}
//2
private static void Grades()
{
int[] array1 = new int[15];
Console.WriteLine("please give me 15 numbers and i will give you the average of them");
for (int i = 0; i < array1.Length; i++)
{
while (int.TryParse(Console.ReadLine(), out array1[i]) == false)
{
Console.WriteLine("Please give me a number not text");
}
}
////here the averaging takes place it basscially averages the array that it has and adds + to every user input
Console.WriteLine("The average is {0} / 15 = {1}", string.Join("+", array1), array1.Average());
}
}
}
I would suggest place it at the beging of your cycle.
something like this:
do
{
...
//this makes the text yellow
Console.Clear();
Console.ForegroundColor = ConsoleColor.Yellow;
...
}
I would suggest writing a small method that asks if you want to continue. If so, Clear screen and then re-display everything
private static void WouldYouLikeToContinue()
{
Console.Write("Would you like to continue? [n to quit]: ");
string input = Console.ReadLine();
if (input.ToLower() == "n")
Quit();
Console.Clear(); // <--- This is where I would suggest adding the Clear.
}
you can use this method after the switch block
switch (sUserChoice)
{
case 1:
//here i say that to start talking about the parameter that i shall be using
userName = WelcomeToTheSystem();
break;
case 2:
// here i call apon the parameter used
Grades();
break;
case 3:
// here i call apon the parameter used
Months();
break;
case 4:
// here i call apon the parameter used
AddingNegitiveAndPossitiveNumbers();
break;
// here i call apon the parameter used
case 0:
Quit();
break;
}
WouldYouLikeToContinue(); // <----- HERE
} while (sUserChoice != 0);
NOTE: Your method to validate data... has a syntax error.
if (iNumber < -11 || iNumber > 11) // iNumber should be > 11 (not < 11)

Nullable Long switch statement not producing expected output in VS2015

I'm seeing some strange behavior when using a nullable long switch statement in VS2015 Update 1 that I'm not seeing in other Visual Studio releases where it runs as expected.
class Program
{
static void Main(string[] args)
{
NullableTest(-1);
NullableTest(0);
NullableTest(1);
NullableTest(2);
NullableTest(null);
}
public static void NullableTest(long? input)
{
string switch1;
switch (input)
{
case 0:
switch1 = "0";
break;
case 1:
switch1 = "1";
break;
default:
switch1 = "d";
break;
}
string switch2;
switch (input)
{
case -1:
switch2 = "-1";
break;
case 0:
switch2 = "0";
break;
case 1:
switch2 = "1";
break;
default:
switch2 = "d";
break;
}
string ifElse;
if (input == 0)
{
ifElse = "0";
}
else if (input == 1)
{
ifElse = "1";
}
else
{
ifElse = "d";
}
Console.WriteLine("Input = {0}, Switch 1 output = {1}, Switch 2 output = {2}, If Else = {3}", input, switch1, switch2, ifElse);
}
This sample code produces the following output (Aligned for readability):
Input = -1, Switch 1 output = d, Switch 2 output = d, If Else = d
Input = 0, Switch 1 output = 0, Switch 2 output = d, If Else = 0
Input = 1, Switch 1 output = d, Switch 2 output = d, If Else = 1
Input = 2, Switch 1 output = d, Switch 2 output = d, If Else = d
Input = , Switch 1 output = d, Switch 2 output = d, If Else = d
I'm only observing this behavior with Nullable types. Non-nullable types work as expected.
Note that this is not the same behavior that's in this question, and is not caused by this compiler bug that's been fixed in VS2015 Update 1. I've verified that both of those examples run correctly.
It looks like this is caused by this bug, and was fixed with this pull request.
I've tried a recent build of Roslyn and the sample code in the question works as expected now.
Here is the updated version of MSBuild Tools 2015 that has corrected the issue.
This is a bug. I believe it must be a left over of the same bug. You better replace your code with an If to avoid any unexpected behavior.

Why is my array of cards empty?

I am a novice coder learning C# for my class and am having trouble with a practice exercise where we have to make a deck of cards and deal them out.
We need to make a class that creates cards by receiving parameters from another class through a constructor. Then use those values in a Tostring method to set the value and suit of those cards with switch statements and return a card. In the second class we populate an array with cards and call the Tostring method in a Dealing method to randomly generate a user specified amount of cards by pulling them out of the array.
The problem is my array isn't being populated I have tried to Console.WriteLine parts of the array directly after they would be assigned and they are empty. You don't have to give me the full answer put just put me on the right track.
Here is the code:
This is the card creation class
` class Card
{
static int value;
static int suit;
static string cdvalue;
static string cdsuit;
string card;
public Card(int ranvalue, int ransuit) //constructor that sets value and suit
{
value = ranvalue;
suit = ransuit;
}
public override string ToString()
{
switch (value) //switch statement for card value
{
case 1: cdvalue = "ace";
break;
case 2: cdvalue = "two";
break;
case 3: cdvalue = "three";
break;
case 4: cdvalue = "four";
break;
case 5: cdvalue = "five";
break;
case 6: cdvalue = "six";
break;
case 7: cdvalue = "seven";
break;
case 8: cdvalue = "eight";
break;
case 9: cdvalue = "nine";
break;
case 10: cdvalue = "ten";
break;
case 11: cdvalue = "jack";
break;
case 12: cdvalue = "queen";
break;
case 13: cdvalue = "king";
break;
}
switch (suit) // switch for card suit
{
case 1: cdsuit = "Hearts ";
break;
case 2: cdsuit = "Spades ";
break;
case 3: cdsuit = "Diamonds ";
break;
case 4: cdsuit = "Clubs ";
break;
}
card = cdvalue + " of " + cdsuit;
return card;// returns a string in the form of "value of suit"`
This class creates the deck
class Deck
{
Random rng = new Random(); //Random object
private static Card[] deck;
static string[] cards;
public Deck()
{
deck = new Card[52];//creates array of 52 elements
int l = 0;
for (int i = 1; i <= 13; i++)//loops to create cards
{
for (int j = 1; j <= 4; j++)
{
deck[l++] = new Card(i,j); // populates the array with all 52 cards
}
}
}
static string dealt;
static int Rndm;
public string deal(int number)//parameter received from user
{
cards = new string[number];//creates an array to contain dealt card objects
int m = 0;
for (int num=0;num<number;num++) // determines the amount of cards to be dealt
{
Rndm = rng.Next(0,53);
cards[m++] = deck[Rndm].ToString();//fills the card array with randomly dealt cards from the deck
dealt = string.Join(" ", cards); // turns the card array into a single string of the cards dealt
}
return dealt;//returns the cards dealt
This is the test class
static void Main(string[] args)
{
// get the number of cards from the user - must be between 1 and 52
int cardsDealt = -1;
do
{
Console.Write("Enter number of cards to get (1-52): ");
String dealStr = Console.ReadLine();
Boolean parsed = int.TryParse(dealStr, out cardsDealt);
} while (cardsDealt < 1 || cardsDealt > 52);
// create a Deck object
Deck cardDeck = new Deck();
// Call the deal method
String cards = cardDeck.deal(cardsDealt);
// List the result
Console.WriteLine("\nCards dealt:\n" + cards);
In your Card class you have the variables value and suit marked as static.
private static int value;
private static int suit;
Because these two variables are static, the reference to these variables will be maintained across instances of the Card objects. What this means is that every time you create a new Card object you are inadvertently updating all the other Card objects value and suit variables with the same value. So now whenever you get an instance of a Card it will be the same card as all other instances (the last Card that was created).
Remove the static keyword from these declarations and you should be ok.
Also I would suggest reading up on static to more familiarize yourself with its usage: Static keyword in c#
Your code is very close to being correct. You seem to have a misunderstanding of when to use static or not.
If you need a variable that is shared amongst all instances of a class, then use static. But if you need per instance values, then don't.
There are no variables in your code that should be static.
Also, just to help you with your code, you don't need to define all of your variables at the class-level. A lot can (and should) be declared within each method.
Here's the refactored version of your code so that you can see what should be done.
void Main()
{
// get the number of cards from the user - must be between 1 and 52
int cardsDealt = -1;
while (cardsDealt < 1 || cardsDealt > 52)
{
Console.Write("Enter number of cards to get (1-52): ");
String dealStr = Console.ReadLine();
Boolean parsed = int.TryParse(dealStr, out cardsDealt);
}
// create a Deck object
Deck cardDeck = new Deck();
// Call the deal method
String cards = cardDeck.deal(cardsDealt);
// List the result
Console.WriteLine("\nCards dealt:\n" + cards);
}
public class Card
{
private int value;
private int suit;
public Card(int ranvalue, int ransuit) //constructor that sets value and suit
{
value = ranvalue;
suit = ransuit;
}
public override string ToString()
{
string cdvalue = null;
switch (value) //switch statement for card value
{
case 1:
cdvalue = "ace";
break;
case 2:
cdvalue = "two";
break;
case 3:
cdvalue = "three";
break;
case 4:
cdvalue = "four";
break;
case 5:
cdvalue = "five";
break;
case 6:
cdvalue = "six";
break;
case 7:
cdvalue = "seven";
break;
case 8:
cdvalue = "eight";
break;
case 9:
cdvalue = "nine";
break;
case 10:
cdvalue = "ten";
break;
case 11:
cdvalue = "jack";
break;
case 12:
cdvalue = "queen";
break;
case 13:
cdvalue = "king";
break;
}
string cdsuit = null;
switch (suit) // switch for card suit
{
case 1:
cdsuit = "Hearts ";
break;
case 2:
cdsuit = "Spades ";
break;
case 3:
cdsuit = "Diamonds ";
break;
case 4:
cdsuit = "Clubs ";
break;
}
string card = cdvalue + " of " + cdsuit;
return card;// returns a string in the form of "value of suit"`
}
}
public class Deck
{
private Random rng = new Random(); //Random object
private Card[] deck;
public Deck()
{
deck = new Card[52];//creates array of 52 elements
int l = 0;
for (int i = 1; i <= 13; i++)//loops to create cards
{
for (int j = 1; j <= 4; j++)
{
deck[l++] = new Card(i, j); // populates the array with all 52 cards
}
}
}
public string deal(int number)//parameter received from user
{
string[] cards = new string[number];//creates an array to contain dealt card objects
for (int num = 0; num < number; num++) // determines the amount of cards to be dealt
{
int Rndm = rng.Next(deck.Length);
cards[num] = deck[Rndm].ToString();//fills the card array with randomly dealt cards from the deck
}
string dealt = string.Join(" ", cards); // turns the card array into a single string of the cards dealt
return dealt;//returns the cards dealt
}
}
The only other concern with this is that your dealing code produces duplicate cards. You may need to do something about that.
If I were coding this, this is how I would do it:
public class Card
{
private int value;
private int suit;
public Card(int ranvalue, int ransuit)
{
value = ranvalue;
suit = ransuit;
}
public override string ToString()
{
var values = new []
{
"ace", "two", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "jack", "queen", "king"
};
var suits = new []
{
"Hearts", "Spades", "Diamonds", "Clubs"
};
return String.Format("{0} of {1}", values[value - 1], suits[suit - 1]);
}
}
public class Deck
{
private Random rng = new Random();
private Card[] deck;
public Deck()
{
deck =
(
from value in Enumerable.Range(1, 13)
from suit in Enumerable.Range(1, 4)
select new Card(value, suit)
).ToArray();
}
public string deal(int number)
{
string dealt = string.Join(" ", deck.OrderBy(x => rng.Next()).Take(number));
return dealt;
}
}
This fixes the duplicate card issue.

C# Convert Alphanumeric phone number

I've been working on this issue for awhile and I've been stuck so I hope someone can push me in the right direction. I have a c# console application that will take in a string and verify that it contains only 0-9, a-z, A-Z, and -.
My issue that I'm having is that I need to convert any letters in the phone number to their respective number. So if I input 1800-Flowers, it will output as 1800-3569377. I have my methods defined:
I'm not looking for the solutions here (this is homework), but I'm looking for a push in the right direction. Do I need to convert the string to a char array to break up each individual character, and then use that in the convert method to switch any letter into a number?
There are certainly a lot of solutions here. Since you're already using Regex, you could approach it in a basic way:
num = Regex.Replace(num, #"[abcABC]", "2");
num = Regex.Replace(num, #"[defDEF]", "3");
//....
or you could create a Dictionary<string,char> and run through each char and convert it to the mapped character. Something like :
var dict = new Dictionary<string, char>();
dict.Add("abcABC",'2');
//...
foreach(char c in num.Where(char.IsLetter))
{
var digit = dict.First(d => d.Key.Contains(c)).Value;
num = num.Replace(c, digit);
}
Like you said, the LINQ here is splitting the string to a char array, and looping through ones that are letters
Since this is for school, i'm sure you can't go crazy with more advanced topics. Lets keep it simple with a switch/case.
You can map the letters to their corresponding number first, just use a switch/case to find the correct number depending on the letter.
For example:
String phoneNumber = "1800ab";
for(int x=0; x < phoneNumber.Length; x++)
{
if(Char.IsLetter(phoneNumber[x]))
{
switch(phoneNumber[x].ToString().ToLower())
{
case "a":
case "b":
case "c":
//This is number 2!
break;
}
}
}
String already implements IEnumerable<char> - so no need to "break up" there.
Mapping of something to something (like letter code to matching number) is generally done with map (associative array) types (in C#/.Net it is Dictionary) that provide mapping one value ("key") to corresponding "value" - consider using that.
string letter1 = AskuserforInput("first letter");
string number1 = SwitchMethod(letter1);
string letter2 = AskuserforInput("second letter");
string number2 = SwitchMethod(letter2);
string letter3 = AskuserforInput("third letter");
string number3 = SwitchMethod(letter3);
string letter4 = AskuserforInput("fouth letter");
string number4 = SwitchMethod(letter4);
string letter5 = AskuserforInput("fifth letter");
string number5 = SwitchMethod(letter5);
string letter6 = AskuserforInput("sixth letter");
string number6 = SwitchMethod(letter6);
string letter7 = AskuserforInput("seventh letter");
string number7 = SwitchMethod(letter7);
string letter8 = AskuserforInput("eigth letter");
string number8 = SwitchMethod(letter8);
string letter9 = AskuserforInput("ninth letter");
string number9 = SwitchMethod(letter9);
string letter10 = AskuserforInput("tenth letter");
string number10 = SwitchMethod(letter10);
//declaring strings
Console.WriteLine("This is the original letter phone digits");
Console.WriteLine("({0}{1}{2})) {3}{4}{5} - {6}{7}{8}{9} ", letter1,letter2, letter3, letter4, letter5, letter6, letter7, letter8, letter9, letter10);//continue this
Console.WriteLine("The actual numbers" );
Console.WriteLine("({0}{1}{2})) {3}{4}{5} - {6}{7}{8}{9} ", number1, number2, number3, number4, number5, number6, number7, number8, number9, number10);//continue this
Console.Read();
#region End Program
//wait for program to acknowledge results
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\n\nPlease hit ENTER to end program. . .");
Console.Read();
#endregion
Console.Read();
//also pulled this back up from a previous program
}
public static string SwitchMethod(string x)
{
string y = "*";
switch (x)
{
case "0":
y = "0";
break;
case "1":
y = "1";
break;
case "A":
case "a":
case "B":
case "b":
case "C":
case "c":
case "2":
y = "2";
break;
case "D":
case "d":
case "E":
case "e":
case "F":
case "f":
case "3":
y = "3";
break;
case "G":
case "g":
case "H":
case "h":
case "I":
case "i":
case "4":
y = "4";
break;
case "J":
case "j":
case "K":
case "k":
case "L":
case "l":
case "5":
y = "5";
break;
case "M":
case "m":
case "N":
case "n":
case "O":
case "o":
case "6":
y = "6";
break;
case "P":
case "p":
case "Q":
case "q":
case "R":
case "r":
case "S":
case "s":
case "7":
y = "7";
break;
case "T":
case "t":
case "U":
case "u":
case "V":
case "v":
case "8":
y = "8";
break;
case "W":
case "w":
case "X":
case "x":
case "Y":
case "y":
case "Z":
case "z":
case "9":
y ="9";
break;
default:
Console.WriteLine("knucklehead, not a letter");
Console.WriteLine("an '*' will show up");
break;
//used cases, next will use to.lower
//Lynch helped
}
return y;
}
public static string AskuserforInput(string x)
{
Console.WriteLine("\nPlease type {0}", x);
String input = Console.ReadLine();
return input;
}
I'm sure someone can think of a better way, but you could loop through each digit and pass it to this function:
int Asc(char ch)
{
//Return the character value of the given character
return (int)Encoding.ASCII.GetBytes(ch)[0];
}
Then just assign a number based on which ASCII character is returned.

Categories

Resources