c# - 我正在使用一系列 if/else 语句,但不是让我在运行时“通过”它们,而是直接进入最后一个 else 语句
问题描述
(初学者在这里使用 c# 在 Visual Studio 上测试 if/else 语句)我在 Visual Studio 中打开了两个窗口,其中一个我创建了我需要的类:
{
public string firstName { get; set; }
public string lastName { get; set; }
public string fullName
{
get
{
return firstName + lastName;
}
}
}
和我所有的 if/else 语句:
{
Person person = new Person();
person.firstName = "John";
person.lastName = "Doe";
if (person.fullName == "John Doe")
{
Console.WriteLine("Welp... you've passed... you want another test?");
string x = Console.ReadLine();
if (x == "Yes")
{
Console.WriteLine("Alright... what's 1+1?");
string y = Console.ReadLine();
if (y == "2")
{
Console.WriteLine("Yay! You're right!");
}
else
{
Console.WriteLine("oop... that's wrong my dude");
}
}
else
{
Console.WriteLine("Lame... guess that's bye for now.");
}
}
else
{
Console.WriteLine("oop... ya didn't pass. guess that's bye for now.");
}
}
当我运行它时,它没有让我遍历所有 if/else 语句,而是直接将我带到最后一个 else 语句,并打印oop... ya didn't pass. guess that's bye for now.
出对不起长度和任何令人困惑的东西(我仍在研究我的词汇量哈哈) 感谢您抽出时间为初学者。
解决方案
fullName
JohnDoe
名称中不包含空格,因此结果是John Doe
.
简单地改变:
public string fullName
{
get
{
return firstName + lastName;
}
}
至
public string fullName
{
get
{
return firstName + " " + lastName;
}
}
这样做之后,这个守卫将评估为true
if (person.fullName == "John Doe")
推荐阅读
- ios - 不能跳入封闭
- excel - 删除指定列 VBA
- swift - 在 Swift UI 中,表达式解析为未使用的函数。无法弄清楚问题是什么
- swift - 如何正确创建 SF 符号?
- css - 如何使用微小的png作为react native的覆盖来填充屏幕
- python - 子进程如何处理文件指针?
- html - 如何在所有打开和关闭的 HTML 中设置块?
- reactjs - 我如何通过双击构建的 index.html 来运行我的 reactjs 应用程序
- angular - 异步验证器未在 FormControl 中设置返回的错误
- javascript - 如何在 Javascript 中显示 Open Weather Map 中的天气图标