C# Foreach循环不工作
我有2个foreach循环,它们各自工作(当另一个被注释掉时)。。但当我把它们粘在一起时,只有第一个起作用C# Foreach循环不工作,c#,loops,foreach,C#,Loops,Foreach,我有2个foreach循环,它们各自工作(当另一个被注释掉时)。。但当我把它们粘在一起时,只有第一个起作用 // Splits the RichTextBox up so the numbers can be formatted properly. String[] myXLines = calculateXRichTextBox.Text.Split('\n'); String[] myYLines = calculateYRichTextBox.Text.Split('\n'); // C
// Splits the RichTextBox up so the numbers can be formatted properly.
String[] myXLines = calculateXRichTextBox.Text.Split('\n');
String[] myYLines = calculateYRichTextBox.Text.Split('\n');
// Converts the numbers to only contain 2 decimal places.
foreach (string decimalXLines in myXLines)
removedXDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalXLines), 2) + "\n");
foreach (string decimalYLines in myYLines)
removedYDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalYLines), 2) + "\n");
是否有人知道如何让它工作,或者为什么它不能正常工作(RTB没有附加文本)
提前感谢您的帮助
编辑:
private void calculateXAndYPlacementOne()
{
尝试
{
尝试
{
//将placementOneListBox的内容保存到文件中。
System.IO.StreamWriter sw=新的System.IO.StreamWriter(filePath+“\\computing X,Y File.txt”);
foreach(placementOneListBox.Items中的对象项)
sw.WriteLine(item.ToString());
sw.Close();
}
//捕获未保存文件时的异常。
捕获(例外)
{
Show(“无法写入文件”);
}
//读取文件中要格式化的行。
var fileReader=File.OpenText(filePath+“\\computing X,Y File.txt”);
//为要存储的行创建列表。
var fileList=新列表();
//将文件中的每一行添加到列表中。
var fileLines=“”;
而((fileLines=fileReader.ReadLine())!=null)
添加(文件行);
//创建新列表以保存每个列表的某些匹配项。
var xysult=新列表();
var xResult=新列表();
var yResult=新列表();
//迭代文件中的每一行并提取x和y值
fileList.ForEach(行=>
{
Match xyMatch=Regex.Match(行,@“(?-?\d+\.\d+)\s+(?-?\d+\.\d+));
if(xyMatch.Success)
{
//从正则表达式匹配中获取x和y值
字符串xValue=xyMatch.Groups[“x”].Value;
字符串yValue=xyMatch.Groups[“y”].Value;
//将这两个值(用空格分隔)添加到“xyResult”列表中。
xysult.Add(String.Join(“,new[]{xValue,yValue}));
//将结果添加到列表中。
xResult.Add(xValue);
yResult.Add(yValue);
//存储旧的X和Y值。
oldXRichTextBox.AppendText(xValue+“\n”);
AppendText(yValue+“\n”);
尝试
{
//计算X&Y值(包括X&Y位移)
double doubleX=double.Parse(xValue);
double doubleXValue=double.Parse(xDisplacementTextBox.Text);
StringBuilder sbX=新的StringBuilder();
AppendLine((doubleX+doubleXValue.ToString());
double doubly=double.Parse(yValue);
double doubleYValue=double.Parse(yDisplacementTextBox.Text);
StringBuilder sbY=新的StringBuilder();
sbY.AppendLine((doubleY+doubleYValue.ToString());
calculateXRichTextBox.AppendText(sbX+“”);
CalculateRichTextBox.AppendText(sbY+“”);
/移除空白行。
calculateXRichTextBox.Text=Regex.Replace(calculateXRichTextBox.Text,@“^\s*$(\n |\r |\r\n)”,“”,RegexOptions.Multiline);
calculateYRichTextBox.Text=Regex.Replace(calculateYRichTextBox.Text,@“^\s*$(\n |\r |\r\n)”,“”,RegexOptions.Multiline);
}
//如果它失败了,它就会被捕获
捕获(例外)
{
Show(“无法计算X&Y值”);
}
}
});
//将RichTextBox拆分,以便正确格式化数字。
字符串[]myXLines=calculateXRichTextBox.Text.Split('\n');
字符串[]myYLines=calculateYRichTextBox.Text.Split('\n');
foreach(myxline中的字符串小数线)
removedXDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalXLines),2)+“\n”);
foreach(myyline中的字符串小数点)
removedYDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalYLines),2)+“\n”);
对于(int theLine=0;line
查看下面的答案以查看修复程序的代码如果将每个修复程序分解为多个语句,会发生什么?您正在执行Convert.ToDouble()和Round()操作,所有操作都与AppendText()调用内联。你确定每个步骤都正确吗?输入数据中的问题,每次输入字符串不是正确的数字时,调用Math.Round(Convert.ToDouble(decimalXLines),2)将抛出e
private void calculateXAndYPlacementOne()
{
try
{
try
{
// Save the contents of the placementOneListBox into the file.
System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath + "\\Calculating X,Y File.txt");
foreach (object item in placementOneListBox.Items)
sw.WriteLine(item.ToString());
sw.Close();
}
// Catches an exception if the file was not saved.
catch (Exception)
{
MessageBox.Show("Could not write to file.");
}
// Reads the lines in the file to format.
var fileReader = File.OpenText(filePath + "\\Calculating X,Y File.txt");
// Creates a list for the lines to be stored in.
var fileList = new List<string>();
// Adds each line in the file to the list.
var fileLines = "";
while ((fileLines = fileReader.ReadLine()) != null)
fileList.Add(fileLines);
// Creates new lists to hold certain matches for each list.
var xyResult = new List<string>();
var xResult = new List<string>();
var yResult = new List<string>();
// Iterate over each line in the file and extract the x and y values
fileList.ForEach(line =>
{
Match xyMatch = Regex.Match(line, @"(?<x>-?\d+\.\d+)\s+(?<y>-?\d+\.\d+)");
if (xyMatch.Success)
{
// Grab the x and y values from the regular expression match
String xValue = xyMatch.Groups["x"].Value;
String yValue = xyMatch.Groups["y"].Value;
// Add these two values, separated by a space, to the "xyResult" list.
xyResult.Add(String.Join(" ", new[] { xValue, yValue }));
// Add the results to the lists.
xResult.Add(xValue);
yResult.Add(yValue);
// Store the old X and Y values.
oldXRichTextBox.AppendText(xValue + "\n");
oldYRichTextBox.AppendText(yValue + "\n");
try
{
// Calculate the X & Y values (including the x & y displacements)
double doubleX = double.Parse(xValue);
double doubleXValue = double.Parse(xDisplacementTextBox.Text);
StringBuilder sbX = new StringBuilder();
sbX.AppendLine((doubleX + doubleXValue).ToString());
double doubleY = double.Parse(yValue);
double doubleYValue = double.Parse(yDisplacementTextBox.Text);
StringBuilder sbY = new StringBuilder();
sbY.AppendLine((doubleY + doubleYValue).ToString());
calculateXRichTextBox.AppendText(sbX + "");
calculateYRichTextBox.AppendText(sbY + "");
// Removes the blank lines.
calculateXRichTextBox.Text = Regex.Replace(calculateXRichTextBox.Text, @"^\s*$(\n|\r|\r\n)", "", RegexOptions.Multiline);
calculateYRichTextBox.Text = Regex.Replace(calculateYRichTextBox.Text, @"^\s*$(\n|\r|\r\n)", "", RegexOptions.Multiline);
}
// Catches if it fails
catch (Exception)
{
MessageBox.Show("Could not calculate the X & Y values.");
}
}
});
// Splits the RichTextBox up so the numbers can be formatted properly.
String[] myXLines = calculateXRichTextBox.Text.Split('\n');
String[] myYLines = calculateYRichTextBox.Text.Split('\n');
foreach (string decimalXLines in myXLines)
removedXDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalXLines), 2) + "\n");
foreach (string decimalYLines in myYLines)
removedYDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalYLines), 2) + "\n");
for (int theLine = 0; theLine < placementOneListBox.Items.Count; theLine++)
{
string replacement1 = calculateXRichTextBox.Lines[theLine];
while (replacement1.Length < 7)
replacement1 = " " + replacement1;
placementOneListBox.Items[theLine] = ((string)placementOneListBox.Items[theLine]).Remove(20, 7).Insert(20, replacement1);
string replacement2 = calculateYRichTextBox.Lines[theLine];
while (replacement2.Length < 7)
replacement2 = " " + replacement2;
placementOneListBox.Items[theLine] = ((string)placementOneListBox.Items[theLine]).Remove(29, 7).Insert(29, replacement2);
};
}
catch (Exception)
{
MessageBox.Show("Could not manipulate the data properly.");
}
File.Delete(filePath + "\\Calculating X,Y File.txt");
}
String[] myXLines = calculateXRichTextBox.Text.Split(new []{'\n'}, StringSplitOptions.RemoveEmptyEntries);
// Splits the RichTextBox up so the numbers can be formatted properly.
String[] myXLines = calculateXRichTextBox.Text.Split('\n');
String[] myYLines = calculateYRichTextBox.Text.Split('\n');
int counterX = 0;
int counterY = 0;
foreach (string decimalXLines in myXLines)
{
if (counterX == 0)
removedXDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalXLines), 2) + "");
if (decimalXLines != "" && counterX != 0)
{
removedXDecimalRichTextBox.AppendText("\n");
removedXDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalXLines), 2) + "");
}
counterX++;
}
foreach (string decimalYLines in myYLines)
{
if (counterY == 0)
removedYDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalYLines), 2) + "");
if (decimalYLines != "" && counterY != 0)
{
removedYDecimalRichTextBox.AppendText("\n");
removedYDecimalRichTextBox.AppendText(Math.Round(Convert.ToDouble(decimalYLines), 2) + "");
}
counterY++;
}