完美数字 1 到 n
编写一个算法来打印从 1 到 n 的完美数。要确定一个数字是否完美,请将该数字的所有小于该数字的因数相加。如果总和等于数字,那就完美了。
import java.util.Scanner;
public class Assign_6 {
public static void main(String[] args){
int num,number,sum=0,factor;
System.out.print("Enter Number");
Scanner keyboard = new Scanner (System.in);
number=keyboard.nextInt();
for (num=1;num<number;num++){
for(factor=1;factor<number;factor++){
if(num%factor==0){
sum= sum+factor;
}
if(sum==num){
System.out.println(sum);
}
sum=0;
}
}
}
}
输出:24
没有打印任何内容。不知道出了什么问题。我哪里错了?我只能使用 while、for 和 else-if 语句。
Write an algorithm that prints perfect numbers from 1 to n. To determine if a number is perfect add up all the factors of the number that are less than the number. If the sum is equal to the number, it is perfect.
import java.util.Scanner;
public class Assign_6 {
public static void main(String[] args){
int num,number,sum=0,factor;
System.out.print("Enter Number");
Scanner keyboard = new Scanner (System.in);
number=keyboard.nextInt();
for (num=1;num<number;num++){
for(factor=1;factor<number;factor++){
if(num%factor==0){
sum= sum+factor;
}
if(sum==num){
System.out.println(sum);
}
sum=0;
}
}
}
}
Output: 24
Nothing prints out. Don't know whats wrong. Where am I going wrong? I can only use while, for, and else-if statements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
打印出每个数字的总和(而不仅仅是当 sum==number 时),您就能算出来。
(两条线索:正确的缩进有助于快速找到匹配的 { } 对。并且为变量提供描述性名称是值得的。)
Print out the sum for every number (and not just when sum==number) and you'll be able to figure it out.
(Two clues: correct indentation helps find matching { } pairs quickly. And it pays to give your variables descriptive names.)
您的代码中有两个问题:
以下代码应该使用小于或等于比较,否则,您会错过
n
,只对(n-1)执行1
应该是下面的代码。
以下代码位于
for(factor=1;...)
内部,但它应该位于循环外部。否则,您实际上会检查 &每个因素的明确总和。There are two problems in your code:
The following code should use a less than or equal to comparison, otherwise, you miss the
n
, only do 1 to(n-1)
It should be the following code.
The following code is inside of
for(factor=1;...)
, but it should be outside of the loop. Otherwise, you actually check & clear sum, for each factor.在纸上追踪你的程序。您的 sum 变量有问题。
Trace through your program on paper. There is an issue with your sum variable.
当您开始编码时很容易混淆循环,因为在本作业中您有 2 个 for 循环,并且它们执行不同的操作。作为提示,您的程序无法运行的原因是因为您将一个 for 循环中的内容放错了到另一个 for 循环中。
您的方向是正确的,但请确保您能够单独考虑这些 for 循环。也就是说,内循环的预期目的是什么?它实际上在做什么?外循环的预期目的是什么?它实际上在做什么?看起来在你的内部循环中你正在尝试总结 num 的因子。它实际上只是总结因素,还是有一些您忽略的副作用/错误?
It's very easy when you are starting out coding to confuse your loops, since in this homework you have 2 for loops and they do different things. As a hint, the reason your program is not working is because you have misplaced stuff from one for loop to the other.
You are in the right direction, but make sure you are able to think about those for loops separately. That is, what is the inner loop's intended purpose? What is it actually doing? What is the outer loop's intended purpose, and what is it actually doing? It looks like in your inner loop you are trying to sum up factors for num. Is it actually just summing up the factors, or are there some side effects/errors you overlooked?
尝试这段代码它将帮助你:
Try this code it will help you:
}
}