I am currently taking a course in c#. One of the exercises is the following.
Ask the user to enter the ring colors of the first 3 rings as text (e.g. green). Then show the value of this resistor. So if the user enters in sequence: red - purple - red
Then the program will show:
This resistor has value of 2700 Ohms
I want to solve this with “if and els is”. I already have the following code but I am stuck on one part.
Console.WriteLine("Enter the first ring color");
string firstRingColor = Console.ReadLine();
Console.WriteLine("Enter the second ring color");
string secondRingColor = Console.ReadLine();
Console.WriteLine("Enter the third ring color");
string thirdRingColor = Console.ReadLine();
if (firstRingColor =="black" || secondRingColor =="black" || thirdRingColor == "black")
{
Console.WriteLine("0");
}
else if (firstRingColor == "brown" || secondRingColor == "brown" || thirdRingColor == "brown")
{
Console.WriteLine("1");
}
with the execution of this program it only executes the last part. Is there a way to save the result and display it afterwards
CodePudding user response:
Here's your homework code
NOTE that this code is for 5- and 6-band resistors and your sample of red-purple-red
calculates to 2700ohm for 4-band resistors, so you need to enter 'red-purple-black-red' to convert it to 5- and 6-band format... or you can remove reading r3
// entry point
void Main()
{
// define the values associated with colors for resistivity
var rColorValues = new Dictionary<string, float> {
{"black",0},
{"brown",1},
{"red", 2},
{"gold",3},
{"yellow",4},
{"green",5},
{"blue",6},
{"purple",7},
{"silver",8},
{"white",9},
};
// colors associated with the multiplier ring
// similar strategy can be used for tolerance and temperature coefficient rings, not shown here
var multColorValues = new Dictionary<string, float>{
{"black", 1},
{"brown", 10},
{"red", 100},
{"gold", 1000},
{"yellow",10000},
{"green", 100000},
{"blue", 1000000},
{"purple",10000000},
{"silver",0.01f},
};
// read the colors for 3 rings.This works for 5 and 6-band resistors
// for 4-band resistor the third ring has to be 'black'
float r1 = ReadColorValue("First ring:", rColorValues);
float r2 = ReadColorValue("Second ring:", rColorValues);
float r3 = ReadColorValue("Third ring:", rColorValues);
// get the color for the multiplier ring
float mult = ReadColorValue("Multiplier ring:", multColorValues);
// calculate the ohms
int ohms = (int)((r1*100 r2*10 r3)*mult);
Console.WriteLine($"{ohms} Ohm resistor");
}
// this method writes the prompt, reads a line and returns the value from the dictionary, if one matches
float ReadColorValue(string prompt, Dictionary<string, float> colorToValueMap)
{
while (true) // keep asking for input until a valid one is received
{
Console.Write(prompt);
var consoleInput = Console.ReadLine().ToLower();
if (colorToValueMap.ContainsKey(consoleInput)){
// if the color name is found - return the value associated with it
return colorToValueMap[consoleInput];
}
// ... otherwise - help the user
Console.Error.WriteLine("Unknown value. Enter one of: " string.Join(", ",colorToValueMap.Keys));
}
}
CodePudding user response:
@sten petrov Thanks again for the reply Sten. But I would like to correct a few things this is not homework but a series of exercises for learning from it. I can understand that this is very simple for you. But for me it is quite difficult. I try to do my best but I don't always find the right search terms. I will look at your code tomorrow in detail and I will try to understand it. If I have any questions about it may I contact you?
CodePudding user response:
Based on the code, I'm going to write my own tomorrow. Can I then upload this here tomorrow for you guys to review?