我的代码没有返回我期望的值
My Code is not returning the value I expect
嗨,我最近决定开始学习 C#,我做了这个快速程序来熟悉它的编写方式。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpTesting
{
class Program
{
static void Main(string[] args)
{
string[] Names = new string[4] { "Ben", "Thor", "Zoe", "Kate" };
Names[0] = "ben";
Names[1] = "thor";
Names[2] = "zoe";
Names[3] = "kate";
int Max = 4;
int Current = 0;
bool Found = false;
Console.WriteLine("What player are you looking for?");
string PlayerName = Console.ReadLine();
while ((Found = false) && (Current <= Max))
{
if (Names[Current].Equals(PlayerName))
{
Found = true;
}else
{
Current++;
}
Console.Write(Names[Current] + PlayerName);
}
if (Found == true)
{
Console.WriteLine("Yes, they have a top score");
Console.Write(PlayerName);
}
else if (Found == false)
{
Console.WriteLine("No, they do not have a top score");
Console.Write(PlayerName);
}
else
{
Console.WriteLine("ERROR");
}
Console.ReadKey();
}
}
}
每次我 运行 代码它 returns "No, they do not have a top score" 即使我输入相同的字符串作为数组中的名称之一。我认为问题可能出在我检查 'PlayerName' 和数组名称是否相同但我不确定的方式上。任何帮助将不胜感激。
您需要将 if 子句更改为:
if (Names[Current] == PlayerName)
while ((Found = false) && (Current <= Max))
应该是
while ((Found == false) && (Current <= Max))
顺便说一下,开头大写的名字通常是类。
将您的 while 条件更改为:
while ((Found == false) && (Current <= Max))
(Found = false)
是一个错误。如果您需要比较,请使用 ==
(Found == false)
= 旨在将错误值分配给 Found 变量
您所有的代码都可以替换为
if (Names.Contains(PlayerName ) )
问题出在大写案例上。
更改“Equals
”方法并与 ignoreCase 进行比较
if(string.Compare(PlayerName, Names[Current], true) == 0)
无论如何,正如其他用户所说:Found = false
是错误的。
你必须写 Found == false
嗨,我最近决定开始学习 C#,我做了这个快速程序来熟悉它的编写方式。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSharpTesting
{
class Program
{
static void Main(string[] args)
{
string[] Names = new string[4] { "Ben", "Thor", "Zoe", "Kate" };
Names[0] = "ben";
Names[1] = "thor";
Names[2] = "zoe";
Names[3] = "kate";
int Max = 4;
int Current = 0;
bool Found = false;
Console.WriteLine("What player are you looking for?");
string PlayerName = Console.ReadLine();
while ((Found = false) && (Current <= Max))
{
if (Names[Current].Equals(PlayerName))
{
Found = true;
}else
{
Current++;
}
Console.Write(Names[Current] + PlayerName);
}
if (Found == true)
{
Console.WriteLine("Yes, they have a top score");
Console.Write(PlayerName);
}
else if (Found == false)
{
Console.WriteLine("No, they do not have a top score");
Console.Write(PlayerName);
}
else
{
Console.WriteLine("ERROR");
}
Console.ReadKey();
}
}
}
每次我 运行 代码它 returns "No, they do not have a top score" 即使我输入相同的字符串作为数组中的名称之一。我认为问题可能出在我检查 'PlayerName' 和数组名称是否相同但我不确定的方式上。任何帮助将不胜感激。
您需要将 if 子句更改为:
if (Names[Current] == PlayerName)
while ((Found = false) && (Current <= Max))
应该是
while ((Found == false) && (Current <= Max))
顺便说一下,开头大写的名字通常是类。
将您的 while 条件更改为:
while ((Found == false) && (Current <= Max))
(Found = false)
是一个错误。如果您需要比较,请使用 ==
(Found == false)
= 旨在将错误值分配给 Found 变量
您所有的代码都可以替换为
if (Names.Contains(PlayerName ) )
问题出在大写案例上。
更改“Equals
”方法并与 ignoreCase 进行比较
if(string.Compare(PlayerName, Names[Current], true) == 0)
无论如何,正如其他用户所说:Found = false
是错误的。
你必须写 Found == false