Results 1 to 4 of 4
  1. #1
    dexter is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default How do you make program repeat question?

    Im alittle burned out on this code it works just fine but im trying to make it ask me again to enter number after it gives me result of previous number... This is my code can someone tell me what im doing wrong... Thx





    import java.util.Scanner;
    public class Fib {

    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);




    System.out.print("Enter Fib number: ");
    int n = sc.nextInt();

    int[] results = new int[n];
    printNTerms( results, n );




    }

    public static void printNTerms( int[] results, int n ) {


    results[0] = 0;
    results[1] = 1;
    results[2] = 1;

    int newTerm = 0;



    if ( n == 1 )
    System.out.println(results[1]);

    else if ( n == 2 )
    System.out.println( results[1] + " " + results[2]);
    else {


    System.out.print("f(" + n + ") = " + results[1] + " , " + results[2] + " , ");
    for ( int index = 3; index <= n; index++) {

    newTerm = results[1] + results[2];
    System.out.print( newTerm + " , ");

    results[1] = results[2];
    results[2] = newTerm;


    }


    }


    }
    }

  2. #2
    codesmuggler is offline Member
    Join Date
    Feb 2011
    Posts
    30
    Rep Power
    0

    Default

    Put your code into "code" tags, I don't see anything.
    Repeat question? Just do it in loop.

  3. #3
    dexter is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    After i input a number i want it to give me result then ask me again to enter number

  4. #4
    dexter is offline Member
    Join Date
    Feb 2011
    Posts
    6
    Rep Power
    0

    Default

    I tried adding a do while loop in order for it to ask to enter number again.... But cant seem to make it work.... Any suggestions would be greatly appreciated...thx






    import java.util.Scanner;
    public class Fib {
    public static void main(String[] args) {

    do{

    Scanner sc = new Scanner(System.in);


    System.out.print("How many fib terms would you like to display? ");
    int n = sc.nextInt();
    int[] results = new int[n];
    printNTerms( results, n );

    }
    while ((0 < n) && (n < 100));
    }
    public static void printNTerms( int[] results, int n ) {


    results[0] = 0;
    results[1] = 1;
    results[2] = 1;
    int newTerm = 0;

    if ( n == 1 )
    System.out.println(results[1]);
    else if ( n == 2 )
    System.out.println( results[1] + " " + results[2]);
    else {

    System.out.print("f(" + n + ") = " + results[1] + " , " + results[2] + " , ");
    for ( int index = 3; index <= n; index++) {
    newTerm = results[1] + results[2];
    System.out.print( newTerm + " , ");
    results[1] = results[2];
    results[2] = newTerm;
    }

    }
    }
    }

Similar Threads

  1. can't make ui:repeat work
    By kaghamdi in forum JavaServer Faces (JSF)
    Replies: 6
    Last Post: 12-31-2010, 12:43 PM
  2. Replies: 3
    Last Post: 11-26-2010, 04:08 PM
  3. can't make ui:repeat work
    By kaghamdi in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 09-26-2010, 08:19 PM
  4. Some question to make Image Viewer
    By BluXit in forum New To Java
    Replies: 5
    Last Post: 04-14-2010, 05:29 AM
  5. Replies: 24
    Last Post: 07-29-2008, 05:04 PM

Posting Permissions

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