简体   繁体   English

Do-While 循环,但 while 语句的值在 Do 语句中

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

I am trying to learn C# by making a program that finds two values in which the system becomes equilibrium.我正在尝试通过编写一个程序来学习 C#,该程序可以找到系统处于平衡状态的两个值。 I tried the Do-While loop but I can't establish the while statement since the values for the while statement is inside the Do-statement.我尝试了 Do-While 循环,但我无法建立 while 语句,因为 while 语句的值在 Do-语句中。 It just says "The variables does not exist in the current context".它只是说“变量在当前上下文中不存在”。 I hope I make sense.我希望我说得通。

Update: The complete code is posted below.更新:完整的代码发布在下面。 I tried the while (true) statement and added a if-break statement.我尝试了 while (true) 语句并添加了一个 if-break 语句。 I don't really know if this is how the if-break statement works.我真的不知道这是否是 if-break 语句的工作方式。 The loop goes on and on without end.循环继续下去,没有尽头。 I tried printing the values but from the very first loop, it already satisfies the conditions.我尝试打印值,但从第一个循环开始,它就已经满足条件了。

Below is my code:下面是我的代码:

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;
    }
}

If you replace如果你更换

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

with

while (true)

does the code compile?代码能编译吗?

If not, then make sure you declared the variables Px, Py, sumRx, sumRy before the start of the do-while loop.如果没有,那么请确保在 do-while 循环开始之前声明了变量 Px、Py、sumRx、sumRy。

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

I put your code into dotnetfiddle.net, and the error I get when changing the while statement to我将您的代码放入 dotnetfiddle.net,并将 while 语句更改为

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

is Use of unassigned local variable 'Px' .Use of unassigned local variable 'Px' To not get this error you need to assign a value to Px sumRx Py and sumRy before starting the loop.为了避免出现此错误,您需要在开始循环之前为Px sumRx PysumRy分配一个值。 Above the loop you can just change your initialization to在循环上方,您可以将初始化更改为

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

Also, your break statement isn't working because the break statement applies to your current loop block which is your innermost for loop, so you are breaking out of the innermost for loop, and not breaking out of the while loop.此外,您的 break 语句不起作用,因为 break 语句适用于您当前的循环块,它是您最内层的 for 循环,因此您正在跳出最内层的 for 循环,而不是跳出 while 循环。

The general answer to why the loop goes on and on is that your for loop is going to check every condition, which means that it will loop nrow * ncol number of times even if it does find the answer on the first time through.为什么循环继续下去的一般答案是您的 for 循环将检查每个条件,这意味着它会循环 nrow * ncol 次数,即使它在第一次找到答案时也是如此。 This is assuming that in the code you're running this line isn't commented out.这是假设在您运行的代码中没有注释掉这一行。 (while true will totally loop forever and ever). (而 true 将永远永远循环下去)。

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

The break statement in there bails out of the inner most for loop.那里的 break 语句跳出最内部的 for 循环。 If you want to keep breaking out early you'll need to also write break out logic for all the loops.如果你想保持早期爆发,你还需要为所有循环编写爆发逻辑。 In general that's pretty ugly though and it is a great example of why nesting loops and if statements can get ugly very fast.总的来说,这非常难看,这是一个很好的例子,说明为什么嵌套循环和 if 语句会很快变得难看。 :) :)

Hope that helps.希望有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM