C# 我能';我不明白为什么我的程序要求多个输入
因此,我试图解决一个问题,其中: 小女孩Tanya正在学习如何将一个数字减少一,但她对由两个或更多数字组成的数字做了错误的处理。Tanya通过以下算法从一个数字中减去一: 如果数字的最后一位不是零,她将数字减少一; 如果数字的最后一位是零,她将数字除以10(即删除最后一位)。 您将获得一个整数这是基本的“在用户开始理解之前请求输入”代码:C# 我能';我不明白为什么我的程序要求多个输入,c#,C#,因此,我试图解决一个问题,其中: 小女孩Tanya正在学习如何将一个数字减少一,但她对由两个或更多数字组成的数字做了错误的处理。Tanya通过以下算法从一个数字中减去一: 如果数字的最后一位不是零,她将数字减少一; 如果数字的最后一位是零,她将数字除以10(即删除最后一位)。 您将获得一个整数这是基本的“在用户开始理解之前请求输入”代码: 对每个值执行一次。循环之后,您知道变量被设置为有效的输入整数。可以向while循环中添加额外的检查(就像它们需要是正数一样)。为了确保只获得两个输入,您必须获
对每个值执行一次。循环之后,您知道变量被设置为有效的输入整数。可以向while循环中添加额外的检查(就像它们需要是正数一样)。为了确保只获得两个输入,您必须获得第一个输入,然后对其进行验证,如果满足所有要求,则可以获得第二个输入。限制的一种方法是使用循环,将用户保持在此循环中,直到用户输入有效的输入,然后它从循环中断,继续执行代码重置 为了使事情简单明了,错误减法问题需要两个输入,并满足以下要求
- [输入1]n应为介于2和100000000之间的有效整数
- [输入2]k应为介于1和50之间的有效整数
- [结果]应为正整数
while
循环在验证输入时将用户保持在此循环内,如果我们得到有效输入,我们只需中断此循环,如果用户输入无效输入,我们要求用户重新输入有效整数
例如:
int n;
// Get the first input, keep the user in this loop until you get a vaild integer
while (true)
{
if (!int.TryParse(Console.ReadLine(), out n))
{
// not a valid integer
Console.WriteLine("(n) Incorrect Input, please try again!");
}
else if (!(n >= 2 && n <= 1000000000))
{
// (n) should be between 2 and 1,000,000,000
Console.WriteLine("(n) please choose integer between 2 and 1,000,000,000");
}
else
{
// it's a valid integer and between 2 and 1,000,000,000
break;
}
}
用法:
class Program
{
static void Main(string[] args)
{
Math m = new Math();
// Optional loop, if you want to keep the application running until the user close it.
while(true)
{
var result = m.DoingMath();
Console.WriteLine(result);
Console.WriteLine();
Console.WriteLine("Do you want to continue ? [Yes/No]");
var doContinue = Console.ReadLine();
if (doContinue.ToLower().Trim().Equals("no"))
break;
}
}
}
为什么需要检查输入中是否有两个以上的可用值很重要?因此需要执行k/inputValue2操作数。但具体操作取决于当前最后一位数字。(如果为0,则除以10,-1表示所有其他值|您应该将这两个值都设置为整数。您可以使用模来检查特殊情况。
如果(currentValue%10==0)
|如果处理字符串输入,您只会遇到占位符问题,如“,”或“”,这取决于区域性。“我的调试器工作不正常”--听起来你的第一步是找出你的调试器出了什么问题。堆栈溢出不是一个“我不能找出我的工具,所以我会让其他人来做”的站点。
class Math
{
private int n;
private int k;
public int DoingMath()
{
// Get the first input, keep the user in this loop until you get a vaild integer
while (true)
{
if (!int.TryParse(Console.ReadLine(), out n))
{
// not a valid integer
Console.WriteLine("(n) Incorrect Input, please try again!");
}
else if (!(n >= 2 && n <= 1000000000))
{
// (n) should be between 2 and 1,000,000,000
Console.WriteLine("(n) please choose integer between 2 and 1,000,000,000");
}
else
{
// it's a valid integer and between 2 and 1,000,000,000
break;
}
}
// Get the second input, keep the user in this loop until you get a vaild integer
while (true)
{
if (!int.TryParse(Console.ReadLine(), out k))
{
// not a valid integer
Console.WriteLine("(k) Incorrect Input, please try again!");
}
else if (!(k >= 1 && k <= 50))
{
// (k) should be between 1 and 50
Console.WriteLine("(k) please choose integer between 1 and 50.");
}
else
{
// it's a valid integer and between 1 and 50
break;
}
}
// now, do the subtraction logic
for (int x = 0; x < k; x++)
{
if (n < 10) // if n has one digit, then decrease the number
{
n--;
}
else // if n has more than one digit
{
var lastDigit = int.Parse(n.ToString().Substring(n.ToString().Length - 1));
if (lastDigit > 0)
n--;
else
n = n / 10;
}
}
return System.Math.Abs(n); // always returns positive number
}
}
class Program
{
static void Main(string[] args)
{
Math m = new Math();
// Optional loop, if you want to keep the application running until the user close it.
while(true)
{
var result = m.DoingMath();
Console.WriteLine(result);
Console.WriteLine();
Console.WriteLine("Do you want to continue ? [Yes/No]");
var doContinue = Console.ReadLine();
if (doContinue.ToLower().Trim().Equals("no"))
break;
}
}
}