[英]print two digit numbers which are less than or equal to 56 and the sum of digits is greater than 10 JAVA
[英]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.