简体   繁体   English

循环10次添加输入,在C#中使用for和do while循环?

[英]Looping 10 times adding input, with for and do while loop in C#?

I'm new taking a basic C# course and I'm having trouble getting an assignment to work. 我是一名基础C#课程的新手,我在完成工作任务时遇到了麻烦。 I built a basic calculator and it works fine. 我建了一个基本的计算器,它工作正常。 Now I had to add a new button called "Sum" that will take an input from one of my boxes (number1Txtbox) and add it to itself 10 times through a loop. 现在我必须添加一个名为“Sum”的新按钮,它将从我的一个盒子(number1Txtbox)中获取输入,并通过循环将其添加到自身10次。

I poured through pages of my c# book and can't figure this one out. 我倒了我的c#书的页面,无法想出这个。 I figured out how to initialize the loop with the counter etc, I just can't get this to work for the life of me. 我想出了如何使用计数器等初始化循环,我只是无法让它在我的生活中工作。

I was told to use a for loop, then switch to a do while loop. 有人告诉我使用for循环,然后切换到do while循环。 Which doesn't really make sense to me, I assumed I could do this with just a for loop. 这对我来说真的没有意义,我以为我可以用for循环做到这一点。 So my question is: 所以我的问题是:

1) Do I even need to switch to a do while loop to do this? 1)我甚至需要切换到do while循环来执行此操作吗?
2) What am I doing wrong? 2)我做错了什么?

Here is what I have so far and it just makes my program freeze when I attempt to hit the sum button after putting a number in the textbox: 这是我到目前为止所做的,当我在文本框中输入一个数字后尝试点击总和按钮时,它只会使我的程序冻结:

private void sumBtn_Click(object sender, EventArgs e)
{
    int counter;
    int loopAnswer;
    int number1;

    number1 = int.Parse(number1Txtbox.Text);

    for (counter = 1; counter <= 10; counter++)
    {
        loopAnswer = number1 + number1;
        do
        {
            loopAnswer = loopAnswer + number1;
        } while (counter <= 10);

        equalsBox.Text = loopAnswer.ToString();
    }
}

Thanks guys! 多谢你们!

You mixing things. 你混合的东西。 You either do this: 你要么这样做:

private void sumBtn_Click(object sender, EventArgs e)
{
    int counter;
    int loopAnswer = 0;
    int number1 = int.Parse(number1Txtbox.Text);

    for (counter = 1; counter <= 10; counter++)
    {
        loopAnswer += number1; //same as loopAnswer = loopAnswer + number1;
    }
    equalsBox.Text = loopAnswer.ToString();
}

or this: 或这个:

private void sumBtn_Click(object sender, EventArgs e)
{
    int counter = 1;
    int loopAnswer = 0;
    int number1 = int.Parse(number1Txtbox.Text);

    do
    {
        loopAnswer += number1; //same as loopAnswer = loopAnswer + number1;
        counter++;
    } while (counter <= 10);


    equalsBox.Text = loopAnswer.ToString();

}

Also, the final answer ( equalsBox.Text = loopAnswer.ToString(); ) should be out of the loop. 此外,最终答案( equalsBox.Text = loopAnswer.ToString(); )应该不在循环中。

It freezes because when it enters the do while loop, the counter is never changed. 它会冻结,因为当它进入do while循环时, counter永远不会改变。 If it's never changed, counter <= 10 is always true so you get an infinite loop. 如果它永远不会改变,则counter <= 10始终为真,因此您将获得无限循环。 It is stuck there. 它被困在那里。

private void sumBtn_Click(object sender, EventArgs e)
{
    //these should default to 0, but we should to it explicitly, just in case.
    int loopAnswer = 0;
    int number1;

    if(int.TryParse(number1Txtbox.Text, out number1)
    {
        for (counter = 1; counter <= 10; counter++)
        {
            loopAnswer += number1;
        }

        equalsBox.Text = loopAnswer.ToString();
    }
    else
        equalsBox.Text = "Not A Number";
}

The TryParse here is just good practice. 这里的TryParse只是一个好习惯。 It takes care of a situation where you would have text input. 它会处理您需要输入文本的情况。 A try catch block could also be used. 也可以使用try catch块。

private void sumBtn_Click(object sender, EventArgs e)
{
    int counter;
    int loopAnswer = 0;
    int number1;

    number1 = int.Parse(number1Txtbox.Text);


    for (counter = 1; counter <= 10; counter++)
    {
            loopAnswer += number1;
    }


equalsBox.Text = loopAnswer.ToString();

}

Your program freezes because 你的程序会冻结,因为

        do
        {
            loopAnswer = loopAnswer + number1;
        } while (counter <= 10);

Doesn't update the counter variable at all. 根本不更新计数器变量。 Therefore counter is never going to reach 10 so this loop is never going to exit. 因此,计数器永远不会达到10,所以这个循环永远不会退出。

to sum in a while loop do this 在一个while循环中求和这样做

counter = 1;
do {
    loopAnswer += number1;
    counter++;
} while(counter <= 10);

Your inner loop (while) is running endlessly because counter is never increased, that's why your program hangs. 你的内循环(while)无休止地运行,因为计数器永远不会增加,这就是你的程序挂起的原因。 Set a breakpoint on a line to be able to debug your program and gain a better understanding of how loops work. 在一行上设置断点,以便能够调试程序并更好地理解循环的工作方式。

To solve this assignment you only need one loop, definitly not nested loops. 要解决此分配,您只需要一个循环,绝对不是嵌套循环。 It doesnt matter what kind of looping mechanism you use. 你使用什么样的循环机制并不重要。

This code cause an infinite loop (that is the reason of the freeze): 此代码导致无限循环(这是冻结的原因):

for (counter = 1; counter <= 10; counter++)
{
    loopAnswer = number1 + number1;
    do
    {
        loopAnswer = loopAnswer + number1;
    } while (counter <= 10);

    equalsBox.Text = loopAnswer.ToString();
}

Infact here you're looping from 1 to 10, and for each iteration you perform loopAnswer = loopAnswer + number1; 事实上,你在这里循环从1到10,并且每次迭代你执行loopAnswer = loopAnswer + number1; until the condition counter <= 10 becomes false. 直到条件counter <= 10变为假。 But that never happens since in your do-while the counter variable doesn't change and so the program remains forever in the first iteration. 但这种情况从未发生,因为在你做的时候,计数器变量不会改变,所以程序在第一次迭代中永远存在。

I think you should get rid of the inner do-while and put equalsBox.Text = loopAnswer.ToString(); 我认为你应该摆脱内在的do-while并把equalsBox.Text = loopAnswer.ToString(); outside the for-loop. 在for循环之外。

For the sake of examples, let's say that number1 = 4. When you execute the line loopAnswer = number1 + number1; 为了举例,我们假设number1 = 4.当你执行line loopAnswer = number1 + number1; the resulting value of loopAnswer will always be 8. If you wanted loopAnswer to increment, then you should use loopAnswer = loopAnswer + number1; loopAnswer的结果值将始终为8.如果您希望loopAnswer递增,那么您应该使用loopAnswer = loopAnswer + number1; , or the shorthand syntax loopAnswer += number1; ,或简写语法loopAnswer += number1;

Regarding the use of a for loop versus a do-while , I'm guessing that it's not a matter of using both loops at the same time, it's a matter of using a for loop to illustrate the concept and then switching to use a do-while loop to illustrate the concept. 关于for循环与do-while ,我猜测这不是同时使用两个循环的问题,而是使用for循环来说明概念然后切换到使用do-while循环来说明这个概念。

You could complete this exercise using a for loop like this: 您可以使用这样的for循环完成此练习:

for (counter = 1; counter <= 10; counter++)  
{  
    loopAnswer += number1; 
}

equalsBox.Text = loopAnswer.ToString();  

You could also accomplish the same functionality using a do-while loop like this: 您还可以使用do-while循环完成相同的功能,如下所示:

int counter = 1;
do  
{  
    loopAnswer += number1;  
    counter++;
} while (counter <= 10);  

equalsBox.Text = loopAnswer.ToString();  

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

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