Results 1 to 5 of 5
Like Tree1Likes
  • 1 Post By Norm

Thread: Help optimizing simple program.

  1. #1
    whileloop is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Help optimizing simple program.

    I'm writing a program that would read ten numbers from the user and print the largest number.
    How would I optimize line 13? Should I do something else?
    Anything else?
    Java Code:
        public static void topTen() {
            Scanner input = new Scanner(System.in);
            double largest = 0;
            double userInput;
            double counter;
            double large;
            for (counter = 1; counter <= 10; counter++) {
                System.out.print("Enter Number: ");
                userInput = input.nextDouble();
                if (largest < userInput) { //Changes largest value to the user input.
                    largest = userInput;
                }
                    System.out.println("The largest number is " + largest);
            }
        }
    Thanks!
    Last edited by whileloop; 04-18-2012 at 01:52 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Help optimizing simple program.

    What is the purpose of the else block? Why have 2 assignment statements?
    Fubarable likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    whileloop is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help optimizing simple program.

    I just noticed the else statement is not needed. Can't believe I couldn't figure that out before. What do you mean by assignment statements?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default Re: Help optimizing simple program.

    The else block had this:
    a=b;
    b=a; // the above made the two equal already.
    Now the code prints out every time around the loop. Is that what you want?
    Why is counter a double?
    What is large used for?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    whileloop is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Help optimizing simple program.

    Quote Originally Posted by Norm View Post
    The else block had this:
    a=b;
    b=a; // the above made the two equal already.
    Now the code prints out every time around the loop. Is that what you want?
    Why is counter a double?
    What is large used for?
    Thanks I get it now. The large was never used a typo. I'll make the counter int.

Similar Threads

  1. Optimizing paiontComponent()
    By EricB in forum Java 2D
    Replies: 1
    Last Post: 11-05-2011, 08:29 PM
  2. Still need some optimizing help for my game.
    By Alerhau in forum New To Java
    Replies: 5
    Last Post: 09-25-2011, 09:18 AM
  3. Simple program, simple problem
    By taymilll in forum New To Java
    Replies: 12
    Last Post: 06-20-2011, 06:12 AM
  4. Quick Optimizing question
    By sgthale in forum New To Java
    Replies: 6
    Last Post: 04-15-2011, 02:25 AM
  5. Please help with simple program.. Very simple.
    By jonytek in forum New To Java
    Replies: 7
    Last Post: 02-14-2011, 01:44 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
  •