1、良心出品java编程题1. 定义一个包括10个元素一维整型数组,通过从键盘输入的10个整数对数组进行初始化,将数组中的元素按小到大排序后在屏幕上显示,求出该数组中元素的最小值、最大值以及平均值并在屏幕上显示输出。解答:import java.util.Scanner;import java.util.Arrays;public class Testpublic static void main(String args) int x = new int10; double sum =0; Scanner scr = new Scanner(System.in); for(int i=0;ix.l
2、ength;i+) xi= scr.nextInt(); Arrays.sort(x); for(int i=0;ix.length;i+) sum += xi; System.out.println(Max=+xx.length-1); System.out.println(Min=+x0); System.out.println(Average=+sum/x.length); 2.编写一个学生类Student:属性包括:学号(id)、姓名(name)、英语成绩(eng)、数学成绩(math)、计算机成绩(comp)和总成绩(sum)方法包括:构造方法、各属性的set方法、各属性的get方法
3、、toString方法(输出学生的全部信息)、sum方法(计算总成绩)。解答:public class Student implements Serializable /属性定义 public Student(String id,String name,int eng,int math,int comp) this.id=id; this.name=name; this.eng=eng; this.math=math; p=comp; sum(); /计算总成绩 public Student(Student s) this.id=s.id; this.name=new String(s.nam
4、e); this.eng=s.eng; this.math=s.math; p=p; sum(); /计算总成绩 public void setId(String id) this.id=id; public void setName(String name) this.name=name; public void setEng(int eng) this.eng=eng; sum(); public void setMath(int math) this.math=math; sum(); public void setComp(int comp) p=comp; sum(); public
5、 String getId() return id; public String getName() return name; public int getEng() return eng; public int getMath() return math; public int getComp() return comp; public int getSum() return sum; void sum() this.sum=eng+math+comp; public String toString() return getId() + t+getName() + t+getEng() +
6、t+getMath() +t+getComp() + t+getSum(); 3. 设计一个一元二次方程类,并为这个类添加异常处理。解答:public class Operation public static void main(String args) String s=; double a,b,c,r1,r2; System.out.print(求二元一次方程的根) ; System.out.print(请键入第一个系数a) ; try BufferedReader in =new BufferedReader( new InputStreamReader(System.in); s=i
7、n.readLine(); catch(IOException e) a=Double.parseDouble(s); System.out.print(请键入第二个系数b) ; try BufferedReader in =new BufferedReader( new InputStreamReader(System.in); s=in.readLine(); catch(IOException e) b=Double.parseDouble(s); System.out.print(请键入第三个系数c) ; try BufferedReader in =new BufferedReade
8、r( new InputStreamReader(System.in); s=in.readLine(); catch(IOException e) c=Double.parseDouble(s); r1=(-b+Math.sqrt(b*b-4*a*c)/(2*a); r2=(-b-Math.sqrt(b*b-4*a*c)/(2*a); System.out.print(该二元一次方程的根为:+r1+和+r2) ; 4.编写一个应用程序创建两个线程,一个线程打印输出11000之间所有3的倍数,另外一个线程打印输出10002000之间所有5的倍数。解答:class Thread1 extends
9、 Thread public Thread1(String msg) super(msg); public void run() int sum=0; for(int i=1;i=1000;i+) if(i % 3 =0) System.out.println(i); class Thread2 extends Thread public Thread2(String msg) super(msg); public void run() int sum=0; for(int i=1000;i=2000;i+) if(i % 5 =0) System.out.println(i); public
10、 class Exam5 public static void main(String args) Thread1 x = new Thread1(Thread1); Thread2 y = new Thread2(Thread2); x.start(); y.start(); 5.水仙花数是指这样的三位数,其个位、十位和百位三个数的平方和等于这个三位数本身,请编写程序打印输出所有(100999之间)的水仙花数。解答:public class Narcissus public static void main(String args) int i,j,k,n=100,m=1; while (n
11、1000) i=n/100; j=(n-i*100)/10; k=n%10; if(Math.pow(i,3)+Math.pow(j,3)+Math.pow(k,3)=n) System.out.println(找到第+m+ +个水仙花数:+n); n+; m=1; / 或者使用下面的方法 for (n=100;n1000;n+) i=n/100; j=(n-i*100)/10; k=n%10; if(Math.pow(i,3)+Math.pow(j,3)+Math.pow(k,3)=n) System.out.println(找到第+m+ +个水仙花数:+n); n+; 6. 编写程序随机生
12、成10个1到200之间的正整数,打印输出这些随机数并求它们的最大值、最小值、平均值。解答:import java.util.Arrays;public class Test public static void main(String args) int a= new int10; double sum=0; for(int i=0;ia.length;i+) ai=(int)(Math.random()*200+1); sum+=ai; System.out.print(ai+ ); Arrays.sort(a); System.out.println(nmin=+a0); System.o
13、ut.println(max=+aa.length-1); System.out.println(average=+(sum/a.length); 7.按以下要求定义一个类Circle描述一个圆,并完成相应的操作:(1) 实例属性:圆心x坐标(xpoint)、圆心y坐标(ypoint)和半径(radius)。(2) 构造方法:给3个属性赋初值。 (3) 实例方法(area):求圆的面积。(4) 实例方法(circumference):求圆的周长。(5) 重写toString()方法,返回圆心坐标和半径。(6) 实例化这个类,调用方法完成信息的输出。解答:class Circle private double xpoint; private double ypoint; private double radius; public Circle(double x,double y,double r) xpoint = x; ypoint = y; radius = r; public double area() return Math.PI*radius*radius; public double circumference() return 2* Math.PI*radius; public String toString() return 圆心:(+xpoint+,+yp
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1