English 中文(简体)
C# 每三分之一印刷一次。
原标题:C# print every third number!
  • 时间:2011-02-03 13:32:31
  •  标签:
  • c#

Help needed to perform an output in C# console application.

我拥有最高数目

int maxNr = 10;
int myValue = 1;

public void myMethod(){
int choice = int.Parse(Console.ReadLine());  // only 1 or 2 accepted.

//Only choice = 1 in displayed here.
if(choice == 1){
   while(myValue <= maxNr){
     Console.WriteLine(myValue);
     myValue = myValue + 3;
   }
}
}

Expected output: 1, 4, 7, 10 Next time the function is called the output should be: 3, 6, 9 2, 5, 8

问题回答

在发出第一次呼吁之后,我的瓦莱被困在13个地方,因此该法典没有第二次进入 lo。

在休息前添加:

if (myValue >= 10)
    myValue -= 10;

<><>Edit>:

1. 导言 如果我正确理解你,预期产出是:

1st call 1, 4, 7, 10.
2nd call: 3, 6, 9.
3rd call 2, 5, 8.

2. 结 论 如有些人建议的那样,你应使用<条码>代替<条码>。

if (myValue >= maxNr)
    myValue -= maxNr;

for (; myValue <= maxNr; myValue += 3)
{
    Console.WriteLine(myValue);
}

for (i=0; i<n; i+=3) no work for You?

为什么不只使用这一方法?

for (int i = n; i <= maxNr; i = i+3) {
    Console.WriteLine(i);
}

当地没有界定我的遗体,因此,在再次使用这一方法时,你需要将其确定为0,否则,它仍将是10,你不会进入。

public void myMethod(){
int choice = int.Parse(Console.ReadLine());  // only 1 or 2 accepted.

int maxNr = 10;
int myValue = choice;

//Only choice = 1 in displayed here.
if(choice == 1){
   while(myValue <= maxNr){
     Console.WriteLine(myValue);
     myValue = myValue + 3;
   }
}
}

每次重开起点。

您需要在一种临时变量中储存<代码>myValue,并在离开这一方法之前更新。 如我理解你的要求,实现产出的守则如下所示。

static int maxNr = 10;
static int myValue = 1;

private static void Test()
{
    int lastValue = myValue;
    int choice = int.Parse(Console.ReadLine());  // only 1 or 2 accepted.

    //Only choice = 1 in displayed here.
    if (choice == 1)
    {
        while (myValue <= maxNr)
        {
            Console.WriteLine(myValue);
            myValue = myValue + 3;
        }
    }

    if (lastValue == 1)
    {
        myValue = lastValue + 3 - 1;
    }
    else
    {
        myValue = lastValue - 1;
    }
}


static void Main(string[] args)
{
    Test();
    Test();
    Test();

Console.ReadLine();

}


Output
1
4
7
10

3
6
9

2
5
8

Note that the user needs to enter the value 1 at every function call.




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签