新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
使用DecimalFormat格式化
为企业提供成都网站设计、成都做网站、网站优化、网络营销推广、竞价托管、品牌运营等营销获客服务。成都创新互联拥有网络营销运营团队,以丰富的互联网营销经验助力企业精准获客,真正落地解决中小企业营销获客难题,做到“让获客更简单”。自创立至今,成功用技术实力解决了企业“网站建设、网络品牌塑造、网络营销”三大难题,同时降低了营销成本,提高了有效客户转化率,获得了众多企业客户的高度认可!
Scanner input = new Scanner(System.in);
int[] scores = new int[4];
int sum = 0;
for (int i = 0; i 4; i++) {
System.out.println("请输入第" + (i + 1) + "位同学的成绩");
scores[i] = input.nextInt();
sum = sum + scores[i];
}
double ave = sum / 4.0;
DecimalFormat format = new DecimalFormat("0.00");
System.out.println("平均成绩是:" + format.format(ave));
按照题目要求编写的用javaBean规范设计的学生类Student的Java程序如下
需要创建user.java.test包,把Student.java文件和Test.java文件放入包中,编译Student.java文件并且编译运行Test.java文件得到运行结果
Student.java文件代码如下
package user.java.test;
import java.io.Serializable;
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
private String no;
private String name;
private double score;
public Student(){}
public Student(String no,String name,double score){
this.no=no;
this.name=name;
this.score=score;
}
public String getNo(){ return no;}
public void setNo(String no){ this.no=no;}
public String getName(){ return name;}
public void setName(String name){ this.name=name;}
public double getScore(){ return score;}
public void setScore(double score){ this.score=score;}
public String toString(){
return "学号:"+no+",姓名:"+name+",成绩:"+score;
}
public static double getAvg(Student[] sArray){
double sum=0,avg;
for(int i=0;isArray.length;i++){
sum=sum+sArray[i].getScore();
}
avg=sum/sArray.length;
return avg;
}
}
Test.java文件代码如下
package user.java.test;
public class Test{
public static void main(String[] args){
Student[] sArray=new Student[5];
sArray[0]=new Student("001","张三",89.5);
sArray[1]=new Student("002","李四",82.5);
sArray[2]=new Student("003","王五",93);
sArray[3]=new Student("004","赵六",73.5);
sArray[4]=new Student("005","孙七",66);
System.out.println("这些学生的平均分:"+Student.getAvg(sArray));
for(int i=0;isArray.length;i++){
System.out.println(sArray[i].toString());
}
}
}
import java.util.*;
public class xiaoming{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
System.out.println("请输入小明这一学期的语文成绩:");
int chinese=in.nextInt();
System.out.println("请输入小明这一学期的数学成绩:");
int math=in.nextInt();
System.out.println("请输入小明这一学期的英语成绩:");
int english=in.nextInt();
int sum=0;
sum=chinese+math+english;
int avg=sum/3;
System.out.println("小明的语文成绩是"+chinese+"分");
System.out.println("小明的数学成绩是"+math+"分");
System.out.println("小明的英语成绩是"+english+"分");
System.out.println("小明的平均成绩是"+avg+"分");
int mark=avg;
if((90markmark100)==true){
System.out.println("小明的成绩等级是A");
}else if((80markmark90)==true){
System.out.println("小明的成绩等级是B");
}else if((70markmark80)==true){
System.out.println("小明的成绩等级是C");
}else if((60markmark70)==true){
System.out.println("小明的成绩等级是D");
}else{
System.out.println("小明的成绩等级是E");
}
}
}
public static void main(String[] args) {
double scores[] = new double[5];
double total = 0;
double avg = 0;
double max = 0;
double min = 0;
int count=0;
String inputStr=null;
System.out.println("请输入5名学生的成绩:");
Scanner input = new Scanner(System.in);
while(count5){
try{
if(count 5){
System.out.println("请输入第"+(count+1)+"个分数:");
}
inputStr=input.nextLine();
scores[count++]=Double.valueOf(inputStr.trim());
}catch(Exception e){
if(inputStr!=null "exit".equals(inputStr.trim())){
System.out.println("您已成功结束程序");
System.exit(0);
}
System.out.println("若想结束请输入:exit");
System.out.print("您输入的分数不是数值类型,");
count--;
}
}
input.close();
Arrays.sort(scores);
min=scores[0];
max=scores[scores.length-1];
for(double score :scores){
total += score;
}
avg=total/scores.length;
System.out.println("总成绩是" + total);
System.out.println("最高分是" + max);
System.out.println("最低分是" + min);
System.out.println("平均分是" + avg);
}
//-------------------------------------------------------------------------
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while(true){
Double[] scores = null;
double total = 0;
double avg = 0;
double max = 0;
double min = 0;
int count=1;
ListDouble inputScores=new ArrayListDouble();
String inputStr=null;
System.out.println("请输入要统计学生的成绩(理论上可以输入无限个,前提是你有那么大的内存):");
while(true){
try{
System.out.println("请输入第"+count+++"个分数,或输入ok进行计算,离开请输入exit");
inputStr=input.nextLine();
inputScores.add((double)Double.valueOf(inputStr.trim()));
}catch(Exception e){
if(inputStr!=null "exit".equals(inputStr.trim().toLowerCase())){
System.out.println("您已成功结束程序");
input.close();
System.exit(0);
}
if(inputStr!=null "ok".equals(inputStr.trim().toLowerCase())){
break;
}
System.out.println("您输入的分数不是数值类型,");
System.out.println("若想结束请输入exit ,若想计算结果请输入ok");
count--;
}
}
if(inputScores.size()==0){
System.out.println("您没有输入学生成绩,无数据可统计,程序结束。");
return ;
}
scores=inputScores.toArray(new Double[inputScores.size()]);
Arrays.sort(scores);
min=scores[0];
max=scores[scores.length-1];
for(double score :scores){
total += score;
}
avg=total/scores.length;
System.out.println("总成绩是" + total);
System.out.println("最高分是" + max);
System.out.println("最低分是" + min);
System.out.println("平均分是" + avg);
}
}
//
import java.util.Scanner;
//
public class Test2014 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入学生人数:");
int n = sc.nextInt();
int sum = 0;
for(int i = 1;i = n;++i){
System.out.println("输入第"+i+"个学生成绩:");
sum += sc.nextInt();
}
System.out.println("总成绩是:"+sum+" "+"平均成绩是:"+(double)sum/n);
}
}
int[] a={22,33,44,33,44};
int sum=0;
int ave=0;
for(int i=0;ia.length;i++)
{
sum+=a[i];
}
ave=sum/a.length;
System.out.println("平均成绩为:"+ave);