简体   繁体   English

如何从左至右打印输出?

[英]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";
}

Demo on ideone. ideone上的演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何将左右移动分配给我的 2d 游戏 - How do i assign left and right movement to my 2d game 我如何让这个统一项目只向左或向右移动低灵敏度的鼠标? - how do i make this unity project move my mouse with low sensitivity only left or right? 如何在 c# 中将数组的所有元素从左侧移动到右侧 - How do I shift all of the elements of an array from the left side to the right side in c# 如何以某种方式打印输出 - How do I print output in a certain way 如何在我的网络表单中从右到左导航栏? - How Can I Make Right To Left Navbar In My Webform? 如何在 MVC 5 的 Bootstrap 3 导航中左右对齐链接 - How do I align links to left and right in a Bootstrap 3 navigation in MVC 5 我如何一个元素向左对齐,另一个元素向右对齐? - How do I one element align to the left, and another align to the right? 如何从数据库中输出HTML - How do I output html from my database 解析文本时我无法解决的两个问题我也如何从右向左解析为另一种方式? - Two problems I can't solve when parsing the text how do i parse to the other way from right to left too? 如何在C#中从右到左框中的数字左侧显示负号? - How do I make negative signs appear on the left of numbers in a right to left box in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM