我不断收到加重的CS0165错误代码。我已经多次重写这个问题,甚至用谷歌搜索了这些问题。我无法解决这个问题,这是一个大学任务,现在已经晚了2天。谁能请帮忙。会很感激。这是下面的代码:
using System;
using System.IO;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
namespace Assignment_2
{
class Program
{
static void Main()
{
string InputPath = @"C:\Users\....\Desktop\CP Class\Assignment 2\Asgn2InputFile.txt";
string OutputPath = @"C:\Users\....\Desktop\CP Class\Assignment 2\Payroll.txt";
using (StreamReader sr = new StreamReader(InputPath))
using (StreamWriter sw = new StreamWriter(OutputPath))
{
// input line
string InputLine;
// input fields
string First;
string Last;
int Hours;
int OverT;
double Pay;
// output fields
double Earnings;
double Gross;
//
sw.Write("NAME".PadRight(11));
sw.Write("HOURS WORKED ".PadRight(23));
sw.Write("PAY RATE".PadRight(20));
sw.Write("OVERTIME".PadRight(27));
sw.WriteLine();
while ((InputLine = sr.ReadLine()) != null)
// parse input line
First = InputLine.Substring(0, 5);
Last = InputLine.Substring(0, 11);
Pay = double.Parse(InputLine.Substring(31, 5));
Hours = int.Parse(InputLine.Substring(16, 2));
OverT = int.Parse(InputLine.Substring(29, 1));
//
Earnings = (Hours * Pay);
Gross = Earnings + (OverT * (Pay * 1.5));
sw.Write(First.PadRight(11)); [[ Error Code occurs here ]]
sw.WriteLine(Last.PadRight(11));
//
sw.Write(Earnings.ToString().PadLeft(10) + " @ " + Gross.ToString("C").PadRight(9));
sw.WriteLine(Earnings.ToString("C").PadLeft(17));
sw.WriteLine();
//Total += Earnings;
}
}
}
}
答案 0 :(得分:1)
错误 CS0165 是因为您使用的是未初始化的变量....
原因是省略 {} 此处
while ((InputLine = sr.ReadLine()) != null)
这意味着,while循环的范围与您需要的东西不同
实际上你的代码相当于:
while ((InputLine = sr.ReadLine()) != null){
First = InputLine.Substring(0, 5);
}
将其余部分保留为未初始化
答案 1 :(得分:1)
由于您的while
循环没有任何大括号来阻止其包含的代码,因此只需将第一行代码作为内容。由于编译器无法保证该行将运行,因此可能不会初始化变量First
。
所以不要这样做:
while (something)
DoSomething();
DoSomethingElse();
你应该写:
while (something)
{
DoSomething();
DoSomethingElse();
}
答案 2 :(得分:0)
您的代码中存在多个问题
请查看以下代码,我认为这是您想要的
using System;
using System.IO;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;
namespace Assignment_2 {
class Program {
static void Main() {
string InputPath = @"C:\Users\....\Desktop\CP Class\Assignment 2\Asgn2InputFile.txt";
string OutputPath = @"C:\Users\....\Desktop\CP Class\Assignment 2\Payroll.txt";
using (StreamReader sr = new StreamReader(InputPath))
using (StreamWriter sw = new StreamWriter(OutputPath)) {
sw.Write("NAME".PadRight(11));
sw.Write("HOURS WORKED ".PadRight(23));
sw.Write("PAY RATE".PadRight(20));
sw.Write("OVERTIME".PadRight(27));
sw.WriteLine();
// input line
string InputLine;
while ((InputLine = sr.ReadLine()) != null) {
// parse input line
var First = InputLine.Substring(0, 5);
var Last = InputLine.Substring(0, 11);
var Pay = double.Parse(InputLine.Substring(31, 5));
var Hours = int.Parse(InputLine.Substring(16, 2));
var OverT = int.Parse(InputLine.Substring(29, 1));
//
var Earnings = (Hours * Pay);
var Gross = Earnings + (OverT * (Pay * 1.5));
sw.Write(First.PadRight(11)); //Error Code occurs here
sw.WriteLine(Last.PadRight(11));
//
sw.Write(Earnings.ToString().PadLeft(10) + " @ " + Gross.ToString("C").PadRight(9));
sw.WriteLine(Earnings.ToString("C").PadLeft(17));
sw.WriteLine();
//Total += Earnings;
}
}
}
}
}