Reputation: 1
I recently started to "coding" and I'm really really into the beginning and this is one of my first "projects". It is supposed to be SI converter where you can type value, its unit and the unit you want it to be converted.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Program
{
class Program
{
static void Main()
{
decimal one = 1;
decimal two = 0.001m;
decimal three = 0.000001m;
decimal four = 0.000000001m;
decimal five = 0.000000000001m;
decimal answer;
begn: Console.WriteLine("SI converter!\nPlease, enter value: ");
decimal value = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nFactors: \n1.One \n2.Milli(m)\n3.Micro(µ)\n4.Nano(n)\n5.Pico(p)\nEnter factor: ");
decimal factor = int.Parse(Console.ReadLine());
if (factor == 1)
{
factor = one;
}else if (factor == 2)
{
factor = two;
}else if (factor == 3)
{
factor = three;
}else if (factor == 4)
{
factor = four;
}else if (factor == 5)
{
factor = five;
}
Console.WriteLine("\nFactors: \n1.One \n2.Milli(m)\n3.Micro(µ)\n4.Nano(n)\n5.Pico(p)\nEnter the second factor: ");
decimal factor2 = Convert.ToInt32(Console.ReadLine());
if (factor2 == 1)
{
factor2 = one;
answer = value * factor;
Console.WriteLine("The answer is : " + answer);
}
else if (factor2 == 2)
{
factor2 = two;
}
else if (factor2 == 3)
{
factor2 = three;
}
else if (factor2 == 4)
{
factor2 = four;
}
else if (factor2 == 5)
{
factor2 = five;
}
answer = value * factor / factor2;
Console.WriteLine("The answer is : " + answer);
Console.WriteLine("Go again?\nY / N");
char ans =char.Parse(Console.ReadLine());
if (ans == 'y')
{
Console.Clear();
goto begn;
}
if(ans=='n')
{
Console.ReadKey();
}
}
}
}
So the problem is that I don't really like this part and I don't have any idea how to do it :
if (factor == 1)
{
factor = one;
}else if (factor == 2)
{
factor = two;
}else if (factor == 3)
{
factor = three;
}else if (factor == 4)
{
factor = four;
}else if (factor == 5)
{
factor = five;
}
P.S Yes I know its probably really really bad, but its my first try.And if you can give me any tips I'll be really happy :)
Upvotes: 0
Views: 1090
Reputation: 13357
You can do it more dynamically by creating a array with the different values and their appropriate text. Then just print the related text with the value of the array column.
string[] arrayText = { "one", "two", "three", "four", "five"};
//Remember than your array start at 0
factor= factor - 1;
System.console.WriteLine(arrayText[factor]);
I think that this is what you want!
Upvotes: 0
Reputation: 1073
You can use code similar to this:
decimal answer;
decimal[] factorArray = new decimal[] { 1, 0.001m, 0.000001m, 0.000000001m, 0.000000000001m };
Console.WriteLine("SI converter!\nPlease, enter value: ");
decimal value = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\nFactors: \n1.One \n2.Milli(m)\n3.Micro(µ)\n4.Nano(n)\n5.Pico(p)\nEnter factor: ");
decimal factor = int.Parse(Console.ReadLine());
if (factor >= 1 && factor <= factorArray.Length)
{
factor = factorArray[(int)factor - 1];
}
Console.WriteLine("\nFactors: \n1.One \n2.Milli(m)\n3.Micro(µ)\n4.Nano(n)\n5.Pico(p)\nEnter the second factor: ");
decimal factor2 = Convert.ToInt32(Console.ReadLine());
if (factor2 >= 1 && factor2 <= factorArray.Length)
{
factor2 = factorArray[(int)factor2 - 1];
}
Upvotes: 0
Reputation: 7400
using System;
namespace Program
{
class Program
{
static decimal[,] factors = new decimal[4, 4] {
/* To Milli To Micro To Nano, To Pico */
/* From Milli */ { 1m, 1000m, 1000000m, 1000000000m },
/* From Micro */ { 0.001m, 1m, 1000m, 1000000m },
/* From Nano */ { 0.000001m, 0.001m, 1m, 1000m },
/* From Pico */ { 0.000000001m, 0.000001m, 0.001m, 1m }
};
static void Main()
{
Console.WriteLine("SI converter!");
while(true)
{
Console.Write("Please, enter value: ");
decimal value = Convert.ToInt32(Console.ReadLine());
Console.Write("\n1) Milli(m)\n2) Micro(µ)\n3) Nano(n)\n4) Pico(p)\nFrom Units: ");
int fromUnits = int.Parse(Console.ReadLine()) - 1;
Console.Write("To Units: ");
int toUnits = int.Parse(Console.ReadLine()) - 1;
decimal factor = factors[fromUnits, toUnits];
decimal answer = factor * value;
Console.WriteLine("The answer is : " + answer);
Console.Write("Go again? (Y/N): ");
string ans = Console.ReadLine();
if(ans.ToUpper() == "N")
break;
}
}
}
}
Upvotes: 1
Reputation: 1505
In place of the if/else
block, you can use a Dictionary<string, decimal>
where the key
(a string) is your expected inputs and the value
(a decimal) is the variable the input corresponds to.
You can build this dictionary in a separate method when the app starts up, as these values won't change. Make the dictionary available to the rest of your program (using public
, for example), and you can re-use this information anywhere you need to with the same simple check.
Once the dictionary is built, all you have to do is check if the dictionary contains the input value and set factor1
(or factor2
) accordingly:
string input;
decimal factor1;
Dictionary<string, decimal> factors = new Dictionary<string, decimal>();
factors.Add("1", one);
factors.Add("2", two);
factors.Add("3", three);
factors.Add("4", four);
factors.Add("5", five);
input = Console.ReadLine();
if (factors.ContainsKey(input))
{
factor1 = factors["input"];
}
You should also consider adding an else
condition somewhere along the lines of you screwed up, try again
to prompt the user to re-enter a value.
Also, in your current code, don't re-use the factor
variable for both input and calculations. Just take the input as a string and check against specific values.
For additional help, I'd suggest CodeReview as has been recommended in the comments.
Upvotes: 0
Reputation: 496
Use Switch condition
switch (factor)
{
case 1:
factor = one;
break;
case 2:
factor = two;
break;
case 3:
factor = three;
break;
case 4:
factor = four;
break;
default:
//default when nothing happens in switch
factor = one;
break;
}
Upvotes: 1