我最近开始“编码”,我真的真的开始了,这是我第一个“项目”之一。它应该是SI转换器,你可以在其中输入值,它的单位和你想要它转换的单位。
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();
}
}
}
}所以问题是我真的不喜欢这部分,我也不知道该怎么做:
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;
}附言:是的,我知道这可能真的真的很糟糕,但这是我的第一个try.And,如果你能给我任何建议,我会很高兴:)
发布于 2015-12-03 02:04:28
使用切换条件
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;
}发布于 2015-12-03 02:54:53
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;
}
}
}
}发布于 2015-12-03 02:10:20
您可以通过创建一个具有不同值及其相应文本的数组来更动态地执行此操作。然后,只打印包含数组列的值的相关文本。
string[] arrayText = { "one", "two", "three", "four", "five"};
//Remember than your array start at 0
factor= factor - 1;
System.console.WriteLine(arrayText[factor]);我想这就是你想要的!
https://stackoverflow.com/questions/34049639
复制相似问题