开发者

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.Group开发者_StackOverflow中文版s["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>\.+)"

It also does tells me this on the score1 and score2 lines:

"The type or namespace name Match could not be found (are you missing a using directive or an essembly reference?)"

And:

"The name `Regex´ does not excist in the current context."

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.

And later on the path I want to assign this into a xy-table. Like so: http://pastebin.com/ahgP04bU


By putting your variables in { ... }, you're creating a separate block scope with those variables. They don't exist outside that scope.

Don't do that.

The Regex class is in the System.Text.RegularExpressions namespace; you need to include that namespace using the using statement.


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.

Also, I don't see where userInput is defined anywhere in your code.


  • You need to scope the namespace, the braces ({ }) are currently missing.
  • You need using System.Text.RegularExpressions at the top of the file with the others.
  • userInput doesn't exist, you need to declare it: var userInput = string.Empty;
  • If strings have special characters in them (such as \), you can use @: @"\myString"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜