按照题目要求编写的用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=0i<sArray.lengthi++){
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=0i<sArray.lengthi++){
System.out.println(sArray[i].toString())
}
}
}
1import java.util.Scanner
public class test1 {
private static Scanner input= new Scanner(System.in)
public static void main(String[] args){
long num
do{
System.out.print("\nInput your num: ")
num=input.nextLong()
}while(num<10000||num>99999)
num=num/100
num*=100
System.out.println("\nThis is num: "+num)
}
}
2.
import java.util.Scanner
public class test2 {
private static Scanner input= new Scanner(System.in)
public static void main(String[] args){
long num
do{
System.out.print("\nInput your num: ")
num=input.nextLong()
}while(num<0||num>1000)
int sum=0
while(num>0){
sum+=num%10
num/=10
}
System.out.println("\nThis is sum: "+sum)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)