public static void main(String[] args) {
Scanner scn=new Scanner(System.in);
String s=scn.nextLine();
String[] paras = s.split(":|,");
int len = Integer.parseInt(paras[0]);
int sum = 0;
for (int i = 1; i <= len; i++) {
sum+=Integer.parseInt(paras[i]);
}
double av=sum*1.0/len;
System.out.println(av);
}
你不能用char直接算啊,char'0'是48呢
import java.util.Scanner;
class Parent{
private Scanner item=new Scanner(System.in);
private StringBuffer str=new StringBuffer();
private int count;
private int sum;
private int y=1;
private float avg;
public void out(){
System.out.println("按要求输入数据");
str.append(item.next());
count=Integer.parseInt(str.substring(0,1));
str.delete(0,2);
for(int x=0;x<=str.length();x+=2){
sum+=Integer.parseInt(str.substring(x,y));
y+=2;
}
avg=sum/count;
System.out.println(avg);
}
}
class Sub{
public Sub(Parent s){
s.out();
}
public static void main(String[] args){
Sub p=new Sub(new Parent());
}
}