views:

54

answers:

2

I have to write a program that allows the user to input 10 single digit numbers, and then it reads the largest number out of the bunch. I need help just starting out with the counter. This is what I need,

a) counter: A counter to count to 10 (that is, to keep track of how many numbers have been input and to determine when all 10 numbers have been processed);

Problem is, I have no where to start, the book I am using doesn't do a good job explaining counters, and I am not looking for someone to give me an answer, just some guidance with a bit of code to start out with.

Any help will be greatly appreciated.

+1  A: 

You probably just need a for loop.

for (int counter = 0;       //a variable to keep track of how many numbers have been read
   counter < 10;            //we want to read only up to 10 numbers
   counter = counter + 1) { //after every loop, we increment the number of numbers by one

   //read in input from the user

   //do stuff with the input

} //end the for loop.  this will jump to the top of the loop if the condition passes
Mike
A: 

Just keep an int local variable

Scanner sc = new Scanner(System.in);
int counter = 0;//this is the counter
int input = 0;
while(sc.hasNext()) {
  input = sc.nextInt();
  counter++;//this increases the counter by 1
  //Do stuff
}
Adam