在公共类中声明变量 [英] Declare variables in a public class
问题描述
我正在尝试使用C#实现堆栈,但是我无法声明变量,我尝试了一个代码,但是我收到一个错误,说名字''顶部' 在当前上下文中不存在。我打开了一个公共静态类,并在那里声明了所有变量,但它似乎没有工作,任何人都可以帮助我。
我甚至试过Data.top ......它不起作用
这是我试过的代码
Hi,
I am trying to implement a stack using C#,but I am not able to declare the variables, I tried a code but I get an error saying "The name ''top'' does not exist in the current context".I opened a public static class and declared all the variables in there,but it doesn''t seem to be working,can anyone help me out.
I even tried Data.top...it doesnt work
This is the code that I tried
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _2
{
class Program
{
public static class Data
{
public const int MAX = 5;//Defining the stack value
public static int top = -1;//Stack is empty
public int[] stack = new int[MAX];//Declare an array called stack
}
public static void push()
{
int element;
if (top ==MAX - 1)
{
Console.WriteLine("Stack Overflow");
return;
}
else
{
Console.WriteLine("enter the number to be pushed");
element = int.Parse(Console.ReadLine());
stack[++top] = element;
Console.WriteLine("The number is pushed successfully", element);
}
return;
}
public static void pop()
{
int y;
if (top == -1)
{
Console.WriteLine("Stack underflow");
return;
}
else
{
y = stack[top];
stack[top--] = 0;
Console.WriteLine("The element has been popped", y);
return;
}
}
public static void display()
{
int i;
if (top == -1)
{
Console.WriteLine("Stack is empty");
return;
}
else
{
Console.WriteLine("Elements are :\n");
for (i = 0; i <= top; i++)
{
Console.WriteLine("%d\n", stack[i]);
}
return;
}
}
static void Main()
{
int ch;
do
{
Console.WriteLine("Enter Your choice \n1.Push\n2.Pop \n3.Display \n4.Exit");
ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: break;
default: Console.WriteLine("Invalid Selection");
break;
}
} while (ch != 4);
}
}
}
推荐答案
要访问top
(以及其他类数据
'的变量)来自类程序
,你有完全引用它,例如
In order to accesstop
(as well other classData
''s variables) from classProgram
, you have to fully reference it, e.g.
if (Data.top == Data.MAX - 1)
而不是
instead of
if(top == MAX - 1)
if (top ==MAX - 1)
顺便说一句,因为 Data.stack
不是 static
,所以没有实例就无法访问它数据
类。
By the way, since Data.stack
is not static
, you cannot access it without an instance of the Data
class.
移动关闭括号!
Move the close bracket!
public static class Data
{
public const int MAX = 5;//Defining the stack value
public static int top = -1;//Stack is empty
public int[] stack = new int[MAX];//Declare an array called stack
}
这是Data类的总和,因为close括号结束了定义。 br />
你需要push,pop和display方法在Data类中,所以在Main方法下面移动关闭的大括号。
This is the totality of your Data class, because the close bracket ends the definition.
You need the push, pop and display methods to be within the Data class, so move the close curly bracket below the Main method.
这是代码,显示没有工作,否则推和流行工作完全正常! Thanx非常帮助我:)
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
命名空间_2
{
class program
{
公共静态类数据
{
public const int MAX = 5; //定义堆栈值
public static int top = -1; // Stack is empty
public static int [] stack = new int [MAX]; //声明一个名为stack的数组
}
public static void push()
{
int element;
if(Data.top == Data.MAX-1)
{
Console.WriteLine(" Stack Overflow" ;;
返回;
}
其他
{
Console.WriteLine(输入要推送的数字);
element = int.Parse(Console.ReadLine());
Data.stack [++ Data.top] = element;
Console.WriteLine("数字被成功推送) ;,元素);
}
返回;
}
public static void pop()
{
int y;
if(Data.top == -1)
{
Console.WriteLine(" Stack underflow");
return;
}
else
{
y = Data.stack [Data.top];
Data.stack [Data.top--] = 0;
Console.WriteLine(元素已被弹出,y);
return;
}
}
public static void display()
{< br $>
int i;
if(Data.top == - 1)
{
Console.WriteLine(Stack is empty);
return; < br $>
}
其他
{
Console.WriteLine(" Elements are:\ n");
for(i = 0;我< = Data.top; i ++)
{
Console.WriteLine(" \ n",Data.stack [i]);
}
返回;
}
}
static void Main()
{
int ch;
do
{
Console.WriteLine(" Enter your choice \\\
1.Push \\\
2.Pop \\\
3.Display \\\
4.Exit");
ch = int.Parse(Console.ReadLine());
开关(ch)
{
案例1:推();
休息;
案例2:pop();
休息;
案例3:显示();
休息;
案例4:休息;
默认:Console.WriteLine(无效选择);
休息;
}
} while(ch!= 4);
}
}
}
Here is the code,display doesn''t work otherwise the push and pop work perfectly fine!! Thanx so much for helping me out :)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _2
{
class Program
{
public static class Data
{
public const int MAX = 5;//Defining the stack value
public static int top = -1;//Stack is empty
public static int[] stack = new int[MAX];//Declare an array called stack
}
public static void push()
{
int element;
if (Data.top==Data.MAX-1)
{
Console.WriteLine("Stack Overflow");
return;
}
else
{
Console.WriteLine("enter the number to be pushed");
element = int.Parse(Console.ReadLine());
Data.stack[++Data.top] = element;
Console.WriteLine("The number is pushed successfully", element);
}
return;
}
public static void pop()
{
int y;
if (Data.top == -1)
{
Console.WriteLine("Stack underflow");
return;
}
else
{
y = Data.stack[Data.top];
Data.stack[Data.top--] = 0;
Console.WriteLine("The element has been popped", y);
return;
}
}
public static void display()
{
int i;
if (Data.top == -1)
{
Console.WriteLine("Stack is empty");
return;
}
else
{
Console.WriteLine("Elements are :\n");
for (i = 0; i <= Data.top; i++)
{
Console.WriteLine("\n",Data.stack[i]);
}
return;
}
}
static void Main()
{
int ch;
do
{
Console.WriteLine("Enter Your choice \n1.Push\n2.Pop \n3.Display \n4.Exit");
ch = int.Parse(Console.ReadLine());
switch (ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: break;
default: Console.WriteLine("Invalid Selection");
break;
}
} while (ch != 4);
}
}
}
这篇关于在公共类中声明变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!