我有用int變量存儲十進制數的算法。我必須總結兩個十進制數。所以我想知道我可以避免重複轉換代碼,並使用相同的轉換代碼只有不同的存儲變量的功能。C#函數用相同的代碼執行與不同變量相同的代碼
namespace structSimple
{
class Program
{
struct decimalanBroj
{
public int cijeliDio;
public int decimalanDio;
public int potencija;
};
static decimalanBroj baze()
{
String number = "100";
int fromBase = 16;
int toBase = 10;
String result = Convert.ToString(Convert.ToInt32(number, fromBase), toBase);
}
static void Main(string[] args)
{
decimalanBroj d = new decimalanBroj();
decimalanBroj e = new decimalanBroj();
Console.WriteLine("Unesite cijeli dio broj: ");
d.cijeliDio = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Unesite decimalna dio broj: ");
d.decimalanDio = Convert.ToInt32(Console.ReadLine());
d.potencija = d.decimalanDio.ToString().Length;
Console.WriteLine(d.cijeliDio + (d.decimalanDio * (Math.Pow(10, -d.potencija))));
Console.WriteLine("Unesite cijeli dio broj2: ");
e.cijeliDio = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Unesite decimalna dio broj2: ");
e.decimalanDio = Convert.ToInt32(Console.ReadLine());
e.potencija = e.decimalanDio.ToString().Length;
Console.WriteLine("Zbroj dva broja: {0}",
(d.cijeliDio + (d.decimalanDio * (Math.Pow(10, -d.potencija)))) +
(e.cijeliDio + (e.decimalanDio * (Math.Pow(10, -e.potencija)))));
}
}
}
這並不編譯:'靜態decimalanBroj巴澤()'必須返回一個值。 –