我坚持这个程序。程序的作用是从用户那里取一个整数并显示切割偶数后留下的所有数字。
int main(){
long n, minder=0;
int cdonr, power=1;
cout<<"Give a positive number "<<endl;
cin>>n;
while (n>0){
cdonr=n%10;
if(cdonr % 2 != 0){
minder=minder+cdonr*power;
power=power*10;
}
n=n/10;
}
cout<<"The number that is left after all even number " << endl;
cout<<minder<<endl;
cout<<"Give a positive nr "<<endl;
cin>>n;
}
有人可以帮我解决这个问题,因为在拆分第一个数字之后,它在第二个数字上没有回应。
感谢这个社区的人们,这就是答案
#include<iostream>
using namespace std;
int main() {
long n, minder = 0;
int cdonr, power = 1;
cout << "Give a positive number " << endl;
cin >> n;
while (n > 0) {
while (n > 0) {
cdonr = n % 10;
if (cdonr % 2 != 0) {
minder = minder + cdonr * power;
power = power * 10;
}
//always zero so we need a nested while loop
n = n / 10;
}
cout << "The number that is left after all even number " << endl;
cout << minder << endl;
//have to set values to default so they do not hold previous values
minder = 0;
cdonr = 0;
power = 1;
//we can reprompt user for a value
//n will get set and it validates with the outer while loop
//this allows it to run as many times as valid inputs
cout << "Give a positive number " << endl;
cin >> n;
}
}
答案 0 :(得分:1)
试试这个,它不是无限的,你可以通过调用函数多次这样做。
#include<iostream>
using namespace std;
void function(long n, long minder, int cdonr, int power) {
while (n > 0) {
cdonr = n % 10;
if (cdonr % 2 != 0) {
minder = minder + cdonr * power;
power = power * 10;
}
n = n / 10;
}
cout << "The number that is left after all even number " << endl;
cout << minder << endl;
}
int main() {
long n, minder = 0;
int cdonr, power = 1;
//can add a while loop here or just use a for
// if you know how many times you want
cout << "Give a positive number " << endl;
cin >> n;
if(n > 0){
function(n, minder, cdonr, power);
}
//probably add an else here in case of == 0 or < 0
// or just loop back if while
}
答案 1 :(得分:1)
用户请求的另一个答案,退出== 0, <= 0, and not a number
#include<iostream>
using namespace std;
int main() {
long n, minder = 0;
int cdonr, power = 1;
cout << "Give a positive number " << endl;
cin >> n;
while (n > 0) {
while (n > 0) {
cdonr = n % 10;
if (cdonr % 2 != 0) {
minder = minder + cdonr * power;
power = power * 10;
}
//always zero so we need a nested while loop
n = n / 10;
}
cout << "The number that is left after all even number " << endl;
cout << minder << endl;
//have to set values to default so they do not hold previous values
minder = 0;
cdonr = 0;
power = 1;
//we can reprompt user for a value
//n will get set and it validates with the outer while loop
//this allows it to run as many times as valid inputs
cout << "Give a positive number " << endl;
cin >> n;
}
}
答案 2 :(得分:0)
如果确实希望永远做某事,请循环播放。 即把你的&#34;功能&#34;在循环中重复。 (并考虑将其拉出来作为一个实际的功能)
int main() {
using namespace std;
while (true)
{
long n, minder = 0;
int cdonr, power = 1;
cout << "Give a positive number " << endl;
cin >> n;
while (n > 0) {
cdonr = n % 10;
if (cdonr % 2 != 0) {
minder = minder + cdonr*power;
power = power * 10;
}
n = n / 10;
}
cout << "The number that is left after all even number " << endl;
cout << minder << endl;
}
}