[英]C# - Using numbers split from lines in text file
我正在制作一個基本的稅收計算器,該計算器必須使用ReadLines從文本文件的兩行中讀取,然后用逗號split()並將其用於計算。 我可以很好地讀取和拆分文件,但是實際上引用拆分數據以供計算時無法正常使用。 文本文件是這樣的:
500, 600, 700, 800, 900
0.1, 0.2, 0.3, 0.4, 0.5
到目前為止,這是我的代碼:
private void btnCalculateEmployeeTax_Click(object sender, EventArgs e)
{
string[] rates = File.ReadLines(@"E:\\path.txt").ToArray();
string str1 = rates[0], str2 = rates[1];
string[] income = str1.Split(',');
string[] tax = str2.Split(',');
int wages = 40*int.Parse(txtHourlyRate.Text);
if (wages < income[0])
{
MessageBox.Show("Less than 500");
MessageBox.Show ("Total tax is $" + (wages*tax[0]));
}
else
{
MessageBox.Show("More than 500");
}
顯然是if / else語句中的代碼引發了錯誤-這只是讓您了解了我要如何使用數字。 有沒有一種方法可以將文本文件中的每個數字分配給局部變量? 還是我在這里嘗試的工作可以解決?
提前為菜鳥問題道歉。
您可以將數組轉換為字符串並轉換為整數數組。 你可以這樣
var result = income.Select(int.Parse).ToArray();
首先,如果您想解決此問題,則需要將string
轉換為int
if (wages < int.Parse(income[0]))
{
MessageBox.Show("Less than 500");
MessageBox.Show ("Total tax is $" + (wages*tax[0])); <- the same problem will be here
}
但是我認為創建double
精度數組而不是string
會更好
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.