Results 1 to 3 of 3

Thread: do while loop?

  1. #1
    shroomiin is offline Member
    Join Date
    Sep 2009
    Rep Power

    Default do while loop?

    hey everyone. I am supposed to make a program that finds the smallest number out of a set of numbers. the amount of numbers input is up to the user. i have to print the smallest number, and what input it was (ex, 1st number 2nd number 3rd number...)

    i have the code pretty much complete, however i cant figure out how to keep track of whether it was the first second or third number. Do i need to use a do while loop? can i do this with a normal while loop? i thought i could make a counter to keep track of it but i cant quit seem to get it...

    this is what i have

    Java Code:
    import java.util.Scanner;
    public class WhileLoops2 {
    	public static void main(String[] args) {
    		int smallest=0;
    		int num;
    		int counter=1;
    		int numCounter = 0;
    		int inputVals;
    	Scanner input=new Scanner (;
    	System.out.println("Enter number of input values: ");
    	inputVals = input.nextInt();
    	System.out.printf("\nEnter a number:");
    		while (counter<=inputVals){
    			System.out.println("Enter another number: ");
    				if (num<smallest)
    		System.out.println(smallest +" was the smallest number entered.");
    		System.out.println(smallest +" was input integer number "+numCounter+".");

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Rep Power


    Java Code:
    if (num<smallest) {
          numCounter = counter;
    Thankfully you don't have to pay a dollar everytime you use a {} pair.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Colombo, Sri Lanka
    Blog Entries
    Rep Power


    Quote Originally Posted by r035198x View Post
    Thankfully you don't have to pay a dollar everytime you use a {} pair.
    Put that adding {} pairs each time for single line block as well. Then no harm. :rolleyes:

Similar Threads

  1. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM
  2. Need help with While Loop
    By mrdestroy in forum New To Java
    Replies: 14
    Last Post: 10-20-2008, 03:29 PM
  3. How to use Do While loop
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 08:45 PM
  4. do...while loop
    By eva in forum New To Java
    Replies: 16
    Last Post: 01-31-2008, 07:44 AM
  5. can you help me with this for loop?
    By java_fun2007 in forum New To Java
    Replies: 6
    Last Post: 12-22-2007, 11:20 AM

Posting Permissions

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