我的导师给我安排了一项任务,让我制作一个
- 演示递归(我想我已经做到了)
- 使用全局变量
- 可以被企业使用
这就是我想到的。它只需要是一个小程序,但我不知道在哪里可以使用全局变量。我一直在想一些减税的办法,但每次我开始的时候,我都忘了我的想法是什么。
static void nameCheck()
{
Console.WriteLine("Name of employee: ");
string employee = Console.ReadLine();
string[] employees = { "Emp1", "Emp2", "Emp3", "Emp4" };
File.WriteAllLines("C:/Users/Chris/Documents/Visual Studio 2013/Projects/ConsoleApplication38/Employees.txt", employees);
string[] lines = File.ReadAllLines("C:/Users/Chris/Documents/Visual Studio 2013/Projects/ConsoleApplication38/Employees.txt");
int match = 0;
foreach (string line in lines)
{
if (employee != line)
{
match = match + 1;
if (match > 3)
{
Console.WriteLine("That name is not in the employee database, try again:");
nameCheck();
}
}
}
}
static double payRoll(double hours, double wage)
{
double pay = hours * wage;
return pay;
}
static void Main(string[] args)
{
Console.WriteLine(" PAYROLL");
Console.WriteLine("--------------------------------------------------------------------------------");
nameCheck();
Console.WriteLine("Number of hours worked this week: ");
int hours = Convert.ToInt32(Console.ReadLine());
const double wage = 7.50;
double pay = payRoll(hours, wage);
Console.WriteLine("Pay before tax for this employee is £" + pay);
Console.ReadLine();
}
}
C#没有全局变量的特定概念,但您可以通过公共静态属性或字段来实现这种效果,然后您可以通过类访问该属性或字段。例如:
public class GlobalVariables
{
public static double TaxRate {get; set;}
}
在GlobalVariabels.TaxRate
访问。
public允许我们从类外部访问变量。static意味着我们不需要GlobalVariables
类的实例来访问它(尽管您确实需要在类的上下文之外遍历类名。
正如Preston所指出的,您可以使GlobalVariables类成为静态的,因为实际上没有任何理由实例化它的实例(尽管这不是必要的)。
对于您的"全局变量",(引用,因为请参阅此处)查找可以从方法中移出的内容。
最好的选择是在对nameCheck
方法string[] employees = { "Emp1", "Emp2", "Emp3", "Emp4" };
的调用之间不发生变化的变量。
由于您没有发布显示使用多个类的代码,因此最好将employees
移到方法之外。如果您已经在课堂上学习了多个类,那么就用您在那里学到的内容来组织代码。还可以看一眼静态。
您也可以像一样使用
static double payRoll(double hours, double wage)
{
return hours * wage;
}
还有
int hours = Convert.ToInt32(Console.ReadLine()); //why this is an Int should be double
我对您的逻辑进行了更改,首先它读取文件一次,检查文件是否存在,否则它会创建它。添加注释并修复结构,删除不必要的变量,并考虑以前的答案来帮助您。
namespace Temp1
{
using System;
using System.IO;
public class GlobalVariables
{
/// <summary>
/// Wage per hour
/// </summary>
public static double WagePerHour = 7.5;
}
public class Program
{
private static void LoadEmployees()
{
// Get the name of the employee
Console.WriteLine("Name of employee: ");
string employee = Console.ReadLine();
// Get the file path
string filePath = string.Format(
@"{0}{1}",
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),
"Employees.txt");
// Check if the file does not exist and create it
if (!File.Exists(filePath))
{
// Generate sample employees
string[] employees = { "Emp1", "Emp2", "Emp3", "Emp4" };
// Write all the lines in the file
File.WriteAllLines(filePath, employees);
}
// Read all the lines from the file
string[] currentEmployees = File.ReadAllLines(filePath);
// Check the employee name
NameCheck(currentEmployees, employee);
}
/// <summary>
/// Do the name check recursively so you don’t keep loading the file all the time
/// </summary>
/// <param name="names">Array of all the employee names</param>
/// <param name="nameToFind">Name to find</param>
/// <param name="currentPosition">Current position in the array</param>
public static void NameCheck(string[] names, string nameToFind, int currentPosition = 0)
{
if (currentPosition == nameToFind.Length - 1)
{
Console.WriteLine("That name is not in the employee database, try again:");
}
else if (string.Compare(names[currentPosition], nameToFind, StringComparison.InvariantCulture) != 0)
{
currentPosition++;
NameCheck(names, nameToFind, currentPosition);
}
}
/// <summary>
/// Calculate pay roll
/// </summary>
/// <param name="hours"></param>
/// <param name="wage"></param>
/// <returns></returns>
private static double PayRoll(double hours, double wage)
{
return hours * wage;
}
/// <summary>
///
/// </summary>
/// <param name="args"></param>
private static void Main(string[] args)
{
Console.WriteLine(" PAYROLL");
Console.WriteLine("--------------------------------------------------------------------------------");
// Load employees and check if the employee is in the list
LoadEmployees();
// Get the number of hours worked
Console.WriteLine("Number of hours worked this week: ");
double hours = Convert.ToDouble(Console.ReadLine());
// Get the current pay
double pay = PayRoll(hours, GlobalVariables.WagePerHour);
Console.WriteLine("Pay before tax for this employee is £" + pay);
Console.ReadLine();
}
}
}
C#是一种面向对象的编程语言,这意味着您可以通过一个类的实现对象访问所有内容。因此,如果类和成员都是可访问的,则每个变量(在这种情况下为成员)都是可见的,这可以通过将它们声明为public来实现。