Java反击问题

时间:2010-07-14 22:47:03

标签: java counter

我必须编写一个程序,允许用户输入10个单位数字,然后它会读出最大数字。我刚从柜台开始需要帮助。这就是我需要的,

a)计数器:计数到10的计数器(即跟踪已输入的数量并确定何时处理了所有10个数字);

问题是,我没有从哪里开始,我正在使用的书并没有很好地解释计数器,我不是在找人给我一个答案,只是一些指导与一些代码从一开始。

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

您可能只需要一个for循环。

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

答案 1 :(得分:0)

只保留一个int局部变量

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
}