1.用循环的嵌套,输出输出如下图形
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
package Exercises.One_Hundred; public class Demo01 {public static void main(String[] args) {for(int i=1;i<5;i++){for(int j=1;j<=2*i-1;j++){System.out.print("*");}System.out.println();} for(int i=3;i>0;i--){for(int j=0;j<2*i-1;j++){System.out.print("*");}System.out.println();}} }
2.写一段程序对数组int[] array内的元素从小到大重新排列
package Exercises.One_Hundred; import java.util.Scanner; public class Demo02 {public static void main(String[] args) {int s=0;Scanner scanner=new Scanner(System.in);System.out.println("输入数组长度");s=scanner.nextInt();int []array=new int[s];System.out.println("输入数组元素");for(int i=0;i<s;i++){array[i]=scanner.nextInt();}System.out.println("排列后数组为:"); int temp;for(int i=0;i<s-1;i++){for(int j=0;j<s-1-i;j++){if(array[j]>array[j+1]){temp=array[j];array[j]=array[j+1];array[j+1]=temp;}}} for(int i=0;i<s;i++){System.out.print(array[i]+" ");} } }
或者用Arrays类中sort方法
package Exercises.One_Hundred; import java.util.Arrays; import java.util.Scanner; public class Demo02 {public static void main(String[] args) {int s=0;Scanner scanner=new Scanner(System.in);System.out.println("输入数组长度");s=scanner.nextInt();int []array=new int[s];System.out.println("输入数组元素");for(int i=0;i<s;i++){array[i]=scanner.nextInt();}System.out.println("排列后数组为:"); Arrays.sort(array); for(int i=0;i<s;i++) {System.out.print(array[i] + " ");} } }
3.求 2/1+3/2+5/3+8/5+13/8.....前20项之和?
package Exercises.One_Hundred; public class Demo03 {public static void main(String[] args) {double fm,fz=2;double sum=0,temp=0;for(fm=1;fm<=20;fm++){ temp=fz/fm;sum+=temp;fz=fz+fm;}System.out.println(sum);} }
4.计算圆周率:
package Exercises.One_Hundred; public class Demo04 {public static void main(String[] args) {System.out.println(Math.PI); } }
5.定义两个数3和10,输出大数和小数
package Exercises.One_Hundred; public class Demo05 {public static void main(String[] args) {int a=3;int b=10;if(a>b){System.out.println("大数为:"+a);System.out.println("小数为:"+b);}else {System.out.println("大数为:"+b);System.out.println("小数为:"+a);}} }
6.用if..else语句,判断一个数字是奇数还是偶数
package Exercises.One_Hundred; import java.util.Scanner; public class Demo06 {public static void main(String[] args) {int a;Scanner scanner=new Scanner(System.in);System.out.println("请输入一个整数");a=scanner.nextInt(); if(a%2==0){System.out.println(a+"是偶数");}else{System.out.println(a+"是奇数");}} }
\7. 定义两个数3和10,用三目运算符输出最大数
package Exercises.One_Hundred; public class Demo07 {public static void main(String[] args) {int a=3;int b=10;int max = 0; max=a>b?a:b;System.out.println(max); } }
\8. 定义两个数3和10,用if语句输出最大数
package Exercises.One_Hundred; public class Demo08 {public static void main(String[] args) {int a=3;int b=10;if(a>b){System.out.println(a);}else{System.out.println(b);}} }
9.用while输出1+2+……+10的和
package Exercises.One_Hundred; public class Demo09 {public static void main(String[] args) {int i=1;int sum=0;while (i<=10){sum+=i;i++;}System.out.println(sum);} }
10.用do while语句 输出1+2+……+10的和
package Exercises.One_Hundred; public class Demo10 {public static void main(String[] args) {int i=1;int sum=0;do{sum+=i;i++;}while (i<=10);System.out.println(sum);} }
\11. 用for语句 输出1+2+……+10的和
package Exercises.One_Hundred; public class Demo11 {public static void main(String[] args) {int sum=0;for(int i=1;i<=10;i++){sum+=i;;}System.out.println(sum);} }
12.输出1-100之间不能被3整除的数
package Exercises.One_Hundred; public class Demo12 {public static void main(String[] args) {for(int i=1;i<=100;i++){if(i%3!=0){System.out.println(i);}}} }
13.定义数组,数组元素的值为67,89,87,69,90,100,75,90,用循环找出数组的最大数和最小数
package Exercises.One_Hundred; public class Demo13 {public static void main(String[] args) {int[]array={67,89,87,69,90,100,75,90};int max=array[0];int min=array[array.length-1];for(int i=0;i<array.length;i++){if(array[i]>max){max=array[i];}if(array[i]<min){min=array[i];}}System.out.println("最大值为"+max+" 最小值为"+min);} }
14.定义方法addInt()和addFloat(),第一个方法能求两个整数的和, 第二个方法能求两个实数的和,写测试类,调用这两个方法
package Exercises.One_Hundred; public class Demo14 {public static int addInt(int a,int b){return a+b;}public static double addFloat(double a,double b){return a+b;} public static void main(String[] args) {System.out.println(addInt(3, 10));System.out.println(addFloat(3.5, 4.7));} }
15.用递归方法求5!
package Exercises.One_Hundred; public class Demo15 {public static int f(int s){if(s==1||s==0){return 1;} return s*f(s-1);} public static void main(String[] args) {System.out.println(f(5));} }
16.定义Person类,Person类有name和age属性,有方法tell()输出Person实例的姓名和年龄,定义测试类,创建Person对象person他的名字叫Tom,年龄18,person调用tell()方法输出它的姓名和年龄
package Exercises.One_Hundred.Dem016; public class Person {String name;int age; public void tell(){System.out.println(name+" "+age);} }
package Exercises.One_Hundred.Dem016; public class test {public static void main(String[] args) {Person person=new Person();person.name="Tom";person.age=18;person.tell();} }
\17. 定义Person类,Person类有name和age属性,
有方法tell()输出Person实例的姓名和年龄,
有构造方法实现对域的初始化
定义测试类,创建Person对象person他的名字叫Tom,年龄18,person调用tell()方法输出它的姓名和年龄
package Exercises.One_Hundred.Demo17; public class Person {String name;int age; public Person(String name,int age){this.name=name;this.age=age;} public void tell(){System.out.println(name+" "+age);} }
package Exercises.One_Hundred.Demo17; import Exercises.One_Hundred.Dem016.Person; public class test {public static void main(String[] args) {Person person=new Person("Tom",18);person.tell(); } }
\18. 定义父类Person和子类Student,父类有非私有的name和age,有两个构造方法,一个是无参的构造方法,一个是有参的构造方法,子类有自己的属性school,有无参的和有三个参数的构造方法
package Exercises.One_Hundred.Demo18; public class Person {String name;int age;public Person(){}public Person(String name,int age){this.name=name;this.age=age;} }
package Exercises.One_Hundred.Demo18; public class Student extends Person{String school;public Student(){ } public Student(String name,int age,String school){this.name=name;this.age=age;this.school=school;} }
\19. 定义父类Person和子类Student,父类有非私有的name和age,有两个构造方法,一个是无参的构造方法,一个是有参的构造方法,子类有自己的属性school,有无参的和有三个参数的构造方法,其中第二个构造方法在初始化时要调用父类的构造方法
package Exercises.One_Hundred.Demo19; public class Person {String name;int age;public Person(){}public Person(String name,int age){this.name=name;this.age=age;} }
package Exercises.One_Hundred.Demo19; public class Student extends Person{String school;public Student(){ } public Student(String name,int age,String school){Person person=new Person(name,age);this.school=school;} }
\20. 定义抽象父类Person,子类Student,父类有非私有的name和age,有两个构造方法,一个是无参的构造方法,一个是有参的构造方法,有一个抽象的方法String getInfo(),子类有自己的属性school,有无参的和有三个参数的构造方法,要求在子类中重写抽象方法,并输出子类的各项信息
package Exercises.One_Hundred.Demo18.Demo20; public abstract class Person {String name;int age; public Person(){ } public Person(String name,int age){this.name=name;this.age=age;} public abstract String getInfo(); }
package Exercises.One_Hundred.Demo18.Demo20; public class Student extends Person{String school; public Student(){ } public Student(String name,int age,String school){this.name=name;this.age=age;this.school=school;}@Overridepublic String getInfo() {return name+" "+age+" "+school;} }