题目1:
有两只老虎,一只体重为180kg,一只体重为200kg,请用程序实现判断两只老虎的体重是否相同。
代码如下:
public class OperatorTest {public static void main (String[] args) {int w1 = 180;int w2 = 200;boolean ans = w1 == w2?true:false;System.out.println("ans = "+ans);}
}
题目2:
有3个人,分别为a,b,c,其中a身高为150cm,b身高为210cm,c身高为165cm,请用程序实现获取这3人中的最高身高。
代码如下:
public class OperatorTest {public static void main (String[] args) {int h1 = 150;int h2 = 210;int h3 = 165;int tmp = (h1 > h2)?h1:h2;int ans = (tmp > h3)?tmp:h3;System.out.println(ans);}
}
题目3:
任意给出一个整数,请用程序判断该数是奇数还是偶数,并在控制台输出该整数是奇数还是偶数。
代码如下:
import java.util.Scanner;public class OperatorTest
{public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("请输入一个整数:");int number = sc.nextInt();if (number%2==0)System.out.println(number + "是偶数");else System.out.println(number + "是奇数");}
}
测试结果:
题目4:
小明马上要期末考试,根据小明不同的成绩,用程序实现小明该得到怎么样的礼物,并用控制台输出。
代码如下:
import java.util.Scanner;public class OperatorTest
{public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("请输入一个分数:");int score = sc.nextInt();if (score > 100 || score < 0) System.out.println("你输入的分数有误");else if (score >= 95 && score <= 100) System.out.println("自行车一辆");else if (score >= 90 && score <= 94) System.out.println("游乐场玩一次");else if (score >= 80 && score <= 89) System.out.println("变形金刚玩具一个");else System.out.println("打一顿就好了");}//数据测试:正确数据,边界数据,错误数据
}
题目5:
一年有12个月,请输入一个月份,输出该月属于哪个季节。
代码如下:
写法一:
import java.awt.desktop.SystemEventListener;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;public class OperatorTest{public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("请输入一个月份:");int month = sc.nextInt();/*春:3,4,5夏:6,7,8秋:9,10,11冬:1,2,12*/switch (month){case 1:System.out.println("冬季");break;case 2:System.out.println("冬季");break;case 3:System.out.println("春季");break;case 4:System.out.println("春季");break;case 5:System.out.println("春季");break;case 6:System.out.println("夏季");break;case 7:System.out.println("夏季");break;case 8:System.out.println("夏季");break;case 9:System.out.println("秋季");break;case 10:System.out.print("秋季");break;case 11:System.out.println("秋季");break;case 12:System.out.println("冬季");break;default:System.out.println("输入有误");break;}}
}
写法二:
import java.awt.desktop.SystemEventListener;
import java.util.Scanner;
import java.util.concurrent.SynchronousQueue;public class OperatorTest{public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("请输入一个月份:");int month = sc.nextInt();/*春:3,4,5夏:6,7,8秋:9,10,11冬:1,2,12*/switch (month){case 1:case 2:case 12:System.out.println("冬季");break;case 3:case 4:case 5:System.out.println("春季");break;case 6:case 7:case 8:System.out.println("夏季");break;case 9:case 10:case 11:System.out.println("秋季");break;default:System.out.println("输入有误");}}
}
题目6:
输出1-5和5-1的数据。
代码如下:
import java.util.Scanner;public class OperatorTest{public static void main(String[] args){for (int i = 1;i<=5;i++) System.out.println(i);System.out.println("--------------------");for (int i = 5;i>=1;i--) System.out.println(i);}
}
题目7:
求和:
求1-5之间的和,并输出。
代码如下:
import java.util.Scanner;public class OperatorTest{public static void main(String[] args){int sum = 0;for (int i = 1;i<=5;i++) sum+=i;System.out.println(sum);}
}
题目8:
求偶数和。
代码如下:
import java.util.Scanner;public class OperatorTest{public static void main(String[] args){int sum = 0;for (int i =1;i<=100;i++){if (i % 2==0) sum+=i;}System.out.println(sum);}
}
题目9:
输出所有的水仙花数。
水仙花数是3位数,水仙花数的个位,十位,百位的数字立方和等于原数。
代码如下:
import java.util.Scanner;public class OperatorTest{public static void main(String[] args){for (int i = 100;i<1000;i++){int a = i%10;int b = i/10%10;int c = i/100;if ((a*a*a+b*b*b+c*c*c)==i) System.out.println(i);}}
}
题目10:
统计水仙花数有多少个?
代码如下:
import java.util.Scanner;public class OperatorTest{public static void main(String[] args){int cnt = 0;for (int i = 100;i<1000;i++){int a = i%10;int b = i/10%10;int c = i/100;if ((a*a*a+b*b*b+c*c*c)==i) cnt++;}System.out.println(cnt);}
}
题目11:
世界最高峰(8843.43米 = 8844430毫米),假如我有一张足够大的纸,它的厚度为0.1毫米,请问,折叠多少次可以达到这个山峰的高度。
代码如下:
import java.awt.*;public class OperatorTest
{public static void main(String[] args){double paper = 0.1;int zf = 8844430;int cnt = 0;while(paper <= zf){paper *=2;cnt++;}System.out.println(cnt);}
}
题目12:
程序自动生成1-100之间的数字,使用程序实现猜出这个数字是多少?
代码如下:
import java.io.OutputStream;
import java.util.Random;
import java.util.Scanner;public class OperatorTest
{public static void main(String[] args){Random r = new Random();int number = r.nextInt(100)+1;while(true){Scanner sc = new Scanner(System.in);System.out.println("请输入你要猜的数字:");int guessNumber = sc.nextInt();if (guessNumber > number) System.out.println("你猜的数字"+guessNumber+"大了");else if (guessNumber < number) System.out.println("你猜的数字"+guessNumber+"小了");else {System.out.println("恭喜你猜对了");break;}}}
}
题目13:
设计一个方法用于打印两个数中的较大值。
代码如下:
public class OperatorTest
{public static void main(String[] args){int a = 13;int b = 7;GetMax(a,b);}public static void GetMax(int a,int b){if (a > b) System.out.println(a);else System.out.println(b);}
}
题目14:
使用方法重载的思想,设计比较两个整数是否相同的方法,兼容全整数类型(byte,short,int,long)。
代码如下:
public class OperatorTest
{public static boolean Compare(byte a,byte b){return a==b;}public static boolean Compare(int a,int b){return a==b;}public static boolean Compare(short a,short b){return a==b;}public static boolean Compare(long a,long b){return a==b;}public static void main(String[] args){int a = 20;int b = 30;boolean ans = Compare(a,b);System.out.println(ans);}
}
题目15:
设计一个方法用于数组遍历,要求遍历的结果是在同一行上的,例如:[11,22,33,44,55]。
代码如下:
public class OperatorTest
{public static void main(String[] args){int [] arr ={11,22,33,44,55};printArray(arr);}public static void printArray(int[] arr){System.out.print("[");for (int i = 0;i<arr.length;i++){if (i== arr.length-1)System.out.print(arr[i]);else System.out.print(arr[i]+",");}System.out.print("]");}
}
题目16:
设计一个方法用于获取数组中的最大值,调用方法并输出结果。
代码如下:
import java.awt.desktop.SystemEventListener;public class OperatorTest
{public static void main(String[] args){int [] arr ={11,22,33,44,55};int maxElem = getMax(arr);System.out.println(maxElem);}public static int getMax(int [] arr){int maxElem = arr[0];for (int i =1;i<arr.length;i++){if (maxElem < arr[i]) maxElem = arr[i];}return maxElem;}
}
题目17:
有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第二十个月的兔子对数为多少?
代码如下:
import java.awt.desktop.SystemEventListener;public class OperatorTest
{public static void main(String[] args){int [] arr = new int [20];arr[0] =1;arr[1] = 1;for (int i = 2;i<arr.length;i++){arr[i] = arr[i-1]+arr[i-2];}System.out.println(arr[19]);}
}
题目18:
设计一个方法,用于比较两个数组的内容是否相同。
代码如下:
import java.awt.desktop.SystemEventListener;public class OperatorTest
{public static void main(String[] args){int [] arr1 = {11,22,33,44,55};int [] arr2 = {11,22,33,44,55};boolean ans = compareArray(arr1,arr2);System.out.println(ans);}public static boolean compareArray(int [] arr1,int [] arr2){if (arr1.length!=arr2.length) return false;for (int i = 0;i<arr1.length;i++){if (arr1[i]!=arr2[i]) return false;}return true;}
}
题目19:
已知一个数组arr = {19,28,37,46,50};键盘录入一个数据,查找该数据在数组中的索引,并在控制台输出找到的索引值。
代码如下:
import java.util.Scanner;public class OperatorTest
{public static void main(String[] args){int [] arr = {11,22,33,44,55,66,80};Scanner sc = new Scanner(System.in);int number = sc.nextInt();int ans = findElem(arr,number);System.out.println(ans);}public static int findElem(int [] arr,int f){for (int i = 0;i<arr.length;i++){if (f==arr[i]) return i;}return -1;}
}
题目20:
已知一个数组arr = {19,28,37,46,50};把它们反转变成{50,46,37,28,19};
代码如下:
import java.util.Scanner;public class OperatorTest
{public static void main(String[] args){int [] arr = {19,28,37,46,50};reverseArray(arr);System.out.print("[");for (int i = 0;i<arr.length;i++){if (i== arr.length-1)System.out.print(arr[i]);else System.out.print(arr[i]+",");}System.out.print("]");}public static void reverseArray(int [] arr){for (int start = 0,end = arr.length-1;start <= end;start++,end--){int tmp = arr[start];arr[start] = arr[end];arr[end] = tmp;}}}
题目21:
首先定义一个学生类,然后定义一个学生测试类,在学生测试类中通过对对象完成成员变量和成员方法的使用。
代码如下:
package test01;public class Student {String name;int age;public void study(){System.out.println("好好学习,天天向上");}public void doHomework(){System.out.println("键盘敲烂,月薪过万");}}
package test01;public class StudentDemo {public static void main(String[] args){Student s = new Student();System.out.println(s.name+","+s.age);s.name ="Tom";s.age = 30;System.out.println(s.name+","+s.age);s.study();s.doHomework();}
}
题目22:
使用Scanner类,实现从键盘录入一个字符串,最后输出在控制台。
代码如下:
import java.util.Scanner;public class OperatorTest
{public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串数据:");String line = sc.nextLine();//快捷键: Ctrl + Alt + V System.out.println("你输入的数据是"+line);}
}
题目23:
用户登录
已知用户名和密码,请用程序实现模拟用户登录,总共给三次机会,登录之后,给出相应的提示。
代码如下:
import java.util.Scanner;public class OperatorTest
{public static void main(String[] args){String userName = "Tom";String passWord = "abcdef";for(int i =0;i<3;i++) {Scanner sc = new Scanner(System.in);System.out.println("请输入用户名:");String name = sc.nextLine();System.out.println("请输入密码:");String pwd = sc.nextLine();if (name.equals(userName) && pwd.equals(passWord)) {System.out.println("登陆成功");break;}else if (2-i==0) System.out.println("你无了!!!");else System.out.println("登陆失败,你还有"+(2-i)+"次机会");}}
}
题目24:
键盘录入一个字符串,使用程序实现在控制台遍历该字符串。
代码如下:
import java.util.Scanner;public class OperatorTest
{public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串:");String line = sc.nextLine();for (int i = 0;i<line.length();i++){System.out.println(line.charAt(i));}}
}
题目25:
键盘录入一个字符串,统计该字符串中大写字母字符,小写字母字符,数字字符出现的次数(不考虑其他字符)。
代码如下:
import java.util.Scanner;public class OperatorTest
{public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串:");String line = sc.nextLine();int bigCnt = 0;int smallCnt = 0;int numberCnt = 0;for (int i = 0;i<line.length();i++){char ch = line.charAt(i);if (ch >= 'A' && ch <= 'Z') bigCnt++;else if (ch >= 'a' && ch <= 'z') smallCnt++;else if (ch >= '0' && ch <= '9') numberCnt++;}System.out.println("大写字母:"+bigCnt);System.out.println("小写字母:"+smallCnt);System.out.println("数字:"+numberCnt);}
}
题目26:
定义一个方法,把int数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,并在控制台输出结果。例如,数组为int[] arr = {1,2,3};,执行方法后的输出结果为:[1,2,3]
代码如下:
public class OperatorTest
{public static void main(String[] args){int [] arr = {1,2,3};String s = arrayToString(arr);System.out.println(s);}public static String arrayToString(int [] arr){String s = "";s+="[";for (int i = 0;i<arr.length;i++){if (i==arr.length-1) s+=arr[i];else {s+=arr[i];s+=",";}}s+="]";return s;}
}
题目27:
定义一个方法,实现字符串反转,键盘录入一个字符串,调用该方法后,在控制台输出结果。
代码如下:
import java.util.Scanner;public class OperatorTest
{public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串:");String line = sc.nextLine();String ans = reverse(line);System.out.println(ans);}public static String reverse(String s){String ss = "";for (int i = s.length()-1;i>=0;i--){ss+=s.charAt(i);}return ss;}
}
题目28:
(用StringBuilder)
定义一个方法,把int数组中的数据按照指定的格式拼接成一个字符串返回,调用该方法,并在控制台输出结果。例如,数组为int[]arr = {1,2,3};,执行方法后的输出结果为:[1,2,3]。
代码如下:
import java.util.Scanner;public class OperatorTest
{public static void main(String[] args){int [] arr = {1,2,3};String s = arrayToString(arr);System.out.println(s);}public static String arrayToString(int [] arr){StringBuilder sb = new StringBuilder();sb.append("[");for(int i = 0;i<arr.length;i++){if (i==arr.length-1) sb.append(arr[i]);else sb.append(arr[i]).append(",");}sb.append("]");String s = sb.toString();return s;}
}
题目29:
(用StringBuilder)
定义一个方法,实现字符串反转。键盘录入一个字符串,调用该方法后,在控制台输出结果。例如,键盘录入abc,输出结果cba。
代码如下:
import java.util.Scanner;public class OperatorTest
{public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("请输入一个字符串");String line = sc.nextLine();String s = myReverse(line);System.out.println(s);}public static String myReverse(String s){
// StringBuilder sb = new StringBuilder(s);
//
// sb.reverse();
//
// String ss = sb.toString();
// return ss;return new StringBuilder(s).reverse().toString();}}
题目30:
创建一个存储字符串的集合,存储3个字符串元素,使用程序实现在控制台遍历该集合。
代码如下:
import java.util.ArrayList;public class OperatorTest
{public static void main(String[] args){ArrayList<String> array = new ArrayList<>();array.add("Tom");array.add("Jack");array.add("Lily");for (int i = 0;i<array.size();i++){String s = array.get(i);System.out.println(s);}}
}
题目31:
创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合。
代码如下:
package test03;public class Student {private String name;private int age;public Student(){}public Student(String name ,int age){this.name = name;this.age = age;}public void setName(String name){this.name = name;}public void setAge(int age){this.age = age;}public int getAge(){return age;}public String getName(){return name;}
}
package test03;import java.util.ArrayList;public class ArrayStudent {public static void main(String[] args){ArrayList<Student> array = new ArrayList<>();Student s1 = new Student("Tom",30);Student s2 = new Student("Jack",18);Student s3 = new Student("Lily",21);array.add(s1);array.add(s2);array.add(s3);for (int i =0;i<array.size();i++){Student s = array.get(i);System.out.println(s.getName()+"," + s.getAge());}}
}
题目32:
创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合,学生的姓名和年龄来自于键盘录入。
代码如下:
package test03;public class Student {private String name;private String age;public Student(){}public Student(String name ,String age){this.name = name;this.age = age;}public void setName(String name){this.name = name;}public void setAge(String age){this.age = age;}public String getAge(){return age;}public String getName(){return name;}
}
package test03;import java.util.ArrayList;
import java.util.Scanner;public class ArrayStudent {public static void main(String[] args){ArrayList<Student> array = new ArrayList<>();addStudent(array);addStudent(array);addStudent(array);for (int i = 0;i<array.size();i++){Student s = array.get(i);System.out.println(s.getName() + "," + s.getAge());}}public static void addStudent(ArrayList<Student> array){Scanner sc = new Scanner(System.in);System.out.println("请输入学生姓名:");String name = sc.nextLine();System.out.println("请输入学生年龄:");String age = sc.nextLine();Student s = new Student();s.setName(name);s.setAge(age);array.add(s);}
}
题目33:
定义老师类和学生类,然后写代码调试;最后找到老师类和学生类当中的共性内容,抽取出一个父类,用继承的方法改写代码,并进行测试。
代码如下:
package test05;public class Person {private String name;private int age;public Person() {}public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
package test05;public class Teacher extends Person{public Teacher(){}public Teacher(String name,int age){super(name,age);}public void teach(){System.out.println("教书育人!!!");}}
package test05;public class Student extends Person{public void study(){System.out.println("好好学习!!!");}
}
package test05;import java.io.OutputStream;public class PersonDemo {public static void main(String[] args){Teacher t1 = new Teacher();t1.setName("Tom");t1.setAge(30);System.out.println(t1.getName() + "," + t1.getAge());t1.teach();Teacher t2 = new Teacher("Jack",22);System.out.println(t2.getName() + "," + t2.getAge());t2.teach();}}
题目34:
请采用继承的思想实现猫和狗的案列,并在测试类中进行测试。
代码如下:
package test06;public class Animal {private String name;private int age;public Animal() {}public Animal(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
package test06;public class Cat extends Animal {public Cat() {}public Cat(String name, int age) {super(name, age);}public void catchMouse(){System.out.println("猫抓老鼠");}
}
package test06;public class Dog extends Animal{public Dog() {}public Dog(String name, int age) {super(name, age);}public void lookDoor(){System.out.println("狗看门");}
}
package test06;import java.io.OutputStream;public class AnimalDemo {public static void main(String [] args){Cat c1 = new Cat();c1.setName("加菲猫");c1.setAge(5);System.out.println(c1.getName()+","+c1.getAge());c1.catchMouse();Cat c2 = new Cat("加菲猫",5);System.out.println(c2.getName()+","+c2.getAge());c2.catchMouse();}}
题目35:
请采用多态的思想实现猫和狗的案列,并在测试类中进行测试。
代码如下:
package test07;public class Animal {private String name;private int age;public Animal() {}public Animal(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public void eat(){System.out.println("动物吃东西");}}
package test07;public class Cat extends Animal{public Cat() {}public Cat(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println("猫吃鱼");}}
package test07;public class Dog extends Animal{public Dog() {}public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println("狗吃骨头");}
}
package test07;public class AnimalTest {public static void main(String [] args){Animal a = new Cat();a.setAge(5);a.setName("加菲");System.out.println(a.getName()+","+a.getAge());a.eat();a = new Cat("加菲",5);System.out.println(a.getName()+","+a.getAge());a.eat();}
}
题目36:
请采用抽象类的思想实现猫和狗的案列,并在测试类中进行测试。
代码如下:
package test08;public abstract class Animal {private String name;private int age;public Animal() {}public Animal(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public abstract void eat();
}
package test08;public class Cat extends Animal{public Cat() {}public Cat(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println("猫吃鱼");}
}
package test08;public class Dog extends Animal{public Dog() {}public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println("狗吃骨头");}
}
package test08;public class AnimalDemo {public static void main(String[] args){Animal a = new Cat();a.setName("加菲");a.setAge(5);System.out.println(a.getName()+","+a.getAge());a.eat();a = new Cat("加菲",5);System.out.println(a.getName()+","+a.getAge());a.eat();}}
题目37:
对猫和狗进行训练,他们就可以跳高了,这里加入了跳高功能。请采用抽象类和接口来实现猫狗案列,并在测试类中进行测试。
代码如下:
package test09;public interface Jumpping {public abstract void jump();
}
package test09;public abstract class Animal {private String name;private int age;public Animal() {}public Animal(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public abstract void eat();
}
package test09;public class Cat extends Animal implements Jumpping{public Cat() {}public Cat(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println("猫吃鱼");}@Overridepublic void jump() {System.out.println("猫可以跳高了");}
}
package test09;public class Dog extends Animal implements Jumpping{public Dog() {}public Dog(String name, int age) {super(name, age);}@Overridepublic void eat() {System.out.println("狗啃骨头");}@Overridepublic void jump() {System.out.println("狗可以跳高了");}
}
package test09;public class AnimalDemo {public static void main(String [] args){Jumpping j = new Cat();j.jump();System.out.println("--------------");Animal a = new Cat();a.setName("加菲");a.setAge(5);System.out.println(a.getName()+","+a.getAge());a.eat();a = new Cat("加菲",5);System.out.println(a.getName()+","+a.getAge());a.eat();System.out.println("----------------");Cat c = new Cat();c.setName("加菲");c.setAge(5);System.out.println(c.getName()+","+c.getAge());c.eat();c.jump();}
}
题目38:
我们现在有乒乓球运动员和篮球运动员,兵乒球教练和篮球教练,为了出国交流,跟乒乓球相关的人员都需要学习英语。请用所学的知识分析,这个案例中有哪些具体类,哪些抽象类,哪些接口,并用代码实现。
分析:
思路:
代码如下:
package test10;public interface SpeakEnglish {public abstract void speak();
}
package test10;public abstract class Person {private String name;private int age;public Person() {}public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public abstract void eat();
}
package test10;public abstract class Coach extends Person{public Coach() {}public Coach(String name, int age) {super(name, age);}public abstract void teach();
}
package test10;public abstract class Player extends Person {public Player() {}public Player(String name, int age) {super(name, age);}public abstract void study();
}
package test10;public class BasketballCoach extends Coach{public BasketballCoach() {}public BasketballCoach(String name, int age) {super(name, age);}@Overridepublic void teach() {System.out.println("篮球教练教如何运球和投篮");}@Overridepublic void eat() {System.out.println("篮球教练吃羊肉喝羊奶");}
}
package test10;public class PingPangCoach extends Coach implements SpeakEnglish{public PingPangCoach() {}public PingPangCoach(String name, int age) {super(name, age);}@Overridepublic void teach() {System.out.println("乒乓球教练教如何发球和接球");}@Overridepublic void eat() {System.out.println("乒乓球吃小白菜和大米粥");}@Overridepublic void speak() {System.out.println("乒乓球教练说英语");}
}
package test10;public class BasketballPlayer extends Player{public BasketballPlayer() {}public BasketballPlayer(String name, int age) {super(name, age);}@Overridepublic void study() {System.out.println("篮球运动员学习如何运球和投篮");}@Overridepublic void eat() {System.out.println("篮球运动员吃牛肉喝牛奶");}}
package test10;public class PingPangPlayer extends Player implements SpeakEnglish{public PingPangPlayer() {}public PingPangPlayer(String name, int age) {super(name, age);}@Overridepublic void study() {System.out.println("乒乓球运动员学习如何发球和接球");}@Overridepublic void eat() {System.out.println("乒乓球运动员吃大白菜喝小米粥");}@Overridepublic void speak() {System.out.println("乒乓球运动员说英语");}
}
题目39:
实现冒泡排序。
代码如下:
package test11;public class ArrayDemo {public static void main(String [] args){int [] arr = {24,69,80,57,13};System.out.println("排序前:"+arrayToString(arr));for (int j = 0;j<arr.length-1;j++)for (int i = 0;i<arr.length-1-j;i++)if (arr[i] > arr[i+1]){int tmp = arr[i];arr[i] = arr[i+1];arr[i+1] = tmp;}System.out.println("排序后:"+arrayToString(arr));}public static String arrayToString(int [] arr){StringBuilder sb = new StringBuilder();sb.append("[");for (int i = 0;i<arr.length;i++){if (i==arr.length-1) sb.append(arr[i]);else sb.append(arr[i]).append(",");}sb.append("]");String s = sb.toString();return s;}
}
题目40:
字符串中数据排序。
需求:有一个字符串:“91 27 46 38 50”,请写程序实现最终输出结果是:“27 38 46 50 91”。
代码如下:
package test13;import java.util.Arrays;public class TestDemo {public static void main(String[] args ) {String s = "91 27 46 38 50";String[] strArray = s.split(" ");
// for (int i = 0;i<strArray.length;i++) System.out.println(strArray[i]);int[] arr = new int[strArray.length];for (int i = 0; i < arr.length; i++) {arr[i] = Integer.parseInt(strArray[i]);}Arrays.sort(arr);StringBuilder sb = new StringBuilder();for (int i = 0;i<arr.length;i++){if (i==arr.length-1) sb.append(arr[i]);else sb.append(arr[i]).append(" ");}String ans = sb.toString();System.out.println(ans);}
}
题目41:
需求:
定义一个日期工具类(DateUtils),包含两个方法:把日期转换为指定格式的字符串;把字符串解析为指定格式的日期,然后定义一个测试类(DateDemo),测试日期工具类的方法。
代码如下:
package test14;import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;public class DateUtils {private DateUtils(){}public static String dateToString(Date date, String format){SimpleDateFormat sdf = new SimpleDateFormat(format);String s = sdf.format(date);return s;}public static Date stringToDate(String s,String format) throws ParseException {SimpleDateFormat sdf = new SimpleDateFormat(format);Date d = sdf.parse(s);return d;}}
package test14;import java.text.ParseException;
import java.util.Date;public class DateDemo {public static void main(String[] args) throws ParseException {Date d = new Date();String s1 = DateUtils.dateToString(d,"yyyy年MM月dd日 HH:mm:ss");System.out.println(s1);String s2= DateUtils.dateToString(d,"yyyy年MM月dd日");System.out.println(s2);String s3 = DateUtils.dateToString(d,"HH:mm:ss");System.out.println(s3);System.out.println("---------------------------");String s = "2048-08-09 12:12:12";Date dd = DateUtils.stringToDate(s,"yyyy-MM-dd HH:mm:ss");System.out.println(dd);}
}
题目42:
获取任意一年的二月有多少天?
代码如下:
package test15;import java.util.Calendar;
import java.util.Scanner;public class CalendarDemo {public static void main(String[] args){Scanner sc = new Scanner(System.in);System.out.println("请输入年份");int year = sc.nextInt();Calendar c = Calendar.getInstance();c.set(year,2,1);c.add(Calendar.DATE,-1);System.out.println(c.get(Calendar.DATE));}
}
题目43:
Collection集合存储学生对象并遍历。
需求:
创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合。
代码如下:
package test16;public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}
package test16;import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;public class CollectionDemo {public static void main(String[] args){Collection<Student> c = new ArrayList<Student>();Student s1 = new Student("Tom",35);Student s2 = new Student("Jack",45);Student s3 = new Student("Lily",13);c.add(s1);c.add(s2);c.add(s3);Iterator<Student>it = c.iterator();while(it.hasNext()){Student s = it.next();System.out.println(s.getName()+","+s.getAge());}}
}
题目44:
需求:
创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合。
代码如下:
package test17;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;public class ListDemo {public static void main(String [] args){List<Student> list = new ArrayList<Student>();Student s1 = new Student("Tom",35);Student s2 = new Student("Jack",34);Student s3 = new Student("Lily",15);list.add(s1);list.add(s2);list.add(s3);Iterator<Student> it = list.iterator();while(it.hasNext()){Student s = it.next();System.out.println(s.getName()+","+s.getAge());}System.out.println("----------------------------");for (int i = 0;i<list.size();i++){Student s = list.get(i);System.out.println(s.getName()+","+s.getAge());}}}
题目45:
List集合存储学生对象用三种方式遍历。
需求:
创建一个存储学生对象的集合,存储3个学生对象,使用程序实现该控制台遍历该集合。
代码如下:
package test18;import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;public class ListDemo {public static void main(String[] args){List<Student> list = new ArrayList<Student>();Student s1 = new Student("Tom",30);Student s2 = new Student("Jack",35);Student s3 = new Student("Lily",18);list.add(s1);list.add(s2);list.add(s3);Iterator<Student> it = list.iterator();while(it.hasNext()){Student s = it.next();System.out.println(s.getName()+","+s.getAge());}System.out.println("---------------------------------");for (int i= 0;i<list.size();i++){Student s = list.get(i);System.out.println(s.getName()+","+s.getAge());}System.out.println("----------------------------------");for (Student s:list){System.out.println(s.getName()+","+s.getAge());}}
}
题目46:
ArrayList集合存储学生对象用三种方式遍历。
需求:
创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合。
代码如下:
package test20;import java.util.ArrayList;
import java.util.Iterator;public class ArrayListDemo {public static void main(String[] args){ArrayList<Student> array = new ArrayList<Student>();Student s1 = new Student("Tom",30);Student s2 = new Student("Jack",31);Student s3 = new Student("Lily",32);array.add(s1);array.add(s2);array.add(s3);for (int i = 0;i<array.size();i++){Student s = array.get(i);System.out.println(s.getName()+","+s.getAge());}Iterator<Student> it = array.iterator();while(it.hasNext()){Student s = it.next();System.out.println(s.getName()+","+s.getAge());}for(Student s:array){System.out.println(s.getName()+","+s.getAge());}}}
题目47:
HashSet集合存储学生对象并遍历。
需求:
创建一个存储学生对象的集合,存储多个学生对象,使用程序实现并在控制台遍历该集合。
要求:
学生对象的成员变量值相同,我们就认为是同一个对象。
代码如下:
package HashSetPackage02;public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Student student = (Student) o;if (age != student.age) return false;return name != null ? name.equals(student.name) : student.name == null;}@Overridepublic int hashCode() {int result = name != null ? name.hashCode() : 0;result = 31 * result + age;return result;}
}
package HashSetPackage02;import java.util.HashSet;public class HashSetDemo {public static void main(String[] args){HashSet<Student> hs = new HashSet<Student>();Student s1 = new Student("Tom",35);Student s2 = new Student("Lily",23);Student s3 = new Student("Jack",17);Student s4 = new Student("Tom",35);hs.add(s1);hs.add(s2);hs.add(s3);hs.add(s4);for (Student s:hs){System.out.println(s.getName()+","+s.getAge());}}}
题目48:
成绩排序。
需求:
用TreeSet集合存储多个学生信息(姓名,语文成绩,数学成绩),并遍历该集合。
要求:按照总分从高到低出现。
代码如下:
package GradeSortPack;public class Student {private String name;private int chinese;private int math;public Student() {}public Student(String name, int chinese, int math) {this.name = name;this.chinese = chinese;this.math = math;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getChinese() {return chinese;}public void setChinese(int chinese) {this.chinese = chinese;}public int getMath() {return math;}public void setMath(int math) {this.math = math;}public int getSum(){return this.chinese+this.math;}
}
package GradeSortPack;import java.util.Comparator;
import java.util.TreeSet;public class TreeSetDemo {public static void main(String[] args){TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>() {@Overridepublic int compare(Student s1, Student s2) {
// int num = (s2.getChinese()+s2.getMath())-(s1.getChinese()+s1.getMath());int num = s2.getSum()-s1.getSum();int num2 = num==0?s1.getChinese()-s2.getChinese():num;int num3 = num2==0?s1.getName().compareTo(s2.getName()):num2;return num3;}});Student s1 = new Student("Tom",98,100);Student s2 = new Student("Jack",95,95);Student s3 = new Student("Lily",100,97);Student s4 = new Student("Bom",98,98);Student s5 = new Student("ZhJk",100,98);Student s6 = new Student("Yuio",98,100);ts.add(s1);ts.add(s2);ts.add(s3);ts.add(s4);ts.add(s5);ts.add(s6);for (Student s:ts){System.out.println(s.getName()+","+s.getChinese()+","+s.getMath()+","+s.getSum());}}}
题目49:
不重复的随机数。
需求:
编写一个程序,获取10个1-20之间的随机数,要求随机数不能重复,并在控制台输出。
代码如下:
package RandomNumPack;import java.util.HashSet;
import java.util.Set;
import java.util.Random;public class SetDemo {public static void main(String[] args){Set<Integer> set = new HashSet<Integer>();Random r = new Random();while(set.size() <10){int number = r.nextInt(20)+1;set.add(number);}for (Integer i:set){System.out.println(i);}}}
package RandomNumPack;import java.util.HashSet;
import java.util.Set;
import java.util.Random;
import java.util.TreeSet;public class SetDemo {public static void main(String[] args){Set<Integer> set = new TreeSet<Integer>();Random r = new Random();while(set.size() <10){int number = r.nextInt(20)+1;set.add(number);}for (Integer i:set){System.out.println(i);}}}
题目50:
HashMap集合存储学生对象并遍历。
需求:
创建一个HashMap集合,键是学号(String),值是学生对象(Student)。存储三个键值对元素,并遍历。
代码如下:
package HashMapPrintPack;public class Student {private String name;private int age;public Student() {}public Student(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
package HashMapPrintPack;import java.util.HashMap;
import java.util.Map;
import java.util.Set;public class HashMapDemo {public static void main(String[] args){HashMap<String,Student> hm = new HashMap<String,Student>();Student s1 = new Student("Tom",30);Student s2 = new Student("Lily",18);Student s3 = new Student("Jack",27);hm.put("001",s1);hm.put("002",s2);hm.put("003",s3);//方式1Set<String> keySet = hm.keySet();for (String key:keySet){Student value = hm.get(key);System.out.println(key+","+value.getName()+","+value.getAge());}System.out.println("----------------------------------------");//方式2Set<Map.Entry<String,Student>> entrySet = hm.entrySet();for (Map.Entry<String,Student> me:entrySet){String key = me.getKey();Student value = me.getValue();System.out.println(key+","+value.getName()+","+value.getAge());}}
}