1、Java编程题解读程序题1.计算1/1+1/2+1/3+.+1/100的值。代码如下:public class Demo1 public static void main(String args) double sum = 0; double n = 1; while (n =0) sum=sum+jiecheng(n); n-; return sum; public static int jiecheng(int n) if(n=1|n=0) return n; else return n*jiecheng(n-1); 4. 将整数数组a赋初值(含8个元素),并求出a中的最大元素和最大元素的
2、下标。代码如下:public class Demo4 public static void main(String args) int arr = 9, 5, 8, 6, 4, 2, 3, 18 ; int maxIndex = 0, max = arr0; for (int i = 0; i max) maxIndex = i; System.out.println(最大的元素为: + arrmaxIndex + 其下标为: + maxIndex); System.out.println(+); System.out.println(CZ04112267); System.out.print
3、ln(徐延迟); 5. 从一堆数组中找出最大的元素和最小的元素代码如下;public class Demo5 public static void main(String args) int arr = 33, 56, 9, 46, 99, 5656, 5, 6, 4, 9 ; int max, min; max = min = arr0; for (int i = 0; i max) max = arri; if (arri min) min = arri; System.out.println(最大值为: + max + n最小值为: + min); System.out.println(
4、+); System.out.println(CZ04112267); System.out.println(徐延迟); 6.计算二维数组中各行元素之和并查找其值最大的那个行,假定元素值都为正整数。代码如下:public class Demo6 public static void main(String args) int arr = 11, 23, 66, 77, 5 , 1, 23, 5, 66, 4 , 5, 6, 8, 9 , 1, 3, 4 ; int sum = 0; int maxSum = 0; int indexI = 0; for (int i = 0; i arr.le
5、ngth; i+) int j; for (j = 0; j maxSum) maxSum = sum; indexI = i; System.out.println(第 + (i+1) + 行的数组之后为: + sum); System.out.println(最大一行为: + (indexI+1); System.out.println(+); System.out.println(CZ04112267); System.out.println(徐延迟); 7. 定义一个计算n!的类Fact,然后在主类Class1中创建一个对象N,求解4!的值。代码如下public class Demo7
6、 public static void main(String args) Fact N= new Fact(); System.out.println(N.jiecheng(4); System.out.println(+); System.out.println(CZ04112267); System.out.println(徐延迟); class Fact public int jiecheng(int n) if (n = 0 | n = 1) return n; else return n * jiecheng(n - 1); 8.编写一个教师类Teacher,要求:(1) 教师类T
7、eacher属性有:name : String类对象,代表姓名age : int型,代表年龄sex :boolean型,代表性别(其中:true表示男,false表示女)phone :long型,代表联系电话(2) 教师类Teacher方法有:Teacher(String n,inta,Booleans,long p):有参构造函数,形象表中的参数分别初始化姓名、年龄、性别和联系电话。intgetAge() :获取年龄作为方法的返回值booleangetSex() : 获取性别作为方法的返回值public String toString() : 以姓名:联系电话的形式作为方法的返回值。代码如下
8、:public class Demo8 private String name=徐延迟;/姓名 private int age; /年龄 private boolean sex;/代表性别(其中:true表示男,false表示女) private long phone=123456;/联系电话 public String getName() return name; public void setName(String name) this.name = name; public int getAge() return age; public void setAge(int age) this
9、.age = age; public boolean getSex() return sex; public void setSex(boolean sex) this.sex = sex; public long getPhone() return phone; public void setPhone(long phone) this.phone = phone; public String toString() return name+ : +phone; public static void main(String args) Demo8 d= new Demo8(); System.
10、out.println(d.toString(); System.out.println(+); System.out.println(CZ04112267); System.out.println(徐延迟); 9.定义一个复数类ComplexNumber,其中定义属性来表示复数的实部和虚部,并实现复数的的相加和相减的基本方法public class Demo9 / main方法 public static void main(String a) Complex1 b = new Complex1(2, 5); Complex1 c = new Complex1(3, 4); System.out.println( b+ c = + b.add(c).m+i+b.add(c).n); System.out.println( b- c = + b.add(c).m+i-+b.add(c).n); System.out.println(+
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1