我正在创建一个包含十进制,二进制,八进制和十六进制列的表。我编写了从低数字到高数字显示每种类型的方法。我的问题是如何并排显示这些方法,以便它们排在相应的列中?
我尝试使用以下内容,但它包含“无效参数”:
Console.WriteLine("{0}\t{1}\t...", generate.buildDecimal(), generate.buildBinary(),...)
这是我的代码:
using System;
public class Converter
{
int decimal0;
int decimal1;
int decimal2;
int decimal3;
int bottomLimit;
int topLimit;
// prompt user for range of numbers to display
public void PromptUser()
{
Console.Write("Enter bottom limit: ");
bottomLimit = Convert.ToInt32(Console.ReadLine());
decimal0 = bottomLimit;
decimal1 = bottomLimit;
decimal2 = bottomLimit;
decimal3 = bottomLimit;
Console.Write("Enter top limit: ");
topLimit = Convert.ToInt32(Console.ReadLine());
}
// display decimal values in range
public void buildDecimal()
{
while (decimal0 <= topLimit)
{
Console.WriteLine(decimal0);
decimal0++;
}
}
// display binary values in range
public void buildBinary()
{
while (decimal1 <= topLimit)
{
string binary = Convert.ToString(decimal1, 2);
Console.WriteLine(binary);
decimal1++;
}
}
// display octal values in range
public void buildOctal()
{
while (decimal2 <= topLimit)
{
string octal = Convert.ToString(decimal2, 8);
Console.WriteLine(octal);
decimal2++;
}
}
// display hexadecimal values in range
public void buildHex()
{
while (decimal3 <= topLimit)
{
string hex = Convert.ToString(decimal3, 16);
Console.WriteLine(hex);
decimal3++;
}
}
// call methods
public static void Main(string[] args)
{
Converter generate = new Converter();
generate.PromptUser();
Console.WriteLine("Decimal Binary Octal Hexadecimal");
generate.buildDecimal();
generate.buildBinary();
generate.buildOctal();
generate.buildHex();
}
}
截至目前,程序返回的内容如下:
Enter bottom limit: 1
Enter top limit: 10
Decimal Binary Octal Hexadecimal
1
2
3
4
5
6
7
8
9
10
1
10
11
100
101
110
111
1000
1001
1010
1
2
3
4
5
6
7
10
11
12
1
2
3
4
5
6
7
8
9
a
如果你不能从我的代码中说出来,我是初学者,所以请尽量不要过头。我实际上应该将所有方法放在一个单独的类中,但我无法弄清楚如何调用这些方法。我试图在转换器中使用之类的东西来调用它们:
Converter callMethod = new Converter();
callMethod.buildDecimal();
但它不知道我指的是什么。
答案 0 :(得分:0)
由于下限/上限始终相同,因此您只需使用一种方法即可。 见下文。
public void BuildNumbers()
{
while (decimal1 <= topLimit)
{
string binary = Convert.ToString(decimal1, 2);
string dec = Convert.ToString(decimal1);
string oct = Convert.ToString(decimal1, 8);
string hex = Convert.ToString(decimal1, 16);
Console.Write(binary);
Console.Write('\t');
Console.Write(dec);
Console.Write('\t');
Console.Write(oct);
Console.Write('\t');
Console.Write(hex);
Console.WriteLine();
decimal1++;
}
}