我似乎无法正常工作 - 在double weekly_salary = Convert.ToDouble(tokens[3]);
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CompanyPayAndExpense
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public double CalculateNetPay(double weekly_salary, double expense1, double expense2)
{
double basic_pay = 0.0; ;
double extra_pay = 0.0;
double net_pay = 0.0;
if (weekly_salary > 550)
{
basic_pay = 550;
extra_pay = weekly_salary - basic_pay;
}
else
{
basic_pay = weekly_salary;
extra_pay = 0.0;
}
double tax = basic_pay * 0.2 + extra_pay * 0.4;
net_pay = weekly_salary + expense1 + expense2 - tax;
return net_pay;
}
private void cbEmployees_SelectedIndexChanged(object sender, EventArgs e)
{
string myEmployees = cbEmployees.SelectedItem.ToString();
lbEmployees.Items.Clear();
string[] lineOfContents = System.IO.File.ReadAllLines(@"Employees.csv");
foreach (var line in lineOfContents)
{
string[] tokens = line.Split(',');
if (tokens [0] == myEmployees)
{
lbEmployees.Items.Add(tokens[1]); }
}
}
private void lbEmployees_SelectedIndexChanged(object sender, EventArgs e)
{
string[] lineOfContents = System.IO.File.ReadAllLines(@"Employees.csv");
foreach (var line in lineOfContents)
{
string[] tokens = line.Split(',');
if (tokens[1] == (string)lbEmployees.SelectedItem) ;
{
double weekly_salary = Convert.ToDouble(tokens[3]);
double expense1 = Convert.ToDouble(tokens[4]);
double expense2 = Convert.ToDouble(tokens[5]);
lblEmployees.Text = "€" + CalculateNetPay(weekly_salary, expense1, expense2).ToString();
}
}
}
}
}
.csv内容
Role,Name,PPS,weekly_salary,expense1,expense2
Engineer,Paul Smith,9018731G,700,220,90
Engineer,Gary Murphy,5018473F,650,190,130
Engineer, Peter Tate, 3108318A, 725, 300, 200
Manager, Larry O'Dwyer, 4012881N, 900, 450, 85
Manager, John O'Connor, 7012812Q, 1100, 360, 75
Assistant Manager, Ryan Smith, 8847233P, 800, 125, 135
Assistant Manager, Mary Leane, 7887323D, 820, 170, 160
Operative, Olivia Mulcahy, 8732432S, 450, 50, 40
Operative, Sarah Buckley, 8923439J, 390, 70, 30
Sales Executive, Ken Wilkins, 7438432D, 560, 110, 110
Sales Executive, Simon McCann, 2382112Y, 610, 140, 200
由于
答案 0 :(得分:1)
我删除了之前的回答,因为对问题的修改使问题变得清晰,我的旧回答变得多余。
在你的csv数据中很明显,在某些逗号之前,值之前有空格,这意味着你试图将 - 例如 - 450
转换为double,即使它前面有空格,因此你应该像这样修剪值
double weekly_salary = Convert.ToDouble(tokens[3].Trim());
double expense1 = Convert.ToDouble(tokens[4].Trim());
double expense2 = Convert.ToDouble(tokens[5].Trim());
做一个for循环而不是foreach也会稍好一些,因为这意味着你跳过顶行(标题)
for (int i = 1; i < lineOfContents.GetLength(0); i++)
{
//AllTheCode
}
这也意味着您必须使用line.Split(',');
lineOfContents[i].Split(',');
答案 1 :(得分:1)
我不确定它是否导致了您的错误,但我确实看到了一个相当有影响力的错误代码:
if (tokens[1] == (string)lbEmployees.SelectedItem) ; // This semicolon should not be here
{
double weekly_salary = Convert.ToDouble(tokens[3]);
double expense1 = Convert.ToDouble(tokens[4]);
double expense2 = Convert.ToDouble(tokens[5]);
lblEmployees.Text = "€" + CalculateNetPay(weekly_salary, expense1, expense2).ToString();
}
我在上面的代码中注释的分号实际上是提前结束了if
语句,因此无论你的if
语句传递如何,都会运行它后面的代码块,这会导致意外的行为