使用开关函数c ++输入正整数或负整数列表以查明数字是偶数还是奇数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用开关函数c ++输入正整数或负整数列表以查明数字是偶数还是奇数相关的知识,希望对你有一定的参考价值。

#include <iostream>
using namespace std; 

限制是输入10个输入数字

const int LIMIT = 10;

int main ()
{

这只是声明变量

float counter ; 
int number ; 

int zeros;
int odds;
int evens;

cout << "Please enter " << LIMIT << "integers, "
     << "positive, negative, or zeros." << endl;

cout << "The numbers you entered are:" << endl;

从这里我试图打印出用户输入的所有数字,并检查它是奇数还是偶数

for (counter = 1; counter <= LIMIT; counter++)
{
    cin >> number;

switch(number % 2)
{
    case 0:

所以在这里我试图将零数作为偶数,所以在最后,如果列表为零,则偶数的输出将包括零

            if (number == 0 ){
                zeros++;
                evens++;
            }

    case 1:
        case -1:
        odds++;
}
}

cout << endl;

cout << "There are " << evens << " evens,"
     <<"which includes " << zeros << " zeros."
     <<endl;
cout << "The number of odd numbers is: " << odds
     << endl;

}
答案

应该初始化值:

int zeros = 0;
int odds = 0;
int evens = 0;

此外,evens的值必须增加偶数。在您的代码中,它仅在number == 0的情况下递增。

switch(number % 2)
{
    case 0:
        if (number == 0 ) { zeros++; }
        evens++; 
    break;

    case 1:
    case -1:
        odds++;
}

您还需要在break之后插入case 0

另一答案

我相信你的一个主要问题是你没有打破你的开关盒标签。例如:

int a = 7;
switch(a)
{
case 7: cout << "case 7
";
case 1: // This will execute if case 7 doesn't have a break statement.
}

在你的情况下:

for (counter = 1; counter <= LIMIT; counter++)
{
    cin >> number;

    switch(number % 2)
    {
    case 0: if (number == 0 )
          {
             zeros++;
             evens++;
          } // Falls straight through to the next cases. Counting evens as odds
    case 1:
    case -1: odds++;
    }
}

如果数字== 0,您只检测均匀,而不是数字模2等于0。

这是一个重写版本:

#include <iostream>

using namespace std;

const int LIMIT = 10;
int counter; // This is better as an int instead of a float
int number; // Also none of these variables need to be initialised
            // As they are guaranteed to be zero-initialised as global static variables
int zeros;
int odds;
int evens;

int main ()
{
    cout << "Please enter " << LIMIT << "integers, "
        << "positive, negative, or zeros." << endl;

        for (counter = 1; counter <= LIMIT; counter++)
        {
            cout << "Enter number " << counter << " : ";
            cin >> number;

                switch (number % 2)
                {
                case 0:
                    if (number == 0) zeros++; // If input equals 0, increment zero counter
                    evens++; // Increment evens if in this case label
                    break; // Break, don't want to increment odds
                default: odds++; // Default case, if not 0, must be odd.
                }
        }

    cout << endl;

    cout << "There are " << evens << " evens,"
        << "which includes " << zeros << " zeros." << endl;
    cout << "The number of odd numbers is: " << odds << endl;

    cin.ignore(); // This just discards the final '
' newline character
    cin.ignore(); // This blocks the program so the console doesn't close immediately

    return 0;
}

以上是关于使用开关函数c ++输入正整数或负整数列表以查明数字是偶数还是奇数的主要内容,如果未能解决你的问题,请参考以下文章

C语言编程:输入两个正整数m和n,求它们的最大公约数。

C语言编程:输入两个正整数m和n,求它们的最大公约数。

java 逆序输出整数

关于c语言超长正整数相加的问题,。求高手指教!!!!!

c语言编程利用自定义函数求两个数的和

Leetcode练习(Python):第367题:有效的完全平方数:给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。 说明:不要使用任何