Simple Java homework
So I'm a java newby and I'm doing this homework assignment that asks me to write a program to find the first four perfect numbers. I think I have everything roughly correct, though I am unable to test my program due to an error message that reads 'Exception in thread "main" javva.lang.NoSuchMethodError: main' when I try to run my program. It compiled fine and I think all the math is right:
import java.awt.*;
public class PerfectNumbers
{
int num = 1, divisor1 = 1, divisor2, divSum, perfNum1, perfNum2, perfNum3, perfNum4;
public void main()
{
do
{
while (num != divSum)
{
if (num % divisor1 == 0 && divisor1 != Math.sqrt(num))
{
divSum += divisor1;
divisor2 = num / divisor1;
divSum += divisor2;
divisor1++;
}
else if (num % divisor1 !=0 && divisor1 != Math.sqrt(num))
divisor1++;
else if (divisor1 == Math.sqrt(num))
{
divSum += Math.sqrt(num);
divisor1 = 1;
}
}
if (num == divSum && perfNum1 == 0)
{
perfNum1 = num;
num++;
divSum = 0;
}
else if (num == divSum && perfNum2 == 0)
{
perfNum2 = num;
num++;
divSum = 0;
}
else if (num == divSum && perfNum3 == 0)
{
perfNum3 = num;
num++;
divSum = 0;
}
else if (num == divSum && p开发者_如何学PythonerfNum4 == 0)
{
perfNum4 = num;
break;
}
}
while (perfNum4 == 0);
if (perfNum4 == 0)
System.out.print("The first four perfect numbers are "
+ perfNum1 + ", " + perfNum2 + ", " + perfNum3 + ", "
+ perfNum4 + ".");
}
}
P.S. any advice on newb mistakes on syntax or style would be greatly appreciated!
public void main()
Should be
public static void main(String[] args)
Please remember that the below are coding best practices, the program would still compile and run fine without making the below changes.
You are not using any awt components, hence you can remove that import statement.
write variable declaration and initializing statements only one variable per line. (refering to line #4. ). This eases debugging effort and maintainability.
int num = 1, divisor1 = 1, divisor2, divSum, perfNum1, perfNum2, perfNum3, perfNum4;
For the last if() statement, please provide the open and close parenthesis even there is a single line. This will improve code readability. So even if some other programmer takes up your code, will not confuse with the lines of code.
As others mentioned, you need to give static modifier for main method.
Two fixes based on your current code.
Fix One - move variables from the class scope to the scope of the main method
Currently your variables are sitting outside the main method but within the class. As the main
method is static you can't reference them as they are variables of each instance of the PerfectNumbers class (and you aren't creating an instance of the PerfectNumbers class).
Change:
int num = 1, divisor1 = 1, divisor2, divSum, perfNum1, perfNum2, perfNum3, perfNum4;
public static void main(String[] args)
{
to
public static void main(String[] args)
{
int num = 1, divisor1 = 1, divisor2, divSum, perfNum1, perfNum2, perfNum3, perfNum4;
Fix Two - initialise your variables
For primitive types (like int
), Java needs to be guaranteed that your variables have been initialised. As you are currently initialising variables within conditional statements, Java can't be 100% guaranteed that they will be initialised.
Change:
int num = 1, divisor1 = 1, divisor2, divSum, perfNum1, perfNum2, perfNum3, perfNum4;
to
int num = 1, divisor1 = 1, divisor2 = 1, divSum = 0, perfNum1 = 0, perfNum2 = 0, perfNum3 = 0, perfNum4 = 0;
Note: These changes will make your code compile but it isn't yet correct. As this is homework, try and see if you can make an attempt to fix it yourself.
A couple of cleanups to make things look a bit nicer
Cleanup One (as pointed out by @Sarma7)
Remove this line:
import java.awt.*;
As you aren't using any methods/variables from this package.
Cleanup Two
It's good style to initialise one variable per line (if declaring without initialising then you can probably leave it on the same line).
e.g.
int num = 1;
int divisor1 = 1;
int divisor2 = 1;
int divSum = 0;
int perfNum1 = 0;
int perfNum2 = 0;
int perfNum3 = 0;
int perfNum4 = 0;
A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself. Equivalently, a perfect number is a number that is half the sum of all of its positive divisors. The first perfect number is 6, because 1, 2 and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6.
public class IsPerfectNumber {
public boolean isPerfectNumber(int number){
int temp = 0;
for(int i=1;i<=number/2;i++){
if(number%i == 0){
temp += i;
}
}
if(temp == number){
System.out.println("It is a perfect number");
return true;
} else {
System.out.println("It is not a perfect number");
return false;
}
}
public static void main(String a[]){
IsPerfectNumber ipn = new IsPerfectNumber();
System.out.println("Is perfect number: "+ipn.isPerfectNumber(28));
}
}
Do this:
public static void main(String[] args) {
new PerfectNumbers().main();
}
All your fields are instance fields (ie non-static), so they can't be referenced from a static
method (they can only be referenced from an instance), so create a PerfectNumbers
object then call the main
method on it.
I think you should first learn some about OOP concept and then what static
means. You need a static main
method to start. Inside of the main method, you can use your other static fields, or first you must instantiate some objects then you can use the fields of that objects. Then catch @Bohemian's answer
精彩评论