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

问题描述 投票:-1回答:2
#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;

}
c++ switch-statement
2个回答
0
投票

应该初始化值:

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


0
投票

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

int a = 7;
switch(a)
{
case 7: cout << "case 7\n";
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 '\n' newline character
    cin.ignore(); // This blocks the program so the console doesn't close immediately

    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.