[問題] 不知道哪邊寫得有問題

看板C_Sharp作者 (太陽好大)時間13年前 (2010/12/10 13:57), 編輯推噓3(305)
留言8則, 3人參與, 最新討論串1/1
原po最近剛剛接觸C# 在練習寫10進位轉2進位時 不知道為什麼一直改都改不太對 想請版上的大家可以告訴我哪邊有問題 還是這樣寫根本不好之類的ˊˋ 謝謝大家 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace prog4 { class Program { static void Main(string[] args) { string sa; double n; Console.Write("input number:"); sa = Console.ReadLine(); n = double.Parse(sa); int x = (int)n; double y = n - x; string sum = ""; while (x != 0) { if (x % 2 == 0) { sum += 0; } else { sum += 1; } } x = x / 2; string a = Reverse() + "."; while (y > 0) { if (y * 2 - 1 >= 0) { a += 1; y = y * 2 - 1; } else { a += 0; y = y * 2; } } } public static string Reverse(string strsource) { if (!string.IsNu110rEmpty(strsource)) { try { char[] charArray = strSource.ToCharArray(); Array.Reverse(charArray); return new string(charArray); } catch (Exception) { throw; } } else { return ""; } } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.119.73.107

12/10 14:25, , 1F
public static void Print2(uint value)
12/10 14:25, 1F

12/10 14:25, , 2F
if (value != 0)
12/10 14:25, 2F

12/10 14:26, , 3F
uint v = value % 2;
12/10 14:26, 3F

12/10 14:26, , 4F
Print2(value / 2);
12/10 14:26, 4F

12/10 14:26, , 5F
Console.Write(v.ToString());
12/10 14:26, 5F

12/10 21:55, , 6F
可以用BitArray()類別來作,不過練習基本功也是好的,但別鑽
12/10 21:55, 6F

12/10 21:55, , 7F
先培養大處成就感,再從小處練習,比較有感覺!
12/10 21:55, 7F

12/12 01:18, , 8F
謝謝大家:)
12/12 01:18, 8F
文章代碼(AID): #1D0S4jm2 (C_Sharp)