Monday, April 15, 2013

Determine Perfect Number, Error With Infinite Loop - Java | Dream.In ...


Example
#{example}
"); ipb.editor_values.get('templates')['togglesource'] = new Template("
??? Cancel Source Edit
"); ipb.editor_values.get('templates')['toolbar'] = new Template(""); ipb.editor_values.get('templates')['button'] = new Template("
  • Icon
  • "); ipb.editor_values.get('templates')['menu_item'] = new Template("
  • #{title}
  • "); ipb.editor_values.get('templates')['togglesource'] = new Template("
    ??? Cancel Source Edit
    "); ipb.editor_values.get('templates')['emoticons_showall'] = new Template(""); ipb.editor_values.get('templates')['emoticon_wrapper'] = new Template("

    Emoticons

    "); // Add smilies into the mix ipb.editor_values.set( 'show_emoticon_link', false ); ipb.editor_values.set( 'bbcodes', $H({"snapback":{"id":"1","title":"Post Snap Back","desc":"This tag displays a little linked image which links back to a post - used when quoting posts from the board. Opens in same window by default.","tag":"snapback","useoption":"0","example":"[snapback]100[/snapback]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"topic":{"id":"5","title":"Topic Link","desc":"This tag provides an easy way to link to a topic","tag":"topic","useoption":"1","example":"[topic=1]Click me![/topic]","switch_option":"0","menu_option_text":"Enter the topic ID","menu_content_text":"Enter the title for this link","single_tag":"0","optional_option":"0","image":""},"post":{"id":"6","title":"Post Link","desc":"This tag provides an easy way to link to a post.","tag":"post","useoption":"1","example":"[post=1]Click me![/post]","switch_option":"0","menu_option_text":"Enter the Post ID","menu_content_text":"Enter the title for this link","single_tag":"0","optional_option":"0","image":""},"spoiler":{"id":"7","title":"Spoiler","desc":"Spoiler tag","tag":"spoiler","useoption":"0","example":"[spoiler]Some hidden text[/spoiler]","switch_option":"0","menu_option_text":"","menu_content_text":"Enter the text to be masked","single_tag":"0","optional_option":"0","image":""},"acronym":{"id":"8","title":"Acronym","desc":"Allows you to make an acronym that will display a description when moused over","tag":"acronym","useoption":"1","example":"[acronym='Laugh Out Loud']lol[/acronym]","switch_option":"0","menu_option_text":"Enter the description for this acronym (EG: Laugh Out Loud)","menu_content_text":"Enter the acronym (EG: lol)","single_tag":"0","optional_option":"0","image":""},"hr":{"id":"12","title":"Horizontal Rule","desc":"Adds a horizontal rule to separate text","tag":"hr","useoption":"0","example":"[hr]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"1","optional_option":"0","image":""},"php":{"id":"14","title":"PHP Code","desc":"Allows you to enter PHP code into a formatted/highlighted syntax box","tag":"php","useoption":"0","example":"[php]$variable = true;\n\nprint_r($variable);[/php]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"html":{"id":"15","title":"HTML Code","desc":"Allows you to enter formatted/syntax-highlighted HTML code","tag":"html","useoption":"0","example":"[html]\n \n[/html]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"sql":{"id":"16","title":"SQL Code","desc":"Allows you to enter formatted/syntax-highlighted SQL code","tag":"sql","useoption":"0","example":"[sql]SELECT p.*, t.* FROM posts p LEFT JOIN topics t ON t.tid=p.topic_id WHERE t.tid=7[/sql]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"xml":{"id":"17","title":"XML Code","desc":"Allows you to enter formatted/syntax-highlighted XML code","tag":"xml","useoption":"0","example":"[xml]\n \n Test\n \n[/xml]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"member":{"id":"31","title":"Member","desc":"Given a member name, a link is automatically generated to the member's profile","tag":"member","useoption":"1","example":"[member=skyhawk133] runs this site.","switch_option":"0","menu_option_text":"Input Username of Member","menu_content_text":"","single_tag":"1","optional_option":"0","image":"memberbbcode.png"},"extract":{"id":"33","title":"Extract Blog Entry","desc":"This will allow users to define an extract for an entry. Only this piece of the entry will be displayed on the main blog page and will show up in the RSS feed.","tag":"extract","useoption":"0","example":"[extract]This is an example![/extract]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"blog":{"id":"34","title":"Blog Link","desc":"This tag provides an easy way to link to a blog.","tag":"blog","useoption":"1","example":"[blog=100]Click me![/blog]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"entry":{"id":"35","title":"Blog Entry Link","desc":"This tag provides an easy way to link to a blog entry.","tag":"entry","useoption":"1","example":"[entry=100]Click me![/entry]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"twitter":{"id":"36","title":"Twitter","desc":"A tag to link to a user's twitter account","tag":"twitter","useoption":"0","example":"[twitter]userName[/twitter]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":"twitter.png"},"inline":{"id":"37","title":"Inline Code","desc":"Formats code inline instead of in a seperate code box. ","tag":"inline","useoption":"0","example":"[inline]style=\"font-size: 12px;\"[/inline]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":""},"il":{"id":"38","title":"Abbreviated Inline (IL)","desc":"Abbreviated version of the [inline] tag. ","tag":"il","useoption":"0","example":"[il]Code Here[/il]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"0","image":"il2.png"},"code":{"id":"41","title":"Code","desc":"Allows you to enter general code","tag":"code","useoption":"1","example":"[code]$text = 'Some long code here';[/code]","switch_option":"0","menu_option_text":"","menu_content_text":"","single_tag":"0","optional_option":"1","image":""}}) ); ipb.vars['emoticon_url'] = "http://cdn.dreamincode.net/forums/public/style_emoticons/default"; //Search Setup ipb.vars['search_type'] = 'forum'; ipb.vars['search_type_id'] = 32; ipb.vars['search_type_2'] = 'topic'; ipb.vars['search_type_id_2'] = 318531; //]]>

    5 Replies - 64 Views - Last Post: Yesterday, 07:40 PM Rate Topic: -----

    #1 dollymonroe ?Icon User is offline

    Reputation: 0

    • Posts: 4
    • Joined: Yesterday, 06:33 PM

    Posted Yesterday, 06:41 PM

    I am having two issues with my program. 1) I cannot figure out how to loop to the next determination when the user enters more than one number. 2) How to print if a number is perfect or not perfect without it going into an infinite loop or printing it the same number of times as there are spots in the array.
    
 public class csedersten_Perfect {     /*      *      *      *      */      public static void main ( String args[] )     {         int getN;         int getP = getPerfect();         int [] perfect = new int[100];         int x = 1;          boolean test = testPerfect(getP, perfect);          while (x <= perfect.length)         {             printFactors(getP, perfect, test);         }     }      /*      *      *      *      */      public static int getNum()     {         Scanner i = new Scanner( System.in ); int num;                            //         int perfectNum = 1;                 //         int total = 0;                      //          System.out.print( "How many numbers would you like to test? ");         num = i.nextInt();          boolean validateN = validateNum(num, perfectNum);          while (!validateN)         {             System.out.print( "How many numbers would you like to test? ");             num = i.nextInt();              validateN = validateNum(num, perfectNum);         }         return num;     }      /*      *      *      *      */      public static boolean validateNum( int num, int perfectNum )     {         if (( num <= 0 ) || ( perfectNum <= 0 ))         {             System.out.println( " Non-positive numbers are not allowed." );         }          else         {             return true;         }         return false;     }      /*      *      *      *      */      public static int getPerfect()     {         Scanner i = new Scanner ( System.in );          int perfectNum = -1; int num = getNum();          System.out.print( "Please enter a possible perfect number: " );         perfectNum = i.nextInt();          boolean validateN = validateNum(perfectNum, num);          while (!validateN)         {             System.out.print( "Please enter a possible perfect number: " );             perfectNum = i.nextInt();              validateN = validateNum(perfectNum, num);         }         return perfectNum;     }      /*      *      *      *      */      public static boolean testPerfect( int perfectNum, int[] perfect )     {         int max = perfectNum;         int a = 0;         int sum = 0;          for (int x = 1; x <= max; x++)         {             perfect[a++] = x;             perfectNum /= x;         }          perfect[a] = perfectNum;          for (int x = 0; x < perfect.length; x++)         {             sum += perfect[x];         }          if (sum == perfectNum)         {             int[] p = perfect;              return true;         }          else         {             return false; }     }      /*      *      *      *      */      public static void printFactors( int perfectNum, int[] perfect, boolean test)     {              if (test == true)             {                 System.out.printf( "%d:%10d", perfectNum, perfect);             }              else             {                 System.out.printf( "%d:NOT PERFECT", perfectNum );             }     } } 


    Is This A Good Question/Topic? 0

    Replies To: Determine perfect number, error with infinite loop

    #2 Flukeshot ?Icon User is offline

    Reputation: 114

    • Posts: 353
    • Joined: 14-November 12

    Re: Determine perfect number, error with infinite loop

    Posted Yesterday, 06:49 PM

    Your while loop is riding on the integer x being less than 100, it is set to 1. There is nothing in your code that will make x ever be more than 100, hence, you have an infinite loop.


    #3 dollymonroe ?Icon User is offline

    Reputation: 0

    • Posts: 4
    • Joined: Yesterday, 06:33 PM

    Re: Determine perfect number, error with infinite loop

    Posted Yesterday, 07:06 PM

    I understand what you're saying, but when I enter a number to determine if it is perfect or not perfect, the program doesn't stop printing the same thing over and over. I have to type ctrl c to escape it. I was wondering how to fix that.


    #4 Flukeshot ?Icon User is offline

    Reputation: 114

    • Posts: 353
    • Joined: 14-November 12

    Re: Determine perfect number, error with infinite loop

    Posted Yesterday, 07:19 PM

    You obviously didn't understand what I said, because addressing the issue I pointed out would correct your infinite loop that's printing everything out.
    
while (x <= perfect.length)

    The int 'x' is 1.
    The length of your array 'perfect' is [100]

    You never change x in your code. x is always <= perfect.length.

    I think you are trying to track how many inputs there are, an array length is not the way to do this since arrays have a set length at birth. What you will want to do is have a variable to record the number of inputs, and use that value in a for loop.

    
 //class variable static int numberOfInputs = 0;  //in your getPerfect() method    System.out.print( "Please enter a possible perfect number: " );   perfectNum = i.nextInt();   numberOfInputs++;  //your for loop for(int i = 0; i < numberOfInputs; i++) {   print }


    #5 pbl ?Icon User is offline

    Reputation: 7855

    • Posts: 30,702
    • Joined: 06-March 08

    Re: Determine perfect number, error with infinite loop

    Posted Yesterday, 07:28 PM

    This does not really make sense
    
         int max = perfectNum;         int a = 0;         int sum = 0;         for (int x = 1; x <= max; x++)         {             perfect[a++] = x;             perfectNum /= x;         } 

    so you store
    perfect[0] = 1
    perfect[1] = 2
    perfect[2] = 3
    .... up to
    perfect[max]

    at the same time assuming perfectNum == 10 which mean max == 10
    you will
    perfectNum /= 1
    perfectNum /= 2
    ...
    perfectNum /= 10

    sure that perfectNum will be == 0 at the end of the loop


    #6 dollymonroe ?Icon User is offline

    Reputation: 0

    • Posts: 4
    • Joined: Yesterday, 06:33 PM

    Re: Determine perfect number, error with infinite loop

    Posted Yesterday, 07:40 PM

    Flukeshot, I would use that solution if I could, but I am not allowed to use global variables for this assignment.


    Page 1 of 1


    Source: http://www.dreamincode.net/forums/topic/318531-determine-perfect-number-error-with-infinite-loop/

    humber raffi torres michael mcdonald jon jones vs rashad evans earth day 2012 jon jones rashad evans ufc jones vs evans

    No comments:

    Post a Comment

    Note: Only a member of this blog may post a comment.