刚学JAVA一个星期,谁能帮我修改一下我的code.目的是要输入任意个数的double,然后求和,平均,最大,number个数。
double...args到底怎么用
import java.util.Scanner;
/**
 * Class that provides 4 simple methods to compute some properties
 * @Any number of double values
 */
public class Question3{
/**
 * compute the sum of the values
 * @param args
 * return sum of values
 */
public double getSum(double...args){
double sum = 0;
for(double n : args){
sum +=  n;
}
return sum;

}
/**
 * compute the count of how many inputs there are
 * return count
 * @param args
 */
public double count(double...args){
double sum = 0;
int count = 0;
for(double n : args){
sum += n;
count++;
}
return count;
}
/**
 * compute the maximum of the values
 * @param args
 * return maximum of the values
 */
public double getMax(double... args){
double max = Double.POSITIVE_INFINITY;
for(double d : args){
if(d >= max){
max = d;
}
}
return max;
}

/**
 * compute the average of the values
 * @param args
 * return average of values
 */
public double getAve(double...args){
double theSum = getSum(args);
double theCount = count(args);
return theSum / theCount;

}
/**
 * The main method reads in inputs and shows the values of 
 * the four methods above. 
 * @param args
 */
public static void main(double...args){
Question3 test = new Question3();
Scanner in  = new Scanner(System.in);
                (这部分实在不知道怎么写 =  =|||)
System.out.print("Enter a int: ");
        System.out.println("Maximum = " + test.getMax(args));
        System.out.println("Sum = " + test.getSum(args));
System.out.println("There are " + test.count(args) +
" " + "inputs");
System.out.println("Average = " + test.getAve(args));
   
}
}

解决方案 »

  1.   

    你的逻辑有问题哎System.out.print("Enter a int: "); Scanner in  = new Scanner(System.in); 应该写在main方法里面把。还有你为什么要参数double的以数组传进去啊
      

  2.   

    不要用增强FOR循环,忘掉它吧。
      

  3.   

    我也知道逻辑有问题,main方法里面不知道怎么写呀。参数double的以数组传进去这个被老师要求的,如果不用数组的话要怎么改,请指教。
      

  4.   

    不管了,把原来的code无视掉吧。
    求好心人给个输入任意个数的double,然后输出和的最简单的例子> <。
      

  5.   

    /** 
    * Class that provides 4 simple methods to compute some properties 
    * @Any number of double values 
    */ 
    public class Question3{ 
    /** 
    * compute the sum of the values 
    * @param args 
    * return sum of values 
    */ 
    public double getSum(double...args){ 
    double sum = 0; 
    for(double n : args){ 
    sum +=  n; 

    return sum;  } 
    /** 
    * compute the count of how many inputs there are 
    * return count 
    * @param args 
    */ 
    public double count(double...args){ 
    double sum = 0; 
    int count = 0; 
    for(double n : args){ 
    sum += n; 
    count++; 

    return count; 

    /** 
    * compute the maximum of the values 
    * @param args 
    * return maximum of the values 
    */ 
    public double getMax(double... args){ 
    double max = Double.MIN_VALUE; //Double.POSITIVE_INFINITY;
    for(double d : args){ 
    if(d >= max){ 
    max = d; 


    return max; 
    } /** 
    * compute the average of the values 
    * @param args 
    * return average of values 
    */ 
    public double getAve(double...args){ 
    double theSum = getSum(args); 
    double theCount = count(args); 
    return theSum / theCount;  } 
    /** 
    * The main method reads in inputs and shows the values of 
    * the four methods above. 
    * @param args 
    */ 
    public static void main(String[] args){ 
    Question3 test = new Question3();
    double[] dargs = new double[args.length];
    for(int i=0;i<args.length;i++){
    dargs[i] = Double.valueOf(args[i]);
    }        System.out.println("Maximum = " + test.getMax(dargs)); 
            System.out.println("Sum = " + test.getSum(dargs)); 
            System.out.println("There are " + test.count(dargs) + 
             " " + "inputs"); 
            System.out.println("Average = " + test.getAve(dargs)); 
      

    }
      

  6.   

    楼上可以,如果不是命令行输入的话:import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Scanner;
    import java.util.regex.MatchResult;/**
     * Class that provides 4 simple methods to compute some properties
     * 
     * @Any number of double values
     */
    public class Question3 {
    /**
     * compute the sum of the values
     * 
     * @param args
     *            return sum of values
     */
    public double getSum(double... args) {
    double sum = 0;
    for (double n : args) {
    sum += n;
    }
    return sum; } /**
     * compute the count of how many inputs there are return count
     * 
     * @param args
     */
    public double count(double... args) {
    double sum = 0;
    int count = 0;
    for (double n : args) {
    sum += n;
    count++;
    }
    return count;
    } /**
     * compute the maximum of the values
     * 
     * @param args
     *            return maximum of the values
     */
    public double getMax(double... args) {
    double max = Double.POSITIVE_INFINITY;
    for (double d : args) {
    if (d >= max) {
    max = d;
    }
    }
    return max;
    } /**
     * compute the average of the values
     * 
     * @param args
     *            return average of values
     */
    public double getAve(double... args) {
    double theSum = getSum(args);
    double theCount = count(args);
    return theSum / theCount; } /**
     * The main method reads in inputs and shows the values of the four methods
     * above.
     * 
     * @param args
     */
    public static void main(double... args) {
    Question3 test = new Question3(); // (这部分实在不知道怎么写 = =|||) System.out.println("Maximum = " + test.getMax(args));
    System.out.println("Sum = " + test.getSum(args));
    System.out.println("There are " + test.count(args) + " " + "inputs");
    System.out.println("Average = " + test.getAve(args)); } public static void main(String[] args){
    System.out.print("Enter a int: ");
    //Scanner in = new Scanner(System.in).skip("\\,");
    //in.findInLine("(\\d)");

    BufferedReader in= new BufferedReader(new InputStreamReader(System.in));

    try {
    String[] s=in.readLine().split(",");
    double[] argDouble=new double[s.length];

    for(int i=0;i<s.length;i++){
    argDouble[i]=Double.parseDouble(s[i]);
    }

    Question3.main(argDouble);

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    //
    // String input = "1 fish 2 fish red fish blue fish";
    //      Scanner s = new Scanner(input);
    //      s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
    //      MatchResult result = s.match();
    //      for (int i=1; i<=result.groupCount(); i++){ 
    //       System.out.println(result.group(i));
    //      }
    //      s.close();
    }
    }
      

  7.   

    double...args这是可变参数,就是输入的参数可以是0个或者多个.
      

  8.   

    感谢大家的帮助问题已经解决了。        public static void main(String[] args){ 
            Question3 n = new Question3();
            String str;
            Scanner in = new Scanner(System.in);
            System.out.println("Enter any numbers: ");
            str = in.nextLine();
            String[] num = str.split(",");
            double[] dargs = new double[num.length];
            for(int i=0;i<num.length;i++){
             dargs[i] = Double.parseDouble(num[i]);
            }
            System.out.println("Maximum = " + n.getMax(dargs)); 
            System.out.println("Sum = " + n.getSum(dargs)); 
            System.out.println("There are " + n.count(dargs) + 
             " " + "inputs"); 
            System.out.println("Average = " + n.getAve(dargs)); 
            }
          }
    把main改成这样了。
    不过如果输入为空白字符的话要怎么改
    if(str == null){
    System.out.println(input was empty);}
    else{
    for(int i=0;i<num.length;i++){
        dargs[i] = Double.parseDouble(num[i]);
        System.out.println("Maximum = " + n.getMax(dargs)); 
        System.out.println("Sum = " + n.getSum(dargs)); 
        System.out.println("There are " + n.count(dargs) + 
             " " + "inputs"); 
        System.out.println("Average = " + n.getAve(dargs)); 
    }
    感觉Java的语法比Python复杂好多= =,继续努力ing。