用do while来改写
时间: 2023-11-10 18:13:30 浏览: 77
while循环:
```java
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
}
```
改写后的do while循环:
```java
int i = 0;
do {
System.out.println(i);
i++;
} while (i < 10);
```
相关问题
用do+while改写根据年份月份输出该月天数的程序,使程序能够判断10个年份月份的天数
使用do-while循环可以实现根据年份月份输出该月天数的程序,并且可以判断10个年份月份的天数。下面是使用do-while循环改写的程序:
```c++
#include <iostream>
using namespace std;
int main() {
int year, month;
int days;
int count = 1; do {
cout << "请输入第" << count << "个年份和月份:" << endl;
cin >> year >> month;
// 判断天数
if (month == 2) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
days = 29;
} else {
days = 28;
}
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
days = 30;
} else {
days = 31;
}
cout << year << "年" << month << "月的天数为:" << days << endl;
count++;
} while (count <= 10);
return 0;
}
```
把do while 改成for
可以将do while循环改写成for循环,例如:
```
int i = 0;
do {
// 循环体
i++;
} while (i < 10);
```
可以改写为:
```
for (int i = 0; i < 10; i++) {
// 循环体
}
```
这样就可以使用for循环来代替do while循环了。