簡體   English   中英

Do-While 循環,但 while 語句的值在 Do 語句中

[英]Do-While Loops but the values for the while statement is inside the Do Statement

我正在嘗試通過編寫一個程序來學習 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;
    }
}

如果你更換

while ((Px != sumRx) || (Py != sumRy))

while (true)

代碼能編譯嗎?

如果沒有,那么請確保在 do-while 循環開始之前聲明了變量 Px、Py、sumRx、sumRy。

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

我將您的代碼放入 dotnetfiddle.net,並將 while 語句更改為

while((Px != sumRx) || (Py != sumRy))

Use of unassigned local variable 'Px' 為了避免出現此錯誤,您需要在開始循環之前為Px sumRx PysumRy分配一個值。 在循環上方,您可以將初始化更改為

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

此外,您的 break 語句不起作用,因為 break 語句適用於您當前的循環塊,它是您最內層的 for 循環,因此您正在跳出最內層的 for 循環,而不是跳出 while 循環。

為什么循環繼續下去的一般答案是您的 for 循環將檢查每個條件,這意味着它會循環 nrow * ncol 次數,即使它在第一次找到答案時也是如此。 這是假設在您運行的代碼中沒有注釋掉這一行。 (而 true 將永遠永遠循環下去)。

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

那里的 break 語句跳出最內部的 for 循環。 如果你想保持早期爆發,你還需要為所有循環編寫爆發邏輯。 總的來說,這非常難看,這是一個很好的例子,說明為什么嵌套循環和 if 語句會很快變得難看。 :)

希望有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM