使用拆分命令时如何发送无效的用户输入
Posted
技术标签:
【中文标题】使用拆分命令时如何发送无效的用户输入【英文标题】:How to send invaild user input when using a split command 【发布时间】:2019-07-30 14:19:28 【问题描述】:我的程序运行正常。我唯一想弄清楚的是如何为无效的用户输入发送错误。如果用户键入字符串或数字而不使用空格,“a * b”,“12*5”,我如何在拆分中发送错误。
using System;
namespace Calculator1
class Program
static void Main(string[] args)
double answer;
bool Continue = true;
Console.WriteLine("\tCalculator");
Console.WriteLine("--------------------------\n");
Console.WriteLine(" Math Operations: ");
Console.WriteLine(" --------------------");
Console.WriteLine(" Multiplication: *");
Console.WriteLine(" Addition: +");
Console.WriteLine(" Subtraction: -");
Console.WriteLine(" Division: /");
while (Continue)
Console.WriteLine("\nEnter your equation below:");
Console.WriteLine(" For example: 5 + 5 ");
string[] values = Console.ReadLine().Split(' ');
double firstNum = double.Parse(values[0]);
string operation = (values[1]);
double secondNum = double.Parse(values[2]);
if (operation == "*")
answer = firstNum * secondNum;
Console.WriteLine("\n" + firstNum + " * " + secondNum + " = " + answer);
else if (operation == "/")
answer = firstNum / secondNum;
Console.WriteLine("\n" + firstNum + " / " + secondNum + " = " + answer);
else if (operation == "+")
answer = firstNum + secondNum;
Console.WriteLine("\n" + firstNum + " + " + secondNum + " = " + answer);
else if (operation == "-")
answer = firstNum - secondNum;
Console.WriteLine("\n" + firstNum + " - " + secondNum + " = " + answer);
else
Console.WriteLine("Sorry that is not correct format! Please restart!");
Console.WriteLine("\n\nDo you want to continue?");
Console.WriteLine("Type in Yes to continue or press any other key and then press enter to quit:");
string response = Console.ReadLine();
Continue = (response == "Yes");
计算器
数学运算:
乘法:* 加法:+ 减法:- 分区:/
在下面输入您的方程式: 例如:5 + 5 5*5 //这里要发送错误
未处理的异常:System.FormatException:输入字符串的格式不正确。 在 System.Number.ParseDouble(ReadOnlySpan`1 值,NumberStyles 选项,NumberFormatInfo numfmt) 在 System.Double.Parse(String s) 在 Calculator1.Program.Main(String[] args) 按任意键继续 。 . .
计算器
数学运算:
乘法:* 加法:+ 减法:- 分区:/
在下面输入您的方程式: 例如:5 + 5 a * b //我这里也想报错
未处理的异常:System.FormatException:输入字符串的格式不正确。 在 System.Number.ParseDouble(ReadOnlySpan`1 值,NumberStyles 选项,NumberFormatInfo numfmt) 在 System.Double.Parse(String s) 在 Calculator1.Program.Main(String[] args) 按任意键继续 。 . .
【问题讨论】:
你应该使用 switch case 而不是 n-if 【参考方案1】:这可能是你需要的:
public static void Main(string[] args)
double answer;
bool Continue = true;
Console.WriteLine("\tCalculator");
Console.WriteLine("--------------------------\n");
Console.WriteLine(" Math Operations: ");
Console.WriteLine(" --------------------");
Console.WriteLine(" Multiplication: *");
Console.WriteLine(" Addition: +");
Console.WriteLine(" Subtraction: -");
Console.WriteLine(" Division: /");
while (Continue)
Console.WriteLine("\nEnter your equation below:");
Console.WriteLine(" For example: 5 + 5 ");
string[] values = Console.ReadLine().Split(' ');
try
double firstNum = double.Parse(values[0]);
string operation = (values[1]);
double secondNum = double.Parse(values[2]);
switch(operation)
case "*":
answer = firstNum * secondNum;
Console.WriteLine("\n" + firstNum + " * " + secondNum + " = " + answer);
break;
case "/":
answer = firstNum / secondNum;
Console.WriteLine("\n" + firstNum + " / " + secondNum + " = " + answer);
break;
case "+":
answer = firstNum + secondNum;
Console.WriteLine("\n" + firstNum + " + " + secondNum + " = " + answer);
break;
case "-":
answer = firstNum - secondNum;
Console.WriteLine("\n" + firstNum + " - " + secondNum + " = " + answer);
break;
default:
Console.WriteLine("Sorry that is not correct format! Please restart!");
break;
Console.WriteLine("\n\nDo you want to continue?");
Console.WriteLine("Type in Yes to continue or press any other key and then press enter to quit:");
string response = Console.ReadLine();
Continue = (response == "Yes");
catch(FormatException ex)
Console.WriteLine("You entered a bad operation, try another one");
您所要做的就是捕获由解析双精度数(实际上不是双精度数)引起的格式异常。如果你捕捉到它,那么你会向用户打印一条错误消息。
【讨论】:
如果这对您有帮助,请不要忘记将此答案标记为解决方案【参考方案2】:我不知道您所说的“在拆分中发送错误”是什么意思。 split 只是拆分一个字符串。
使用您的错误消息验证输入并执行 Console.Writeline。
为了验证用户输入,我建议使用TryParse 而不是 Parse。这样可以避免异常,您可以检查输入是否有效。
是的。使用 switch 语句而不是 if else 版本。 Ctrl-。应该为此提供重构。
亲切的问候 伯恩德
【讨论】:
【参考方案3】:请看这个解决方案:
// Your codes
while (Continue)
Console.WriteLine("\nEnter your equation below:");
Console.WriteLine(" For example: 5 + 5 ");
string str = Console.ReadLine().Replace(" ", ""); // Remove all spaces
string[] values = Regex.Split(str, @"([\+\-\*\/])"); // Split numbers and operator and also keep operator
if (values.Length != 3)
Console.WriteLine("Expresion is not correct.");
else
double firstNum = double.Parse(values[0]);
string operation = (values[1]);
double secondNum = double.Parse(values[2]);
switch (operation)
case "*":
answer = firstNum * secondNum;
break;
case "/":
answer = firstNum / secondNum;
break;
case "+":
answer = firstNum + secondNum;
break;
case "-":
answer = firstNum - secondNum;
break;
default:
Console.WriteLine("Sorry that is not correct format! Please restart!");
break;
Console.WriteLine($"firstNum operation secondNum = answer");
Console.WriteLine("\n\nDo you want to continue?");
Console.WriteLine("Type in Yes to continue or press any other key and then press enter to quit:");
string response = Console.ReadLine();
Continue = (response.ToUpper() == "YES" || response.ToUpper() == "Y");
// Your codes
【讨论】:
以上是关于使用拆分命令时如何发送无效的用户输入的主要内容,如果未能解决你的问题,请参考以下文章
javascript:在用户键入或更改文本框值时忽略无效输入[重复]