刚学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));
}
}
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));
}
}
解决方案 »
- IO操作的一个问题
- 问个小小的问题,记事本写
- 没多少分!但是还是希望众人拾柴火焰高吧!想统计下大家知道的例外、异常,能帮助新手尽快的处理问题!我先发几个
- arrayList问题
- java,.net,网络(ccie),信息安全;这些怎么统一起来呀
- jsp导出excel表问题,保存 已解决,打开 没有得到预期效果
- SQLException: Invalid handle 是怎么回事?
- 替换尾部html标签部分,这个正则表达式该怎么写?
- 请问Timer类 方法scheduleAtFixedRate,是作什么用的
- Exception in Thread "main" java.lang.NoClassDefFoundError: Welcome 什么原因(高分)
- 怎么用java读取文本?
- 线程程序的问题
求好心人给个输入任意个数的double,然后输出和的最简单的例子> <。
* 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));
}
}
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();
}
}
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。