在c++中,如果不能被5整除,则移动到while循环

Moving to a while loop if not divisible by 5 in c++

本文关键字:整除 移动 while 循环 c++ 如果 如果不 不能      更新时间:2023-10-16

我正在为一个类编写一个程序,其中用户输入一个数字,该数字被检查为偶数,检查它是否能被5整除,如果这两个都满足,则该数字将被while循环使用。澄清一下,如果数字是偶数且能被5整除,那么代码运行,如果它是偶数且能被2整除,那么代码运行。第一组代码需要在for循环中,第二组代码(可被2整除)需要在while循环中。

我被困在取cin并在第二组代码中使用它,第一组想先操作它。

任何帮助弄清楚这将是伟大的!

代码:

#include <iostream>
#include <string>
#include <fstream>    
#include <iomanip>
using namespace std;
int main ()
{
    system ("color F0");
    int number = 0, countIf = 0,countWhile = 0, i=0;
    cout << "Please enter a positive integer or zero to quit: ";
    cin >> number;
    cin.ignore();
    if (number < 0 || number%2 != 0)
        cout << "Error: The integer entered was either not positive or a zero. Try again." << endl;
    //else if (number%5 || (number%2) = 0)
    for (number%5; countIf < number; countIf +=5)
    {
        i = number / 10;
        i += countIf;
        cout << i << " ";        
    }
    cin.ignore();
    while (countWhile <= i && i%2) 
    {
        int x = 0;
        cout << x << " ";
        x += 2;
    }
    system ("pause");
    return 0;
}

我在看你原来的else if语句

else if (number%5 || (number%2) = 0)

你想要的是

else if (number%5 == 0 && number%2 == 0)

并在else if块中执行代码

在操作数据之前将其复制到另一个变量

cin>>a;
b=a;

并使用b作为第二组。如果一个数是偶数,它显然能被2整除。所以你不需要检查一个数是否为偶数以及它是否能被2整除