日期:2014-05-20 浏览次数:20882 次
package ch5; import java.text.NumberFormat; public class Student{ private String name; double [] scores; [b]public Student(String aName,double ... aScores)[/b]{ name=aName; scores=aScores; } public String toString(){ String output=name; NumberFormat formatter=NumberFormat.getNumberInstance();//数值标准格式化器 formatter.setMaximumFractionDigits(1); double sum=0; for(double score: scores) sum+=score; double average=sum/scores.length; return output+": "+formatter.format(average); } }
package ch5; import javax.swing.JOptionPane; public class VariableSizeArrayDeclarationApp{ public static void main (String[] args) { Student student1=new Student("王菲",70,78,85); Student student2=new Student("张浩",90,88,95,70); Student student3=new Student("张飞",60,78,88,90,95); String output=student1.toString()+"\n"; output+=student2.toString()+"\n"; output+=student3.toString(); JOptionPane.showMessageDialog(null,output,"结果",JOptionPane.PLAIN_MESSAGE); } }