我最近开始“编码”,我真的很开始,这是我的第一个“项目”之一。它应该是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;
}
P.S是的,我知道它可能真的非常糟糕,但它是我的第一次尝试。如果你能给我任何提示,我会非常高兴:)
答案 0 :(得分:1)
使用切换条件
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;
}
答案 1 :(得分:1)
for(<<bit::1 <- :binary.encode_unsigned(n)>>, do: bit) |> Enum.sum
答案 2 :(得分:0)
您可以通过创建具有不同值及其相应文本的数组来更加动态地执行此操作。然后只需使用数组列的值打印相关文本。
string[] arrayText = { "one", "two", "three", "four", "five"};
//Remember than your array start at 0
factor= factor - 1;
System.console.WriteLine(arrayText[factor]);
我认为这就是你想要的!
答案 3 :(得分:0)
代替let title = agreementButton.titleForState(.Normal)
let attributedTitle = NSAttributedString(string: title, attributes: [NSKernAttributeName: 1.0])
agreementButton.setAttributedTitle(attributedTitle, forState: .Normal)
块,您可以使用if/else
,其中Dictionary<string, decimal>
(字符串)是您的预期输入,key
(小数)是输入对应的变量。
您可以在应用启动时以单独的方法构建此词典,因为这些值不会更改。使字典可用于程序的其余部分(例如,使用value
),您可以使用相同的简单检查在任何需要的地方重复使用此信息。
构建字典后,您只需检查字典是否包含输入值并相应地设置public
(或factor1
):
factor2
您还应该考虑在 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"];
}
的某处添加else
条件,以提示用户重新输入值。
此外,在您当前的代码中,请勿对输入和计算重复使用you screwed up, try again
变量。只需将输入作为字符串并检查特定值。
如需其他帮助,我建议在评论中推荐CodeReview。
答案 4 :(得分:0)
您可以使用与此类似的代码:
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];
}