集册 Java实例教程 使用计数器计算平均值

使用计数器计算平均值

欢马劈雪     最近更新时间:2020-01-02 10:19:05

725
使用反控制重复计算平均值

import java.util.Scanner; 


public class Main //来自 时代Java

{

   public static void main(String[] args) 

   {

      // create Scanner to obtain input from command window

      Scanner input = new Scanner(System.in);


      // initialization phase

      int total = 0; // initialize sum of grades entered by the user

      int gradeCounter = 1; // initialize # of grade to be entered next

   

      // processing phase uses counter-controlled repetition

      while (gradeCounter <= 10) // loop 10 times

      {

         System.out.print("Enter grade: "); // prompt 

         int grade = input.nextInt(); // input next grade

         total = total + grade; // add grade to total

         gradeCounter = gradeCounter + 1; // increment counter by 1

      } // 来 自 N o  w  J a v a . c o m - 时  代  Java

   

      // termination phase

      int average = total / 10; 
展开阅读全文