C#同样的东西我也要打3次才能正常工作,我该如何解决这个问题?
这只是我制作的一个简单程序,需要密码才能打开chromeC#同样的东西我也要打3次才能正常工作,我该如何解决这个问题?,c#,C#,这只是我制作的一个简单程序,需要密码才能打开chrome using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using System.IO; namespace Bitch { class Program { static void Main(strin
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
namespace Bitch
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Type Help If You Need More Info:");
if(Console.ReadLine() == "UneedPassMate")
{
System.Diagnostics.Process.Start("chrome.exe");
System.Environment.Exit(0);
}
if(Console.ReadLine() == "Help")
{
Console.WriteLine("To Open Chrome\n You Will Need To Enter A Password>");
Console.ReadLine();
}
if (Console.ReadLine() == "help")
{
Console.WriteLine("To Open Chrome\n You Will Need To Enter A Password>");
Console.ReadLine();
}
}
}
}
但每次我输入“帮助”或“帮助”时,除非我已经输入了其中一个3次,否则什么都不会发生。有人知道如何解决这个问题吗?这也不是第一次发生。您只想读取用户的输入一次。请尝试以下方法:
var input = Console.ReadLine();
if(input == "UneedPassMate")
{
System.Diagnostics.Process.Start("chrome.exe");
System.Environment.Exit(0);
}
if (input.ToLower() == "help")
{
Console.WriteLine("To Open Chrome\n You Will Need To Enter A Password>");
Console.ReadLine();
}
每次执行
Console.ReadLine()
,程序都会等待输入
应该执行一次,将结果分配给变量,然后检查变量
Console.WriteLine("Type Help If You Need More Info:");
string input = Console.ReadLine();
if(input == "UneedPassMate")
{
System.Diagnostics.Process.Start("chrome.exe");
System.Environment.Exit(0);
}
if (input == "Help" || input == "help")
{
Console.WriteLine("To Open Chrome\n You Will Need To Enter A Password>");
Console.ReadLine();
}
与纯函数式编程语言不同,函数不一定是纯函数,每次调用函数时都可能返回不同的输出
Console.ReadLine()
是一个“不纯”函数的示例。对于3个调用中的每个调用,它从控制台输入读取字符串,并返回用户输入的任何内容
如果只希望用户输入一次,则需要将调用结果存储到Console.ReadLine()
,然后将该结果用于后续测试
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
namespace Bitch
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Type Help If You Need More Info:");
string input = Console.ReadLine(); // Only ask for input once not 3 times!
if(input == "UneedPassMate")
{
System.Diagnostics.Process.Start("chrome.exe");
System.Environment.Exit(0);
}
if(input == "Help")
{
Console.WriteLine("To Open Chrome\n You Will Need To Enter A Password>");
Console.ReadLine();
}
if (input == "help")
{
Console.WriteLine("To Open Chrome\n You Will Need To Enter A Password>");
Console.ReadLine();
}
}
}
}
尝试添加var text=Console.ReadLine();在if条件之前,检查if(text==“”)