Results 1 to 3 of 3
  1. #1
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

    Default Quick question about this simple code..

    Hello everyone. I was to create a code that finds the smallest number, assuming that the user first enters the amount of numbers to be input. I thought this would be a simple task but i ran into a little issue with it. Here is my code, the problem is that the program keeps saying that the last number input is the smallest...where did i go wrong?

    Java Code:
    
    import java.util.Scanner;
    public class SmallestValue {
    
    	public static void main(String[] args) {
    
    		int smallest=999999999;
    		int number;
    		int counternum;
    		int counter=1;
    	
    	Scanner input=new Scanner (System.in);
    	
    	System.out.println("Please the number of values to be input: ");
    	counternum=input.nextInt();
    	
    	System.out.println("Enter a number ");
    	number=input.nextInt();
    	
    		number=smallest;
    		counter++;
    	
    	while (counter <= counternum){		
    		
    			System.out.println("Enter another number: ");
    			number=input.nextInt();
    			
    				if (number<smallest)
    					smallest=number;
    
    			counter++;
    	}
    
    	System.out.println(counternum + " numbers entered.");
    	System.out.println("The smallest number entered was "+ number);
    	
    	}
    
    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,375
    Blog Entries
    7
    Rep Power
    20

    Default

    Why are you printing out 'number' while you should be printing 'smallest'? btw, there is something else wrong after you've read your first number (which is the smallest number read so far). Also, there is no need to initialize smallest to a very large number if you are reading at least one number.

    kind regards,

    Jos

  3. #3
    shroomiin is offline Member
    Join Date
    Sep 2009
    Posts
    31
    Rep Power
    0

Similar Threads

  1. quick question
    By vouslavous in forum Java Applets
    Replies: 4
    Last Post: 04-24-2009, 08:35 PM
  2. Hello everyone! quick question.
    By irishhokie in forum New To Java
    Replies: 5
    Last Post: 04-03-2009, 04:13 AM
  3. Quick Question
    By Graeme in forum New To Java
    Replies: 4
    Last Post: 01-08-2009, 08:01 PM
  4. Replies: 1
    Last Post: 10-02-2008, 03:06 AM
  5. Simple version of quick sort
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:40 PM

Posting Permissions

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