按照题目要求编写的用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())
}
}
}
按照你的要求编写的Java程序如下
import java.util.Scanner
public class S{
public static void main(String[] args){
int n=5
int[] a=new int[n]
System.out.print("请输入"+n+"个正整数:")
Scanner sc=new Scanner(System.in)
for(int i=0i<ni++){
a[i]=sc.nextInt()
}
System.out.print("其中的偶数为:")
for(int i=0i<ni++){
if(a[i]%2==0){
System.out.print(a[i]+" ")
}
}
}
}
public class Demo3 {public static void main(String[] args) {
byte a =1
short b=1
int c = 1
long d = 1l
float e=1f
double f =1
char g ='1'
boolean h = true
System.out.println("byte a ="+a)
System.out.println("short b="+b)
System.out.println("int c ="+c)
System.out.println("long d ="+d)
System.out.println("float e="+e)
System.out.println("double f ="+f)
System.out.println("char g ="+g)
System.out.println("boolean h ="+h)
int x=a%b
long y = c+d
boolean z=(b==c)
System.out.println("int x="+x)
System.out.println("long y"+y)
System.out.println("boolean z"+z)
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)