从一种方法添加到另一种方法

从一种方法添加到另一种方法

本文介绍了从一种方法添加到另一种方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!

首先,我不知道这是否是100%正确的论坛所在地,如果不是,请指导我找到正确的。

First of all, i dont know if this is the 100% correct forum place, if its not, please guide me to the correct one.

解决问题:

我正在编程C#并试图从一种方法添加+1到另一种方法。我认为它应该每次都增加,但我写的代码只写出1(它不会增加)。

I am programming in C# and was trying to add +1 from one method to another. The thought is that it should increase for every time it does so, but the code i have written only writes out 1 (it does not increase).

我缺少的是什么?

这是代码:

我是新手,所以不确定这是否是正确的编码,但这是我可以得到的。

I am new to this so not sure if this is the correct coding at all but it was what i could get out.

推荐答案

价值的原因不增加是因为text函数中的循环永远不会更改score变量的值。

The reason that the value does not increment is because the loop in the text function never changes the value of the score variable.

您可以在循环外部初始化得分,然后在执行循环期间将其递增,如下所示 -

You can initialize score outside of the loop and then increment it during execution of the loop as follows -

        static void text()
        {
            int score = 1;

            for (int i = 0; i < 5; i++)
            {
                points(score);
                ++score;
            }
        }


此外,points函数可以使用传递的参数"x"。在WriteLine函数调用中。 无需将其分配给另一个变量。



Also, the points function can use the passed parameter "x" in the WriteLine function call.  There is no need to assign it to another variable.



这篇关于从一种方法添加到另一种方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 03:07