[英]How do I print my output from left to right?
Below codes run perfectly fine. 下面的代码运行得很好。 But how to I print converted number in left to right manner.
但是我如何以从左到右的方式打印转换后的数字。 For example if I input 898989, it will give me the output DB7AD.
例如,如果我输入898989,它将给我输出DB7AD。 How do I print
我该如何列印
DB7AD
to DB7AD
至
DB 7 A D
Codes: 代码:
public static void Main() { int decimalNumber, quotient; int i = 1, j, num = 0; char [] hexadecimalNumber = new char[100]; char temp; Console.WriteLine("Decimal to HexaDecimal conversion using Ascii code.\\n"); Console.WriteLine("Input DECIMAL NUMBER(S) you want to convert to HEXADECIMAL(S):\\t\\n"); Console.Write("Decimal Numbers : \\t"); decimalNumber = int.Parse(Console.ReadLine()); quotient = decimalNumber; while (quotient != 0) { num = quotient % 16; if (num < 10) num = num + 48; else num = num + 55; temp = Convert.ToChar(num); hexadecimalNumber[i++] = temp; quotient = quotient / 16; } Console.Write("HexaDecimal Numbers : \\t"); for (j = i - 1; j > 0; j--) Console.Write(hexadecimalNumber[j]); Console.WriteLine(); Console.Read(); }
Since you are printing your number character-by-character, you need to modify this loop 由于要按字符打印数字,因此需要修改此循环
for (j = i - 1; j > 0; j--) {
Console.Write(hexadecimalNumber[j]);
}
in such a way as to print zero tabs before the first digit, one tab before the second digit, two tabs before the third digit, and so on. 以这样的方式在第一个数字之前打印零个标签,在第二个数字之前打印一个标签,在第三个数字之前打印两个标签,依此类推。 You can do it by making a
string tabs
variable, and adding a "\\t"
to it after each iteration: 您可以通过使
string tabs
变量,并在每次迭代后向其添加"\\t"
来实现:
string tabs = "";
for (j = i - 1; j > 0; j--) {
Console.WriteLine(tabs + hexadecimalNumber[j]);
tabs += "\t";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.