添加一个do/while循环

Adding a do/while loop

本文关键字:do while 循环 一个 添加      更新时间:2023-10-16

我需要使用另一个!= N ||另一个!= N来添加一个do/while循环到这段代码。问题是我非常讨厌do/while循环,我不太确定我应该把所有东西放在哪里。会是在转换之前吗?还是在整个输入模块之前?

//Compiler Directives
#include<iostream>
#include<cstdlib>
using namespace std;
//Constant Declarations
const int LOWERBOUND = 400;
const int UPPERBOUND = 2400;
//Main Body
int main(){
    //Local Identifiers
    int year, leapCode;
    //Input Module 
    cout<<"Please enter a year in the range 400 to 2400t";
    cin>>year;
    //Process Module
    if ((year >= LOWERBOUND)&& (year <= UPPERBOUND)){
        if (year%4!=0)
           leapCode = 1;
        else
           if (year%100!=0)
              leapCode = 2;
        else
          if (year%400!=0)
              leapCode = 3;
        else
              leapCode = 4;
        switch(leapCode){
              case 1:
                cout<<"The year "<<year<<" is not a leap yearn";
                break;
             case 2:
                cout<<"The year "<<year<<" is a leap yearn";
                break;
             case 3:
                cout<<"The year "<<year<<" is a century year but not a leap yearn";
                break;
             case 4:
                cout<<"The year "<<year<<" is a leap century year";
                break;  
        } //end switch
    }
    else
    cout<<"The year is out of rangen";
    system("PAUSE");
    return 0;
}

Do/While循环用于当您需要从0到N次循环代码块时,这意味着您需要运行给定的代码块至少一次,直到退出条件得到满足。

在您的情况下,您必须将do/while循环放在leapyear变量声明之前,并在system("PAUSE")行之后关闭它,基本上是您的main()方法的大部分。一旦你在你的开关块中确定了处理的结果,更新你的"另一个"变量(或者你想在do/while条件中使用的任何其他变量)。

通过这样做,你的程序将第一次进入do/while循环并打印你的输出字符串,然后将处理用户输入,一旦它到达循环结束,它将评估while子句中的循环条件,以确定代码块是否应该再次执行,如果是,你的程序将向用户请求另一个输入,然后它将处理它。

//Compiler Directives
#include < iostream >
#include < cstdlib >
using namespace std;
//Constant Declarations
const int LOWERBOUND = 400;
const int UPPERBOUND = 2400;
//Main Body
int main()
{
    //Local Identifiers
    do {
        int year, leapCode;
        //declare "another" variable
        //Input Module 
        cout << "Please enter a year in the range 400 to 2400t";
        cin >> year;
        //Process Module
        if ((year >= LOWERBOUND) && (year <= UPPERBOUND)) {
            if (year % 4 != 0)
                leapCode = 1;
            else
                if (year % 100 != 0)
                    leapCode = 2;
                else
                    if (year % 400 != 0)
                        leapCode = 3;
                    else
                        leapCode = 4;
            switch (leapCode) {
                case 1:
                    cout << "The year " << year << " is not a leap yearn";
                    //update "another" variable
                    break;
                case 2:
                    cout << "The year " << year << " is a leap yearn";
                    //update "another" variable
                    break;
                case 3:
                    cout << "The year " << year << " is a century year but not a leap yearn";
                    //update "another" variable
                    break;
                case 4:
                    cout << "The year " << year << " is a leap century year";
                    //update "another" variable
                    break;
            } //end switch
        }
        else
            cout << "The year is out of rangen";
            //update "another" variable
        system("PAUSE");
    } while (another != "N" || another != "n");
    return 0;
}