统计信息

  • 访问数:11208
  • 博客数:142
  • 建立时间:2008-01-05
  • 更新时间:2008-05-21
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

P6Q14

2008-04-15 14:10:48

天气: 晴朗 心情: 高兴

/*filename:P6Q14
 *Name:Wang Chaoran
Desciprtion:
14 (Computing average)
Write a method that returns the average of an unspecified number of numeric arguments.
*/
import java.util.Scanner;
class P6Q14{
 
  public static void main(String[] args){
   
    String input = "";
    System.out.println();
    System.out.println("The average is "+average(input));
  
   }
 
  static boolean isNumeric(String str){
   try {
   Double.parseDouble(str);
  return true;
  } catch (NumberFormatException nfe){
  return false;
}
}
 
  static double average(String input){
    Scanner scan = new Scanner(System.in);
    double sum =0;
      double count =0;
    while(!(input.equals("q"))){
      System.out.println("Please enter the "+(int)(count+1)+" number.(Enter q to end the input)");
      input = scan.next();
      if(isNumeric(input)){
       sum += Double.parseDouble(input);
      count++;
      }
      else if(input.equals("q"))
        System.out.println("The program will end!");
      else
        System.out.println("You have entered an invalid input. Please enter a number.");
}
    return sum/count;
}
 
}

加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd