简体   繁体   English

当前上下文中不存在名称 userInput。 为什么?

[英]The name userInput does not exist in the current context. Why?

I have this at the moment:我现在有这个:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1

static void Main(string[] args)
{
    Console.WriteLine("Which teams have played? right vs inbetween the two teams");
    Match m = Regex.Match("^(?<team1>\.+) vs (?<team2>\.+)$", userInput); 
    if (m.Success) 
    {     
        string team1 = m.Groups["team1"].Value;
        string team2 = m.Groups["team2"].Value; 
    } 

    Console.WriteLine("What is the score? ");    
    Match m = Regex.Match("^(?<score1>\.+)/(?<score2>\.+)$", userInput);
    if (m.Success) 
    {
        int score1 = m.Groups ["score1"].Value;
        int score2 = m.Groups ["score2"].Value;
    }

I get an error on the dot saying:我在点上得到一个错误说:

"Unrecognized escape sequence ^(?<team1>\.+) " “无法识别的转义序列^(?<team1>\.+)

It also does tells me this on the score1 and score2 lines:它还在score1score2行上告诉我这一点:

"The type or namespace name Match could not be found (are you missing a using directive or an essembly reference?)" “找不到类型或命名空间名称Match (您是否缺少 using 指令或 essembly 引用?)”

And:和:

"The name `Regex´ does not excist in the current context." “‘Regex’这个名字在当前上下文中并不存在。”

What I am trying to do is to read two answers from one line and read it as two answers.我要做的是从一行中读取两个答案并将其作为两个答案阅读。

For example: BlueTeam vs RedTeam.例如:蓝队与红队。

Also want to do the same thing for the score.也想为分数做同样的事情。 For example: 1/1 I know the slash is a bit weird but I just did it out of pure laziness to think of something else.例如:1/1 我知道斜线有点奇怪,但我只是出于懒惰去想别的东西。

And later on the path I want to assign this into a xy-table.稍后在路径上,我想将其分配到 xy 表中。 Like so: http://pastebin.com/ahgP04bU像这样: http://pastebin.com/ahgP04bU

By putting your variables in {... } , you're creating a separate block scope with those variables.通过将变量放入{... } ,您将使用这些变量创建一个单独的块 scope。 They don't exist outside that scope.它们不存在于 scope 之外。

Don't do that.不要那样做。

The Regex class is in the System.Text.RegularExpressions namespace;正则Regex class 位于System.Text.RegularExpressions命名空间中; you need to include that namespace using the using statement.您需要使用using语句包含该命名空间。

You also have to include您还必须包括

using System.Text.RegularExpressions

in order to use regular expressions without fully qualifying.为了使用没有完全限定的正则表达式。

Edit: There is a lot that would also improve your code.编辑:还有很多可以改进你的代码。 When you define your variables inside the if blocks, those variables are then locally scoped to the if block and are garbage collected as soon as the if block finishes.当您在if块中定义变量时,这些变量将在本地范围内限定为 if 块,并在 if 块完成后立即进行垃圾收集。

Also, I don't see where userInput is defined anywhere in your code.另外,我看不到代码中任何地方的 userInput 定义。

  • You need to scope the namespace, the braces ( { } ) are currently missing.您需要 scope 命名空间,目前缺少大括号 ( { } )。
  • You need using System.Text.RegularExpressions at the top of the file with the others.您需要在文件顶部与其他文件一起using System.Text.RegularExpressions
  • userInput doesn't exist, you need to declare it: var userInput = string.Empty; userInput不存在,需要声明: var userInput = string.Empty;
  • If strings have special characters in them (such as \ ), you can use @ : @"\myString"如果字符串中有特殊字符(例如\ ),您可以使用@ : @"\myString"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 名称“…”在当前上下文中不存在。 错误 - The name “…” does not exist in the current context. error “当前上下文中不存在名称‘DisplayAlert’。” Xamarin C# - “The name ”DisplayAlert“ does not exist in current context.” Xamarin C# 该名称在当前上下文中不存在。 不上课 - The name does not exist in the current context. Not seeing class 在当前上下文中不存在。 变量声明 - Does not exist in the current context. Variable declaration 在此上下文中不存在名称“ e”。 - The name “e” does not exist in this context. 当前上下文中不存在名称“ABC...”。 c#SQL控制台程序 - The name "ABC..." does not exist in the current context. c# SQL Console program 为什么名称在当前上下文中不存在? - Why name does not exist in current context? Xamarin Forms“...DisplayAlert 在当前上下文中不存在。” - Xamarin Forms “…DisplayAlert does not exist in the current context.” 当前上下文中不存在“会话”。 我该如何使用它? - 'Session' does not exist in the current context. How can I use it? C#error1:当前上下文中不存在名称“DateTimeStyles”。 error2:找不到类型或命名空间名称“CultureInfo” - C# error1:The name 'DateTimeStyles' does not exist in the current context. error2:The type or namespace name 'CultureInfo' could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM