如何在Visual C#Studio 2008中编写控制台应用程序 [英] How to write a console application in Visual C# Studio 2008

查看:106
本文介绍了如何在Visual C#Studio 2008中编写控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要根据以下任务在Visual C#Studio 2008环境中开发控制台应用程序:有一个记录数组(最多5个数组),其中包含两个字段:雇员姓名"(类型字符串)和程序中的年龄"(类型为int).

首次启动程序时,屏幕上将显示以下菜单:

员工人数:暂无资料
1-添加员工
2-显示有关所有员工的信息
4-退出

作为用户,您可以添加有关新员工的信息.再次出现菜单:

员工人数:1
1-添加员工
2-显示有关所有员工的信息
4-退出

按下2键(带有数字2)后,程序将立即显示有关所有员工的信息,并再次显示菜单.

我们必须使用单独的功能来显示有关所有员工的信息

按下按钮4将退出应用程序.
按下按钮(1、2、4除外)后,屏幕上将显示消息"Invalid Key Pressed".

操作员是否检查按下的键.
按照以下任务在Visual C#Studio 2008环境中开发控制台应用程序中的循环:程序具有一个记录数组(最多5个数组),其中包含两个字段:雇员姓名"(类型字符串)和年龄"(类型为int).

首次启动该程序时,将显示屏幕菜单:

员工人数:暂无资料
1-添加员工
2-显示所有员工的信息
4-输出

按下用户可以添加有关新员工的信息.再次出现菜单:

员工人数:1
1-添加员工
2-显示所有员工的信息
4-输出

按2键显示有关所有员工的信息,并再次显示菜单.
输出在单独的函数中.
按下按钮4将退出应用程序.
按下1,2,4以外的消息是"Invalid Key Pressed"消息.

操作员是否检查按下的键.
主程序中的循环必须是()while()
定义按键:function getch()
随着应用程序的开发,传递函数在单独的模块中开发. :做()时()
定义按键:function getch()
开发控制台应用程序后,将转移功能在单独的模块中开发.

我的解决方案是:

To develop a console application in the environment Visual C # Studio 2008 in accordance with the following task: There is an array of (up to 5 array) of the records with two fields: "Employee Name" (type string) and "Age" (type int) in the program.

When you first start the program, on the screen will be showed the following menu:

Number of employees: NO DATA
1 - Adding an employee
2 - Display information about all employees
4 - Exit

As an user you can add information about the new employee. Once again it appears the menu:

Number of employees: 1
1 - Adding an employee
2 - Display information about all employees
4 - Exit

After pressing the 2 key (with number 2) the program will display information about all employees at the moment and again displaye the menu.

We must use a separate function to display information about all employees

Pressing the button 4 is out of the application.
After pressing the button (except 1,2,4)on the screen will be showed the message "Invalid Key Pressed".

Checking the pressed key is performed by the operator if.
The loop in the To develop a console application in the environment Visual C # Studio 2008 in accordance with the following task: Program features an array of (up to 5 array) of the records with two fields: "Employee Name" (type string) and "Age" (type int).

When you first start the program prints the on-screen menu:

Number of employees: NO DATA
1 - Adding an employee
2 - Display information for all employees
4 - Output

Pressing a user can add information about the new employee. Once again it appears the menu:

Number of employees: 1
1 - Adding an employee
2 - Display information for all employees
4 - Output

Pressing the 2 key displays information about all employees and again displayed menu.
The output is in a separate function.
Pressing the button 4 is out of the application.
Pressing except 1,2,4 is the message "Invalid Key Pressed".

Checking the pressed key is performed by the operator if.
The loop in the main program must be do () while ()
Defining the key pressed: function getch ()
Following the development of applications, transfer function developed in a separate module. : do () while ()
Defining the key pressed: function getch ()
After developing the console application, transfer functions developed in a separate module.

My solution is that:

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

namespace Lab1_C_sharp_
{
struct TWorker
{
public string sName;
public int iAge;
}
static public void ShowInfWork(TWorker []arr, int n)
{
int count=0;
for(count=0; count<n; count++)
Console.Out.WriteLine("Worker: {0}, {1} years old", arr[count].sName,arr[count].iAge);
}
class Program
{
static void Main(string[] args)
{
TWorker[] arWork = new TWorker[5];
int iNum = 0;
char key;
bool bDone = false;

while ((!bDone))
{
Console.Clear();
Console.Out.WriteLine("Number of workers: ");
if (iNum == 0)
Console.Out.WriteLine("No Data");
else
Console.Out.WriteLine(iNum);
Console.Out.WriteLine(" 1 - Add worker");
Console.Out.WriteLine(" 2 - Show information about all workers");
Console.Out.WriteLine(" 4 - Exit");
key = Console.ReadKey(true).KeyChar;
switch (key)
{
case ''4'': bDone = true; break;
case ''1'':
if (iNum < 4)
{
Console.Out.WriteLine("Enter worker name: ");
arWork[iNum].sName = Console.In.ReadLine();
arWork[iNum].iAge = int.Parse(Console.In.ReadLine());
}
else
Console.Out.WriteLine("You can not enter more data");
iNum++;
break;
case ''2'':
Console.Out.WriteLine("Information about all workers: ");
ShowInfWork(arWork, iNum);
key = Console.ReadKey(true).KeyChar;
break;
}
}
}
}
}


编译后,我得到一些消息:错误3预期的类,委托,枚举,接口或结构"и错误2预期的标识符"
请纠正我的错误.
谢谢您.


After compiling I got some messages: "Error 3 Expected class, delegate, enum, interface, or struct" и "Error 2 Identifier expected"
Please correct my mistakes.
Thanks in advanced.

推荐答案

您只是在某个地方缺少括号.只需弄清楚大括号不对齐的位置,就可以真正开始调试它.

其实不行您似乎不了解C#的工作原理. ShowInfWork是不在类或结构内的方法.
You''re just missing a brace somewhere. Just figure out where your braces don''t line up and you''ll be able to start debugging this for real.

Actually no. You don''t seem to understand how C# works. ShowInfWork is a method that is not inside a class or struct. That''s not allowed.


编译器还将告诉您在哪一行发现这些错误.开始更仔细地查看这些行...
The compiler will also have told you on which line it discovers these errors. Start looking at those lines more closely...


Program类内移动ShowInfWork方法.然后,您应该能够对其进行编译.
Move the ShowInfWork method inside the Program class. Then you should be able to get it compiled.


这篇关于如何在Visual C#Studio 2008中编写控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆