我两天前在学校开始用 c# 编码,所以我对它很陌生,我的代码遇到了问题,我不知道如何修复它。一旦尝试登录的人尝试登录失败 3 次,它确实会显示文本,告诉用户他们失败了 3 次,应用程序将自行关闭,但应用程序不会关闭应用程序,而是将他们发送到带有文本菜单的下一个“页面”在上面。我尝试了多种方法并询问了学校里的朋友,但没有人真正知道代码出了什么问题。
如果有经验的人知道我真的很感激,如果我的代码还有其他问题或者我犯了错误,请告诉我,我想尽可能多地学习!
谢谢!
这是我的代码:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
namespace Gimpies
{
internal class Program
{
static void Main(string[] args)
{
// strings inlognaam + password
string inlognaam = "inkoop";
string password = "Gimpies_Inkoop";
// attempts counter
for (int i = 0; i < 3; i++)
{
// strings gebruikersnaam + wachtwoord
Console.WriteLine("U heeft nog " + (3 - i) + " pogingen ");
Console.WriteLine("Voor gebruikersnaam in:");
string gebruikersnaam = Console.ReadLine();
Console.WriteLine("Voer wachtwoord in:");
string wachtwoord = Console.ReadLine();
// If else login / password
if (inlognaam == gebruikersnaam && password == wachtwoord)
{
Console.WriteLine("Succes");
Console.WriteLine("Druk een knop in om door tegaan");
Console.ReadLine();
goto AfterLoop;
}
else
Console.WriteLine("Gebruikersnaam of wachtwoord is onjuist probeer opnieuw.");
}
Console.WriteLine("U heeft geen poginen meer, de applicatie sluit af na 5 seconden");
Thread.Sleep(5000);
AfterLoop:
Console.Clear();
Console.WriteLine("Menu");
Console.ReadKey();
}
}
}
我们尝试过添加中断;命令而不是 Thread.sleep 但这当然破坏了循环,我们也搞乱了 ReadKey 但这也没有帮助
如上所述,错误在于 goto,因为即使不调用 AfterLoop 仍然会执行
我建议完全避免使用 goto,只需创建一个新方法 AfterLoop();
但是,如果您出于某种原因仍然想使用它,请添加Environment.Exit(0);就在线程之后。延迟后睡眠以关闭应用程序。
干杯!