我最近開始「編碼」,我真的到了一個開始,這是我的第一個「項目」之一。它應該是SI轉換器,您可以在其中鍵入值,其單位和您希望轉換的單位。如果/其他條件在C#
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;
}
PS是的,我知道它的大概真的很糟糕,但我的第一try.And如果你能給我什麼建議,我會很開心:)
我不明白這一點...有什麼錯的代碼? – bitwise
給初學程序員的第一個建議:幫個忙,忘掉goto關鍵字;)。改用循環。 –
更好的建議,忽略上述評論,並做你需要做的。樣式和技巧稍後提供 – bitwise