繁体   English   中英

找出前两位数字和后两位数字之和的平方等于数字本身的所有四位数字

[英]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

我编写的程序不起作用,我真的不知道为什么。 我将衷心感谢您的帮助。 谢谢

         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);
             }
         }

所以这个程序应该执行所有 4 位数字,前两位数字和最后两位数字之和的平方应该等于它自己的数字,但我的它什么也不做。

您正在使用基于n不同于 0 的循环,但n在循环期间永远不会被修改。 循环如何真正停止..循环? 可能是我遗漏了一些东西,但正如我所见, 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM