c# - 我不知道为什么我的程序要求多个输入
问题描述
所以我试图解决一个问题:
小女孩 Tanya 正在学习如何将一个数字减一,但她做错了一个由两位或更多数字组成的数字。Tanya 通过以下算法从数字中减一:
如果数字的最后一位非零,她将数字减一;如果数字的最后一位是零,她将这个数字除以 10(即删除最后一位)。你得到一个整数。Tanya 会从中减去 1 次。你的任务是打印所有减法后的结果。
保证结果为正整数。
输入 输入的第一行包含两个整数和 (2≤≤109, 1≤≤50) — Tanya 将减去的数字和相应的减法次数。
输出 打印一个整数——减一倍的结果。
保证结果为正整数。
例子
输入复制
512 4
输出复制
50
输入复制
1000000000 9
输出复制
1
注意 第一个示例对应于以下顺序:512→511→510→51→50。
(问题取自:https ://codeforces.com/problemset/problem/977/A )
我的解决方案:
/*
Wrong subtraction solution
Alienator5000
*/
namespace WrongSubtractionAlienator5000
{
class Program
{
static void Main(string[] args)
{
Math m = new Math();
m.doingMath();
Console.Write(m.forReturning);
Console.ReadKey();
}
}
class Math
{
private string inputValue;
private int inputValue2;
public int forReturning;
public int doingMath()
{
inputValue = Console.ReadLine(); //n
inputValue2 = Convert.ToInt32(Console.ReadLine()); //k
while (inputValue2 > 0 && inputValue != null)
{
string checkValue = inputValue.Remove(0, inputValue.Length - 1);
if (checkValue != "0")
{
int newInputValue = Convert.ToInt32(inputValue);
newInputValue = newInputValue - 1;
inputValue2++; //k
forReturning = newInputValue;
}
else if (checkValue == "0")
{
int newInputValue = Convert.ToInt32(inputValue);
newInputValue = newInputValue % 10;
inputValue2++; //k
forReturning = newInputValue;
}
else
{
Console.Write("Incorrect Input");
}
}
return forReturning;
}
}
}
我的调试器工作不正常,所以我不能真正继续我的代码,看看它是否正确,但我只是想知道我能做些什么来确保只接受 2 个输入而不是更多。谢谢!
解决方案
为了确保你只得到两个输入,你必须得到第一个输入,然后验证它,然后如果满足所有要求,你可以得到第二个。一种限制方法是使用循环,将用户保持在此循环中,直到用户输入有效输入,然后它会从循环中中断以继续执行代码的重置。
为了使事情简单明了,这个Wrong Subtraction
问题需要两个具有以下要求的输入。
- [输入 1] n应该是介于 2 和 1,000,000,000 之间的有效整数
- [输入 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;
}
}
正如您在示例中看到的那样,我强制用户通过此验证,用户将无法继续,直到满足两个条件。
int.TryParse(Console.ReadLine(), out n)
它会尝试解析用户输入,如果它是一个有效的整数,那么它将它存储在n
变量中,它会返回true
. 如果是一个无效整数,它将返回false
,并且由于它在循环内,它会要求用户再次重新输入一个有效整数。我们可以对k
输入做同样的事情。
这是问题解决方案的完整示例:
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;
}
}
}
推荐阅读
- ruby - 如何在 Pry 控制台中提供自定义方法
- python - 将数据帧分区到重叠段
- spring-data-mongodb - Spring data MongoDB查询具有BigDecimal类型字段的文档
- c++ - C++ ^ 运算符与函数一起使用
- html - 在网页中制作图表
- java - 如何捕获 GWT TextBox KeyDown 重复
- audio - 如何将音频文件设置为某些特征?
- flutter - Flutter PlatformException(error, 新嵌入不支持旧 FlutterView., null)
- javascript - 使用 javascript 从文本中获取 DOM 正文
- ios - 尝试使用后台任务在 10 秒后检索粘贴板值