11 Replies - 1007 Views - Last Post: 20 February 2012 - 02:59 PM Rate Topic: ***-- 2 Votes

#1 harrypotter2  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 19-February 12

Perfect Number Problem :)

Posted 19 February 2012 - 10:44 AM

Hi,
I'm new a java and I was wondering what is wrong with this code here:
1) It says here that "increment" cannot be resolved to a type what does this mean
2) I don't have a clue what to do afterwards in order to figure out perfect numbers like 6 and such.


public class Perfect {
	public static void main(String[] args){
		int counter1;
		int counter2;
		while(counter1 <= 1000);
			counter1++;
		while (counter2 < counter1);
			if (counter2 == 0);
				increment total;
			if (total = counter1);
				System.out.print("It is a perfct number");			
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Perfect Number Problem :)

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Perfect Number Problem :)

Posted 19 February 2012 - 11:05 AM

A question is what did you mean to do with this statement?
increment total;

Total in not declared and also do increment.
Was This Post Helpful? 1
  • +
  • -

#3 harrypotter2  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 19-February 12

Re: Perfect Number Problem :)

Posted 19 February 2012 - 11:50 AM

OK I did what you told me but now I got a different error on line 10. Here it tells me a type mismatch what am I supposed to do?

if (total1 = counter1);

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,518
  • Joined: 23-August 08

Re: Perfect Number Problem :)

Posted 19 February 2012 - 11:52 AM

= for assignment, == for comparison

Also, lose the ; on the lines with ifs and whiles, or when (or if) you get this to run you'll have a whole new set of problems.
Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Perfect Number Problem :)

Posted 19 February 2012 - 11:56 AM

First fix what JackOfAllTrades told you, then post our updated code when you do changes.
Was This Post Helpful? 0
  • +
  • -

#6 harrypotter2  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 19-February 12

Re: Perfect Number Problem :)

Posted 19 February 2012 - 12:11 PM

What else am I supposed to do after this? and, How do I get it to print every perfect number from 1 to 1000 because I do not know how to?

public class Perfect {
	public static void main(String[] args){
		int counter1 = 0;
		int counter2 = 0;
		while(counter1 <= 1000){
			counter1++;
		}
		while (counter2 < counter1){
			if (counter2 == 0){
				int increment; 
				int total1 = 0;
			if (total1 == counter1){
				System.out.print("It is a perfct number");
				
				
				}
			}	
		}	
	}
}

Was This Post Helpful? 0
  • +
  • -

#7 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Perfect Number Problem :)

Posted 19 February 2012 - 02:40 PM

Perhaps we should step back and consider the logic here. Your job is write a program to print all the perfect numbers from 1 to 1000, right? A perfect number is defined by wikipedia to be "a positive integer that is equal to the sum of its proper positive divisors." At this point I already see a way you can make this program easier on yourself. You can create a method to determine if a number is perfect.

boolean isPerfect(int number) { }

Now, that is for you to figure out as part of your assignment but let's see how this could help us in our main method:
public static void main(String[] args) {
    int iterations = 1000; // what number will we go to? 1000
    for(int i = 1; i < iterations+1; i++) { // Loop 1000 times starting at 1
        if( isPerfect(i) ) {
            System.out.println(i + " is a perfect number!"); // Display that this number is perfect
        }
    }
}
That main method looks very simple and all you have to do now is figure out how to tell if a number is perfect or not. Do you think you could handle that?

This post has been edited by Sheph: 19 February 2012 - 02:41 PM

Was This Post Helpful? 1
  • +
  • -

#8 harrypotter2  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 19-February 12

Re: Perfect Number Problem :)

Posted 19 February 2012 - 04:18 PM

Yeah but now i get a syntax error.


public class Perfect {
	public static void main(String[] args){
		int counter1 = 0;
		int counter2 = 0;
		boolean isPerfect(int number);
int iterations = 1000; // what number will we go to? 1000
    for(int i = 1; i < iterations+1; i++)
		if( isPerfect(i) ) {
            System.out.println(i + " is a perfect number!"); // Display that this number is perfect
        }
		while(counter1 <= 1000){
			counter1++;
		while (counter2 < counter1){
			if (counter2 == 0){
				int increment; 
				int total1 = 0;
			if (total1 == counter1){
				System.out.print("It is a perfct number");
				
				 }
			  }	
		   }	
		}	
	}

Was This Post Helpful? 0
  • +
  • -

#9 The_Programmer-  Icon User is offline

  • Death Scythe
  • member icon

Reputation: 24
  • View blog
  • Posts: 593
  • Joined: 24-October 11

Re: Perfect Number Problem :)

Posted 19 February 2012 - 05:05 PM

You should read a book on Java or go to thenewboston.com and watch the Java tutorials. It seems that you have not learned much Java. In a normal class(this means non-abstract classes) you can not just write
boolean isPerfect(int number);
You need to have a method body like this
boolean isPerfect(int number) {
//do stuff here and return a boolean value based on if the number is a perfect number.

}


Was This Post Helpful? 1
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Perfect Number Problem :)

Posted 19 February 2012 - 05:58 PM

A reference from the snippet section.
Was This Post Helpful? 0
  • +
  • -

#11 harrypotter2  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 19-February 12

Re: Perfect Number Problem :)

Posted 20 February 2012 - 11:40 AM

Now heres my next problem. what should i put in the curly brackets? Give me an example or somthing.




public class Perfect {
	public static void main(String[] args){
		boolean isPerfect (int number){
			
		}
		int iterations = 1000; // what number will we go to? 1000
		for(int i = 1; i < iterations+1; i++) { // Loop 1000 times starting at 1
		if(isPerfect(i)){
			System.out.println(i + " is a perfect number!"); // Display that this number is perfect
			
	 }	
    }
  }
}

Was This Post Helpful? 0
  • +
  • -

#12 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,020
  • Joined: 12-October 11

Re: Perfect Number Problem :)

Posted 20 February 2012 - 02:59 PM

boolean isPerfect() is a method, you need to declare it outside of main(), and if you want to use it in main without creating an object, then you have to declare it as "static". Since you don't know what an object is probably, then I suggest you keep it static for now. Inside the curly brackets should test to see if the variable number is a perfect number. You have basically copy and pasted the code I wrote so far, so let's see some attempts on your part, even if they are wrong. If you show me you can come up with something on your own, I would be glad to continue to help you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1