我正在寻求一点帮助。我是 C# 新手,正在尝试为我的孩子构建一个简单的加法/减法游戏来练习他的技能。到目前为止,我已经掌握了基础知识(还有更多需要开发),但有一个问题我正在寻求解决。
我如何在代码中设置字符串,它将在单独的行上打印几个随机数。我想将这些数字向右对齐,但并不总是有固定的位数。这是到目前为止的代码(可能很草率,所以我很抱歉)。
/* Basic build
- There are two numbers up to 6 numbers
- Number 1 must be higher than number 2 (-'s are still out of scope)
- Operation is randomized
- 10 Questions per round
- There will need to be a scoring system
*/
int num1;
int num2;
int answer;
string opperation = "+";
string guess;
int finalAnswer;
Random randNumber = new Random();
int score = 0;
for (int i = 0; i < 10; i++)
{
num1 = randNumber.Next(0, 101);
num2 = randNumber.Next(0, 101);
string num1String = num1.ToString();
string num2String = num2.ToString();
answer = num1 + num2;
Console.WriteLine($" {num1String}\n+ {num2String}\n---------");
guess = Console.ReadLine();
while (!int.TryParse(guess, out finalAnswer))
{
Console.WriteLine("That is not a valid number. Please guess again:");
guess = Console.ReadLine();
}
if (finalAnswer == answer)
{
score++;
Console.WriteLine("That is correct! :)\n\n");
}
else
{
Console.WriteLine("That is Incorrect. :(\n\n");
}
}
Console.WriteLine($"Your score is {score} out of 10");
当前输出的示例可能是
100
+ 5
_________
我希望让它更像
100
+ 5
_________
我一直在研究,但找不到解决方案。
做例如
Console.WriteLine(" {0,8}\n+ {1,8}\n----------", num1, num2);
应该有帮助。