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 2 of 2

Thread: Not sure how the answer is acchieved

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

    Default Not sure how the answer is acchieved

    I came across this question on a quiz for my course, I have spent over an hour staring at the code and cannot make heads or tails of it. I thought this would have populated the first index of the array with an value of '1' since the value of count at the first iteration of the while-loop would have been 0.

    Can someone explain to me how it populates the array with the values '18, 21, 24, 27, 30'.

     
    	public static void main(String[] args) {
    		int[] nums = new int[5];
    		int count = 0;
    		int val =1;
     
    		while (count<15) {
    			nums[count%5]+= val;
    			val++;
    			count++;
    		}
     
    		System.out.println(Arrays.toString(nums));
    	}

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Not sure how the answer is acchieved

    Have you tried playing computer with the code? Write after each statement what the values of the variables are after that statement is executed.
    Do that for all the statements until the loop ends.

    Another way to help see would be to add a print statement before the end of the loop that prints out the values of all the variables used in the loop.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] My answer comes out a +0.020 more than the answer in the book
    By UniverseCloud in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 18th, 2019, 08:05 PM
  2. Can any One answer it !!!!!!!!!!!!!!!!!!!!!
    By Srinivas_Java_Developer in forum Java Servlet
    Replies: 1
    Last Post: October 23rd, 2013, 12:42 PM
  3. Replies: 3
    Last Post: March 9th, 2013, 07:22 PM
  4. Can somebody help me to answer this? Thanks!
    By dshainad in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 13th, 2012, 09:47 PM
  5. need answer for these
    By satishbs in forum Java Theory & Questions
    Replies: 2
    Last Post: April 15th, 2011, 01:46 AM