C# Do While循环,但While语句的值在Do语句中

C# Do While循环,但While语句的值在Do语句中,c#,loops,while-loop,do-while,C#,Loops,While Loop,Do While,我试图通过制作一个程序来学习C#,该程序可以找到两个系统达到平衡的值。我尝试了Do While循环,但无法建立While语句,因为While语句的值在Do语句中。它只是说“变量在当前上下文中不存在”。我希望我有道理 更新:完整的代码发布在下面。我尝试了while(true)语句并添加了if-break语句。我真的不知道if-break语句是否就是这样工作的。循环不断,没有尽头。我尝试打印这些值,但从第一个循环开始,它就已经满足了条件 下面是我的代码: using System; using Sy

我试图通过制作一个程序来学习C#,该程序可以找到两个系统达到平衡的值。我尝试了Do While循环,但无法建立While语句,因为While语句的值在Do语句中。它只是说“变量在当前上下文中不存在”。我希望我有道理

更新:完整的代码发布在下面。我尝试了while(true)语句并添加了if-break语句。我真的不知道if-break语句是否就是这样工作的。循环不断,没有尽头。我尝试打印这些值,但从第一个循环开始,它就已经满足了条件

下面是我的代码:

using System;
using System.Linq;

public class BoltCoord
{
    public double x1;
    public double y1;
}

public class Program
{
    public static void Main()
    {
        Console.Write("Number of Rows: ");
        int nrow = int.Parse(Console.ReadLine());

        Console.Write("Spacing of Rows: ");
        double srow = double.Parse(Console.ReadLine());

        Console.Write("Number of Columns: ");
        int ncol = int.Parse(Console.ReadLine());

        Console.Write("Spacing of Columns: ");
        double scol = double.Parse(Console.ReadLine());

        Console.Write("Eccentricity from CG: ");
        double ecc = double.Parse(Console.ReadLine());

        Console.Write("Rotation: ");
        double rot = double.Parse(Console.ReadLine());

        int totalbolts = nrow * ncol;

        var boltgroupCG = BoltCG(nrow, ncol, srow, scol); // bolt group cg from first bolt

        double[] xvaluesofbolts = new double[totalbolts]; //x-values of each bolt where first bolt is (0,0)
        for (int ctr = 0; ctr < totalbolts; ctr++)
        {
            xvaluesofbolts[ctr] = (ctr % ncol) * srow;
        }

        double[] yvaluesofbolts = new double[totalbolts]; //y-values of each bolt where first bolt is (0,0)
        for (int ctr = 1; ctr < totalbolts; ctr++)
        {
            yvaluesofbolts[ctr] = (ctr / ncol) * srow;
        }

        double[] FxOfBolts = new double[totalbolts];
        double[] FyOfBolts = new double[totalbolts];
        double[] MomOfBolts = new double[totalbolts];
        double[] XTransOfBolts = new double[totalbolts]; // Translated X
        double[] YTransOfBolts = new double[totalbolts]; // Translated Y

        double[] XLi = new double[totalbolts]; // Translated X
        double[] YLi = new double[totalbolts]; // Translated Y

        double[] BoltDist = new double[totalbolts]; // from IC
        double[] DeformBolts = new double[totalbolts];
        double[] ReactionForce = new double[totalbolts];
        double[] ReactionForceX = new double[totalbolts];
        double[] ReactionForceY = new double[totalbolts];
        double[] MomentBolt = new double[totalbolts];

        var IC = new BoltCoord(); // from CG

        double Px;
        double Py;
        double sumRx;
        double sumRy;
        double ctrX;
        double ctrY;

        do
        {
            for (ctrX = 0; ctrX < 50; ctrX = ctrX + 0.1)
            {
                for (ctrY = 0; ctrY < 50; ctrY = ctrY + 0.1)
                {
                    for (int El = 0; El < totalbolts; El++)
                    {
                        XTransOfBolts[El] = xvaluesofbolts[El] - boltgroupCG.x1;
                        YTransOfBolts[El] = yvaluesofbolts[El] - boltgroupCG.y1;
                        XLi[El] = XTransOfBolts[El] + ctrX;
                        YLi[El] = YTransOfBolts[El] + ctrY;
                        BoltDist[El] = Math.Sqrt(Math.Pow(XLi[El], 2) + Math.Pow(YLi[El], 2));

                        double MaxDist = BoltDist.Max();
                        // int MaxIndex = Array.IndexOf(BoltDist, MaxDist);
                        double delta = 0.34;
                        double Rult = 74;


                        DeformBolts[El] = delta * BoltDist[El] / MaxDist;
                        ReactionForce[El] = Rult * Math.Pow((1 - Math.Exp(-10 * DeformBolts[El])), 0.55);

                        ReactionForceX[El] = ReactionForce[El] * YLi[El] / BoltDist[El];
                        ReactionForceY[El] = ReactionForce[El] * XLi[El] / BoltDist[El];

                        MomentBolt[El] = ReactionForce[El] * BoltDist[El];

                        sumRx = ReactionForceX.Sum();
                        sumRy = ReactionForceY.Sum();
                        double sumMoment = MomentBolt.Sum();
                        double sumReax = ReactionForce.Sum();

                        double ro = (ecc + ctrX) * Math.Cos(Math.PI * rot / 180) + ctrY * Math.Sin(Math.PI * rot / 180);
                        Px = sumMoment / ro;
                        Py = Px * Math.Cos(Math.PI * rot / 180);

                        Console.WriteLine(Px + " " + sumRx + " " + Py + " " + sumRy); // tried printing the values to see if loop happens. 
                                                                                      // Px minus sumRx and Py minus sumRy are always less than 1000 but
                                                                                      // loop still continues?

                        if ((Px - sumRx <= 1000) && (Py - sumRy <= 1000))
                        {
                            break;
                        }

                    }
                }
            }
        } while (true); //((Px != sumRx) || (Py != sumRy));
    }

    public static BoltCoord BoltCG(int nrow, int ncol, double srow, double scol)
    {
         var BoltCGCoord = new BoltCoord
         {
             x1 = (ncol - 1) * scol * 0.5,
             y1 = (nrow - 1) * srow * 0.5
         };
         return BoltCGCoord;
    }
}
使用系统;
使用System.Linq;
公共级博尔特库德
{
公共双x1;
公共双y1;
}
公共课程
{
公共静态void Main()
{
Console.Write(“行数:”);
int nrow=int.Parse(Console.ReadLine());
控制台。写入(“行间距:”);
double srow=double.Parse(Console.ReadLine());
Console.Write(“列数:”);
int-ncol=int.Parse(Console.ReadLine());
控制台。写入(“列间距:”);
double scol=double.Parse(Console.ReadLine());
控制台。写入(“与CG的偏心度:”;
double ecc=double.Parse(Console.ReadLine());
控制台。写入(“旋转:”);
double-rot=double.Parse(Console.ReadLine());
int总螺栓=nrow*ncol;
var boltgroupCG=BoltCG(nrow,ncol,srow,scol);//第一个螺栓的螺栓组cg
double[]xvaluesofbolts=新的double[totalbolts];//第一个螺栓所在的每个螺栓的x值(0,0)
对于(int ctr=0;ctr
while ((Px != sumRx) || (Py != sumRy))

代码是否编译

如果没有,那么确保在do while循环开始之前声明了变量Px、Py、sumRx、sumRy

double Px, Py, sumRx, sumRy;
do
{
...

我将您的代码放入dotnetfiddle.net,并将while语句更改为

while((Px!=sumRx)| |(Py!=sumRy))
使用未分配的局部变量“Px”
。要避免此错误,您需要在启动循环之前为
Px
sumRx
Py
sumRy
分配一个值。在循环上方,您可以将初始化更改为

double Px=0;
双Py=0;
双sumRx=0;
双加总=0;
双ctrX=0;
双ctrY=0;
还有,哟
double Px, Py, sumRx, sumRy;
do
{
...
    } while (true); //((Px != sumRx) || (Py != sumRy));