【问题标题】:Alternating Odd Even or Even Odd in C#在 C# 中交替奇偶或偶奇
【发布时间】:2021-11-04 11:10:34
【问题描述】:

我创建了一个程序,该程序将用户的所有输入值相加,并在用户输入 0 或大于 101 时打印总和。这是我的代码:

int n, sum = 0;
do
{
    Console.Write("Enter a number:");
    n = int.Parse(Console.ReadLine());
    sum += n;
 }
 while (n != 0 && n < 101);
 Console.WriteLine("Sum is:" + sum);
 Console.ReadKey();

我试图弄清楚如何交替接受数字。例如,输入值为:4、7、8、3、6、1。如果用户输入两个连续的奇数或偶数,系统将不接受两个连续的奇数或偶数,否则将显示所有输入数字的总和。

【问题讨论】:

  • 如果你保存之前的输入值,你可以检查(previousValue % 2) == (currentValue % 2)
  • 您可以将所有用户输入存储在List<int> 中,并且您可以根据已经收到的输入执行任何类型的逻辑。

标签: c# while-loop do-while


【解决方案1】:

根据 Andrew an Peter 的建议,您可以添加列表以保存您以前的输入,并对当前和上一个数据进行一些检查以执行逻辑。

实现这一点的代码如下:

        //save input list
        List<int> inputNumbers = new List<int>();
       
        int n, sum = 0;
        do
        {
            Console.Write("Enter a number:");
            n = int.Parse(Console.ReadLine());
            //My Recomendation ==============================
                
                //save previus input numbers
                inputNumbers.Add(n);

                //Check is there are previous input number
                if(inputNumbers.Count>1){
                    //New control vars
                    Boolean previousNumberIsOdd= false,currentNumberIsOdd= false;

                    foreach (int item in inputNumbers)
                    {
                        Console.Write(item+",");
                    }
                    
                    //check if previus number is odd
                    if((inputNumbers[inputNumbers.Count-2])%2 == 0){
                        previousNumberIsOdd = true;
                    }

                    //Check if current number is odd
                    if(n%2==0){
                        currentNumberIsOdd = true;
                    }

                    Console.WriteLine("Control vars:" + previousNumberIsOdd +"/"+currentNumberIsOdd);
                    //Check diferent scenarios and do the logic
                    //previous and current number are odds
                    if(previousNumberIsOdd && currentNumberIsOdd){
                        
                        //break while and write the result
                        break;
                    }
                    //previous and current number are evens
                    if(!previousNumberIsOdd && !currentNumberIsOdd){
                        
                        //break while and write the result
                        break;
                    }
                }

            //if there aren't numbers to break the cycle then do the original logic 
            
            //End of my recomendation =====================
            
            sum += n;

        }
        while (n != 0 && n < 101);
        Console.WriteLine("Sum is:" + sum);
        Console.ReadKey();
        }

你可以对这段代码做很多优化,但是这样说在逻辑上是非常清晰的。

【讨论】:

    【解决方案2】:

    通过使用两个标志,您可以达到结果。

    int n, sum = 0;
    bool previous = false, current;
    bool init = true;
    
    do
    {
        Console.Write("Enter a number:");
        n = int.Parse(Console.ReadLine());
        current = n % 2 == 0;
        
        if( current == previous && !init)
            break;
    
        previous = current;
        init = false;
        sum += n;
     }
     while (n != 0 && n < 101);
     Console.WriteLine("Sum is:" + sum);
     Console.ReadKey();
    

    输出

    【讨论】:

      猜你喜欢
      • 2019-03-08
      • 2021-11-17
      • 2019-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多