我的导师为我设定了制作C#程序的任务
这就是我想出的。它只需要是一个小程序,但我不知道在哪里可以使用全局变量。我在考虑减税,但每次开始我都会忘记我的想法。
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();
}
}
答案 0 :(得分:7)
C#没有全局变量的特定概念,但是您可以使用公共静态属性或字段来实现效果,然后可以通过类访问它。例如:
public class GlobalVariables
{
public static double TaxRate {get; set;}
}
GlobalVariabels.TaxRate
访问。
public允许我们从类外部访问变量。 static表示我们不需要GlobalVariables
类的实例来访问它(尽管您确实需要在类的上下文之外查看类名。
正如Preston指出的那样,你可以使你的GlobalVariables类保持静态,因为实际上没有任何理由来实例化它的实例(尽管它没有必要)。
答案 1 :(得分:0)
对于你的"全局变量," (引用因为看here)寻找可以移出方法的东西。
您最好的选择是在调用nameCheck
方法之间不变的变量 - string[] employees = { "Emp1", "Emp2", "Emp3", "Emp4" };
。
由于您没有发布显示使用多个类的代码,因此在方法之外移动employees
是您最好的选择。如果您已经在课堂上学过多个课程,那么请将您在那里学到的知识用于组织代码。看看static也是如此。
答案 2 :(得分:0)
您也可以使用
static double payRoll(double hours, double wage)
{
return hours * wage;
}
也
int hours = Convert.ToInt32(Console.ReadLine()); //why this is an Int should be double
答案 3 :(得分:0)
我已对您的逻辑进行了更改,首先它会读取文件一次,检查文件是否存在,否则会创建它。添加了评论并修复了结构,删除了不必要的变量,并将以前的答案考虑在内以帮助您解决问题。
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();
}
}
}
答案 4 :(得分:-2)
C#是一种面向对象的编程语言,这意味着您可以通过作为类实现的对象访问所有内容。因此,如果可以访问类和成员,则可以看到每个变量(在这种情况下的成员),可以通过将它们声明为 public 来完成。