名称“输入”不会在目前情况下存在 [英] The name 'input' does not exist in the current context
问题描述
我一直在C#编程了大约一年,现在我常陷入这样的问题由地方俯瞰细节。我觉得我再这样做,但我似乎无法来解决这个问题。我有一个字符串变量命名为输入我已经宣布它是一个'如果'语句在我的主类像这样:结果
字符串输入=到Console.ReadLine();
结果
作为一个独立的'如果'的语句,在同一个主类,我写了这个:
(输入!= NULL ||输入!= 0)
{
//我有这部分代码,但它是不相关的
$} b $ b,否则
{
//这部分,但它也是无关紧要
}
我的问题是,MonoDevelop的是在第二个如果声明强调两个输入变量,并说'名字输入并不在目前的情况下存在。我觉得我忽视的东西,任何帮助,将不胜感激。
我完整的代码是这样的:
使用系统;
命名空间ConsoleTest
{
类MainClass
{
公共静态字符串版本=0.0.1;
公共静态字符串前体=/:>中;
公共静态无效的主要(字串[] args)
{
Console.Write(控制台测试);
Console.WriteLine(版本);
Console.Write(前体);
字符串开始=到Console.ReadLine();
如果(开始==开始){
,而(真){
Console.WriteLine(已启动);
Console.Write(前体);
列输入=到Console.ReadLine();
}
}其他{
Environment.Exit(0);如果(输入!= NULL ||输入!= 0)
}
{
//代码
}其他{
Console.WriteLine(错误:输入空);
}
}
}
}
你的第一个if语句前声明的输入变量。因为它宣布它里面只有你的if语句内可用的(或在这种情况下,你的循环中)
I've been C# programming for about a year now and I normally get into problems like this by overlooking a detail somewhere. I feel like I am doing this again but I can't seem to solve the problem. I have a string variable named 'input' I have declared it in an 'if' statement in my 'Main' class like so:
string input = Console.Readline();
As a separate 'if' statement, in the same 'Main' class, I have written this:
if (input != null || input != 0)
{
//I have code for this part, but it is irrelevant
}
else
{
//And this part, but it is also irrelevant
}
my problem is that MonoDevelop is highlighting both of the 'input' variables in the second 'if' statement and saying 'the name input does not exist in the current context'. I feel like I am overlooking something, any help would be appreciated. My full code is this:
using System;
namespace ConsoleTest
{
class MainClass
{
public static string version = "0.0.1";
public static string precursor = "/:>";
public static void Main (string[] args)
{
Console.Write ("Console Test ");
Console.WriteLine (version);
Console.Write (precursor);
string start = Console.ReadLine ();
if (start == "start") {
while (true) {
Console.WriteLine ("Started");
Console.Write (precursor);
string input = Console.ReadLine ();
}
} else {
Environment.Exit (0);
}
if (input != null || input != 0) {
//Code
} else {
Console.WriteLine("Error: Input null");
}
}
}
}
Declare your input variable before your first if statement. Because it is declared inside it is only available inside your if statement (or in this case inside your loop)
这篇关于名称“输入”不会在目前情况下存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!