逐渐打印整数的最后一位?
Print last digits of an integer gradually?
我正在尝试打印用户输入整数的最后一位数字。
例如,如果用户输入 5432
我的输出是
2个
32
432
5432。
我已经设法使用 while 循环为此编写了代码,但是我不明白为什么我的循环没有终止,请帮我终止它?
void main()
{
//declare variables
int input, output, modulu = 10;
//read input from user
cout << "Please enter a number: ";
cin >> input;
int test = input % modulu; // test checks if all the number has been printed
//disect number
while (test > 0);
{
output = input % modulu;
modulu = modulu * 10;
cout << endl << output << endl;
test = input % modulu;
}
}
只是
测试=输入/模数;
而不是 test = input % modulu;
test
对于任何输入 > 0
总是 > 0
你可以用不同的循环实现同样的效果:
int input, modulu = 1;
cout << "Please enter a number: ";
cin >> input;
do {
modulu *= 10;
cout << endl << (input % modulu) << endl;
} while ((input % modulu) != input);
你的第一个问题在这里:
while (test > 0);
;
终止 while 语句,代码将永远停留在 while 中。换句话说——下面的所有代码都不会被执行。删除 ;
你的第二个问题是你处理的方式 test
- 不要取模而是除以 10。像这样:
int main()
{
//declare variables
int input, output, modulu = 10;
//read input from user
cout << "Please enter a number: ";
cin >> input;
int test = input; // <------------- Just make test equal to the input
while (test > 0) // <------------- The ; removed
{
output = input % modulu;
modulu = modulu * 10;
cout << endl << output << endl;
test = test / 10; // <----------- Divide by 10
}
return 0;
}
注意上面的代码有一些零的问题,例如1001
将输出 1
1
1
1001
而不是 1
01
001
1001
。
您可以通过使用 string
而不是 int
的完全不同的方法来解决这个问题
喜欢:
int main()
{
//declare variables
string input;
//read input from user
cout << "Please enter a number: ";
cin >> input;
cout << input << endl;
int size = input.size();
int tmp = size;
while (tmp >= 0)
{
for (int t = tmp; t < size; t ++) cout << input[t];
cout << endl;
--tmp;
}
return 0;
}
对于初学者来说,while 语句后有一个分号。
while (test > 0);
^^^
所以如果输入的数字的最后一位不等于0,那么循环就是无限的。
但是,如果您删除分号,则条件无效,因为 test == 0
仅在最后一位数字等于 0 的情况下。
考虑到 C++ 中的 main
应具有 return 类型 int
。
程序可以这样看
#include <iostream>
int main()
{
while ( true )
{
const unsigned int Base = 10;
std::cout << "Please enter a non-negative number (0-exit): ";
unsigned int x;
if ( !( std::cin >> x ) || x == 0 ) break;
unsigned int y = x;
unsigned int modulo = 1;
do
{
modulo *= Base;
std::cout << x % modulo << std::endl;
} while ( y /= Base );
std::cout << std::endl;
}
}
如果要输入
123456789
0
那么输出会像
Please enter a non-negative number (0-exit): 123456789
9
89
789
6789
56789
456789
3456789
23456789
123456789
Please enter a non-negative number (0-exit): 0
我正在尝试打印用户输入整数的最后一位数字。 例如,如果用户输入 5432 我的输出是 2个 32 432 5432。 我已经设法使用 while 循环为此编写了代码,但是我不明白为什么我的循环没有终止,请帮我终止它?
void main()
{
//declare variables
int input, output, modulu = 10;
//read input from user
cout << "Please enter a number: ";
cin >> input;
int test = input % modulu; // test checks if all the number has been printed
//disect number
while (test > 0);
{
output = input % modulu;
modulu = modulu * 10;
cout << endl << output << endl;
test = input % modulu;
}
}
只是 测试=输入/模数; 而不是 test = input % modulu;
test
对于任何输入 > 0
你可以用不同的循环实现同样的效果:
int input, modulu = 1;
cout << "Please enter a number: ";
cin >> input;
do {
modulu *= 10;
cout << endl << (input % modulu) << endl;
} while ((input % modulu) != input);
你的第一个问题在这里:
while (test > 0);
;
终止 while 语句,代码将永远停留在 while 中。换句话说——下面的所有代码都不会被执行。删除 ;
你的第二个问题是你处理的方式 test
- 不要取模而是除以 10。像这样:
int main()
{
//declare variables
int input, output, modulu = 10;
//read input from user
cout << "Please enter a number: ";
cin >> input;
int test = input; // <------------- Just make test equal to the input
while (test > 0) // <------------- The ; removed
{
output = input % modulu;
modulu = modulu * 10;
cout << endl << output << endl;
test = test / 10; // <----------- Divide by 10
}
return 0;
}
注意上面的代码有一些零的问题,例如1001
将输出 1
1
1
1001
而不是 1
01
001
1001
。
您可以通过使用 string
而不是 int
喜欢:
int main()
{
//declare variables
string input;
//read input from user
cout << "Please enter a number: ";
cin >> input;
cout << input << endl;
int size = input.size();
int tmp = size;
while (tmp >= 0)
{
for (int t = tmp; t < size; t ++) cout << input[t];
cout << endl;
--tmp;
}
return 0;
}
对于初学者来说,while 语句后有一个分号。
while (test > 0);
^^^
所以如果输入的数字的最后一位不等于0,那么循环就是无限的。
但是,如果您删除分号,则条件无效,因为 test == 0
仅在最后一位数字等于 0 的情况下。
考虑到 C++ 中的 main
应具有 return 类型 int
。
程序可以这样看
#include <iostream>
int main()
{
while ( true )
{
const unsigned int Base = 10;
std::cout << "Please enter a non-negative number (0-exit): ";
unsigned int x;
if ( !( std::cin >> x ) || x == 0 ) break;
unsigned int y = x;
unsigned int modulo = 1;
do
{
modulo *= Base;
std::cout << x % modulo << std::endl;
} while ( y /= Base );
std::cout << std::endl;
}
}
如果要输入
123456789
0
那么输出会像
Please enter a non-negative number (0-exit): 123456789
9
89
789
6789
56789
456789
3456789
23456789
123456789
Please enter a non-negative number (0-exit): 0