添加一个do/while循环
Adding a do/while loop
我需要使用另一个!= 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
循环放在leap
和year
变量声明之前,并在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;
}
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- 如果条件为TRUE(最佳方式?),则在do while循环中后置增量
- 即使没有满足他们的条件,我也无法通过一些 do-while 循环
- 如何使用 do while 循环确定最高值和最低值
- do while 循环中的 if 语句以 yes 或 no 结尾
- 使用 while 或 do while 退出循环
- 带有开关语句的 do-while 循环 -- 无穷循环错误
- 为什么当我输入一个被接受的数字时,我的 do-while 循环没有中断?
- 为什么我的 Do-while 循环无法在C++中运行菜单
- 如果语句不会在 do - while 循环中运行
- 如何在c++中读取do-while循环中的空白
- 什么是'do while loop'的真实应用?
- 将嵌套的 for 循环更改为 do-while 循环
- 如何修复不会在退出选择时终止的 do-while 循环
- 为什么这个do-while循环每隔一个循环只写入数组?
- C++ do/while 循环和 if/else: 骰子游戏 -- 第 75 行错误:"Cout"之前的预期"While" -- 如何解决这个问题?
- 卡在链表中的无限 Do-while 循环中
- for(;;) vs do..while() for main program loop
- 试图调节我的 do-while 以停止在终端中退出程序.菜单不会根据需要循环使用当前语句
- 使用基本 do-while 循环求解泊松方程