作者vivalabooom (太陽好大)
看板C_Sharp
標題[問題] 不知道哪邊寫得有問題
時間Fri Dec 10 13:56:58 2010
原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
1F:推 heyshi: public static void Print2(uint value) 12/10 14:25
2F:→ heyshi: if (value != 0) 12/10 14:25
3F:→ heyshi: uint v = value % 2; 12/10 14:26
4F:→ heyshi: Print2(value / 2); 12/10 14:26
5F:→ heyshi: Console.Write(v.ToString()); 12/10 14:26
6F:推 tomex:可以用BitArray()類別來作,不過練習基本功也是好的,但別鑽 12/10 21:55
7F:推 tomex:先培養大處成就感,再從小處練習,比較有感覺! 12/10 21:55
8F:→ vivalabooom:謝謝大家:) 12/12 01:18