Results 1 to 4 of 4

Thread: Problem with Finding maximum value (negative, 0, positive)

  1. #1
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with Finding maximum value (negative, 0, positive)

    This program will read n double values.
    value reads the values, and valMax stores the largest value.
    The values read in could be positive or 0 or negative.



    Code:
    import java.util.Scanner;
    public class ExampleProblem {
    	public static void main(String[] args)
    	{   
    		double value, valMax = 0.0;
    		int n;
    		Scanner scan = new Scanner(System.in);
    		n = scan.nextInt();
    		for(int j = 0; j < n; j++)
    		{
    			value = scan.nextDouble();
    			
                            //valMax = value;
    
    			if(valMax < value)
    			{
    				valMax = value;
    			}
    		
    			
    		}
    		System.out.println("Largest Number is: " + valMax);
    	}
    }
    I have trouble with initialize the valMax.
    When it comes to negative number, the initialized value 0.0 is always the largest... but when I set valMax = value, the result is wrong...
    Last edited by laduch; 04-10-2008 at 04:46 AM.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    We don't do homework problems for you. If you'd like to ask a specific question, we can try to help. After all, this is practice for your class and that's how you learn.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Apr 2008
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I have tried to solve it. Not like I am asking you to solve it.
    I am not asking for solution, am I? I was just confused about initialization.
    But if you think inappropriate for any such question, just leave it.

  4. #4
    Join Date
    Mar 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    im having exactly the same problem as you man with the all negative numbers

Bookmarks

Posting Permissions

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