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: I don't quite understand the last bit of this code. Could someone comment it out for me?

  1. #1
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    My Mood
    Angelic
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I don't quite understand the last bit of this code. Could someone comment it out for me?

    This code will let a user type 10 double's, and return the smallest of those 10 double's. But I don't really understand what happens in de last for-loop. So could someone explain it to me, or comment the code. Thanks

    package hsleiden.webcat.exercise06_09;
    import java.util.Scanner;
    public class Exercise06_09
    {
    	public static void main (String[] args){
     
    		double[] allNumbers = new double [10];
     
    		Scanner input = new Scanner(System.in);
    		System.out.print("Enter ten double numbers: ");
     
            for (int i = 0; i < allNumbers.length; i++){
    		allNumbers[i] = input.nextDouble();
            }
            System.out.println("The min is: " + min(allNumbers));
    	}
     
    	public static double min (double[] list){
    		double min = list[0];
     
    		 for (int i = 1; i < list.length; i++){
    			 if (min > list[i]) 
    			        min = list[i]; 
    		 	}
    		return min;
    		}
    	}
    Last edited by JohnRoest; October 1st, 2014 at 10:41 AM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I don't quite understand the last bit of this code. Could someone comment it out for me?

    What do you think happens?

    What happened when you stepped through this with a debugger? Or at least added some print statements? Or just walked through it with a piece of paper and a pencil?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    My Mood
    Angelic
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I don't quite understand the last bit of this code. Could someone comment it out for me?

    It searches the smallest number, and returns that. But what it exactly does I do not understand. Like: what the steps are that the code does. That is what is bothering me.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: I don't quite understand the last bit of this code. Could someone comment it out for me?

    Take it one line at a time. Get out a piece of paper and a pencil. What is the value of every variable as the program executes? Keep track of it on the piece of paper, and update it as you step through using some fictional input. What happens if you step through it with different input?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Oct 2014
    Posts
    3
    My Mood
    Angelic
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I don't quite understand the last bit of this code. Could someone comment it out for me?

    Okay, I'll try that .

Similar Threads

  1. help please i don't understand why the count is not adding .
    By gagakzs in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 5th, 2014, 05:57 AM
  2. I don't understand this. Please help...
    By javaman1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 20th, 2014, 04:32 AM
  3. [SOLVED] Gotten easy code by proffessor that I don't understand, do you?
    By matitorn in forum Java Theory & Questions
    Replies: 7
    Last Post: October 9th, 2012, 01:30 PM
  4. Simply don't understand minimax...
    By Herah in forum Algorithms & Recursion
    Replies: 3
    Last Post: October 13th, 2011, 12:45 PM
  5. I don't understand what I'm supposed to do
    By dmcettrick in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 11th, 2011, 09:34 AM

Tags for this Thread