[英]find all four digit numbers for which the square of the sum of the first two digits and the last two digits equal the number itself
The program that I wrote it is not working and I really don't know why.我编写的程序不起作用,我真的不知道为什么。 I would really appreciate your help.
我将衷心感谢您的帮助。 Thank you
谢谢
for (int i = 1000; i <= 9999; i++){
int n = i;
int remandier1, remandier2,finalanswer;
double result1=0;
while(n != 0){
remandier1 = n % 100;
remandier2 = n /100;
finalanswer = remandier1 + remandier2;
result1 = Math.pow(finalanswer, 2);
}
if (result1 == n){
System.out.println(i);
}
}
So this program should execute all 4 digit numbers,the square of the sum of the first two digits and the last two digits should be equal to the number it self but mine it does nothing.所以这个程序应该执行所有 4 位数字,前两位数字和最后两位数字之和的平方应该等于它自己的数字,但我的它什么也不做。
You are using a loop based on n
being different from 0, but n
is never modified during the loop.您正在使用基于
n
不同于 0 的循环,但n
在循环期间永远不会被修改。 How would the loop could actually stop.. looping?循环如何真正停止..循环? May be I'm missing something, but as I see it,
n
will forever be equal to the set value of i
.可能是我遗漏了一些东西,但正如我所见,
n
将永远等于i
的设定值。
while(n != 0)
{
remandier1 = n % 100;
remandier2 = n /100;
finalanswer = remandier1 + remandier2;
result1 = Math.pow(finalanswer, 2);
// add something to stop the loop
n = n - 1; // for example
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.