我需要从csv文件中的数据返回所有峰值和谷值。当我运行程序时,我不断收到异常,告诉我字符串格式不正确。我不知道我写错了什么。我将值传递给testX和testY,然后在我的while循环中进行比较。 while循环是我将testY转换为double时遇到错误的地方。
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;
using System.IO;
namespace WindowsFormsApp6
{
public partial class Form1 : Form
{
double firstY = 0.0;
string testX;
string testY;
string[] xpoint = new string[5000];
string[] ypoint = new string[5000];
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (var reader = new StreamReader(@"D:\data.csv"))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
var values = line.Split(',');
testX = values[0];
testY = values[1];
while (Convert.ToDouble(testY) <= firstY)
{
firstY = Convert.ToDouble(testY);
if (firstY == Convert.ToDouble(testY))
{
break;
}
}
if (Convert.ToDouble(testY) > firstY)
{
listBox1.Items.Add(Convert.ToDouble(testX) + "," + firstY);
}
while (Convert.ToDouble(testY) >= firstY)
{
firstY = Convert.ToDouble(testY);
if (firstY == Convert.ToDouble(testY))
{
break;
}
}
if (Convert.ToDouble(testY) < firstY)
{
listBox2.Items.Add(Convert.ToDouble(testX) + "," + firstY);
}
Convert.ToString(testX);
Convert.ToString(testY);
}
}
}
}
}