Results 1 to 4 of 4

Thread: Java Method

  1. #1
    Haizaaa is offline Member
    Join Date
    Nov 2016
    Posts
    4
    Rep Power
    0

    Default Java Method

    Hi. I'm writing a method that used to determine whether the parameter is a pentagonal number or not. However, whether it is a pentagonal number or not, the method will return false.
    This is my code:
    Java Code:
    public class PentagonalNum {
        public static void main(String[]args){
            System.out.println(PentagonNum(5));
        }
        
        public static boolean PentagonNum(int n){
            int pent;
            for(int i=1; i<=0; i++){
                pent = (i*((3*i)-1))/2;
                if(pent == 0)
                    return true;
            }
        return false;    
        }
    }
    Is there something wrong with my method? I kinda confuse right now. Really appreciate if anyone could help me with this problem
    Last edited by Tolls; 11-02-2016 at 11:17 AM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default Re: Java Method

    When posting code please wrap it in code tags.
    I've done it for you this time.

    Your issue is here:
    Java Code:
    for(int i=1; i<=0; i++){
    That sets i to 1 and will only loop if i <= 0.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Java Method

    Are you required to use a loop? Otherwise, there is a simple test for determining if a number
    is a pentagonal number or not.

    Regards,
    Jim
    Last edited by jim829; 11-02-2016 at 06:26 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Haizaaa is offline Member
    Join Date
    Nov 2016
    Posts
    4
    Rep Power
    0

    Default Re: Java Method

    Thank you for your help I really appreciate it :)))

Similar Threads

  1. Replies: 11
    Last Post: 10-23-2013, 06:03 PM
  2. Replies: 7
    Last Post: 04-11-2013, 05:31 AM
  3. Java script call to a Java method
    By mitchj in forum New To Java
    Replies: 5
    Last Post: 04-26-2012, 05:54 PM
  4. Java method
    By javasm in forum New To Java
    Replies: 5
    Last Post: 03-16-2012, 01:42 PM
  5. Replies: 3
    Last Post: 02-09-2010, 05:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •