Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Question about my code

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Question about my code

    The code box below includes a live int array variable called nums, whose declaration and initialization is hidden. The elements in this array are distinct. What is the largest element in the array? Write appropriate code, involving a loop, that will print this value to the console.

    The answer you enter should execute exactly one System.out.println statement.


    And below is my code:

    int largest = nums[0];
    for (int j = 0; j < nums.length; j++){
    largest = nums[j];
    System.out.println(largest);
    }

    it doesn't come out with the largest element. I don't know why. Can anyone help?

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    79
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Question about my code

    Hi,

    That loop prints all the elements instead of just one. Can you see why?

    And you're assiging nums[j] to 'largest' without checking first to see whether nums[j] is actually larger. So 'largest' just ends up with the last element, not the largest. Can you see why?

  3. #3
    Junior Member
    Join Date
    Oct 2017
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about my code

    Oh, I think I mean to switch the largest = nums[j], so it should be nums[j] = largest

  4. #4
    Member
    Join Date
    Apr 2014
    Posts
    79
    Thanks
    2
    Thanked 6 Times in 6 Posts

    Default Re: Question about my code

    Quote Originally Posted by justforeva View Post
    Oh, I think I mean to switch the largest = nums[j], so it should be nums[j] = largest
    Are you sure about that? The 'nums' array is your input to be analyzed; its elements should not be changed.

    You need to check if nums[j] is is greater than 'largest' before storing its value into 'largest'.

  5. #5
    Junior Member
    Join Date
    Oct 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about my code

    Your code runs through every element of the array and prints its value. Going through each element of the array is fine, but as you go through each element you should see if it is larger than the one you have. If that's the case, you have to stay with the largest.

    Once you go through all the elements of the array, you can print out the largest.

    Tip: You'll need to use an IF sentence somewhere.

Similar Threads

  1. I need a java code for this question. can any one help me..?
    By Vinod Kumar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2014, 09:50 AM
  2. Code for a question???
    By bickey in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 19th, 2013, 05:36 PM
  3. Boolean code question!
    By Scorks in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 1st, 2013, 12:41 PM
  4. code set up question
    By op117 in forum Java Theory & Questions
    Replies: 19
    Last Post: September 10th, 2012, 07:54 PM
  5. This is my code and i would like to ask a question..
    By savvas in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 24th, 2011, 01:01 PM