[英]String "Sunday" was not recognized as a valid Boolean
I'm quite a beginner when it comes to programming so my problem might seem quite easy for most of people here.在编程方面,我是一个初学者,所以对于这里的大多数人来说,我的问题似乎很容易。 I'm trying to make an easy program where you write down a day of the week and it should show an information depending on an answer.
我正在尝试制作一个简单的程序,您可以在其中写下一周中的某一天,它应该根据答案显示信息。 I chose Sunday as a correct answer.
我选择星期天作为正确答案。 I tried it with bool, with string but I had a problem with it not converting the answer so I don't really know what to do next.
我用 bool 和 string 尝试过,但我遇到了一个问题,它没有转换答案,所以我真的不知道下一步该做什么。 Here's the code.
这是代码。
Boolean day;
Boolean Sunday = true;
Boolean Monday, Tuesday, Wednesday, Thursday, Friday, Saturday = false;
Console.Write("What's the day of the week today? Write your answer here: ");
day = Boolean.Parse(Console.ReadLine());
if (Sunday == true)
{
Console.WriteLine("Your answer is correct!");
}
else
{
Console.WriteLine("Your answer is incorret. Please try again.");
}
Console.ReadKey();
The user input you are expecting is a string
of day of week, but your data type is Boolean
which is either true
or false
.您期望的用户输入是星期几的
string
,但您的数据类型是Boolean
,它是true
或false
。
So when converting from "Sunday" which will be user input for example, the code tries to convert to either true or false and throws exception.因此,当从“Sunday”转换为用户输入时,代码会尝试转换为 true 或 false 并抛出异常。
You should write your program this way:你应该这样写你的程序:
private static void Main(string[] args)
{
var currentDayOfWeek = DayOfWeek.Sunday;
// currentDayOfWeek = DateTime.Today.DayOfWeek; // or if you want dynamically set todays day of week
string userInputDayOfWeek;
Console.Write("What's the day of the week today? Write your answer here: ");
userInputDayOfWeek = Console.ReadLine();
if ((Enum.TryParse(typeof(DayOfWeek), userInputDayOfWeek, ignoreCase: true, out object userInputParsed))
&& (DayOfWeek)userInputParsed == currentDayOfWeek)
{
Console.WriteLine("Your answer is correct!");
}
else
{
Console.WriteLine("Your answer is incorrect. Please try again.");
}
Console.ReadKey();
}
Here we make use of enum DayOfWeek
from the System
namespace and we parse the user input with ignore case flag.在这里,我们使用
System
命名空间中的枚举DayOfWeek
并使用忽略大小写标志解析用户输入。
As I understand reflections are out of the scope of your school project, so you can create a string array据我了解,反射不在您学校项目的 scope 范围内,因此您可以创建一个字符串数组
string trueDay = "Sunday";
// or maybe better
string trueDay=DateTime.Today.DayOfWeek.ToString();
string[] falseDays = new string[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
Console.Write("What's the day of the week today? Write your answer here: ");
var day = Console.ReadLine();
if (day.ToLower() == trueDay.ToLower())
{
Console.WriteLine("Your answer is correct!");
}
else if ( falseDays.Any( d =>d.ToLower()==day.ToLower() ))
{
Console.WriteLine("Your answer is incorret. Please try again.");
} else
Console.WriteLine("Please type a correct day name!");
Console.ReadKey();
if Linq is not allowed too, try this如果 Linq 也不允许,试试这个
var found=false;
foreach (var item in falseDays)
{
if (item.ToLower() == day.ToLower())
{
found=true;
Console.WriteLine("Your answer is incorret. Please try again.");
break;
}
}
if(!found) Console.WriteLine("Please type a correct day name!");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.