赞
踩
第1关 Object类
- package case1;
-
- import java.util.Scanner;
-
- public class ObjectTest {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int num1 = sc.nextInt();
- int num2 = sc.nextInt();
- // ÔÚ²âÊÔÀàÖд´½¨DemoÀà¶ÔÏód1£¬´«ÈëÊäÈëÖµnum1£¬ d1µ÷ÓÃtoString·½·¨²¢´òÓ¡Êä³ö¸ÃÖµ
- // ´´½¨DemoÀà¶ÔÏód2£¬Í¬Ñù´«ÈëÊäÈëÖµnum1£¬´òÓ¡ÅжÏd1ºÍd2ÊÇ·ñÏàµÈ£¨Êµ¼ÊÊDZȽϵØÖ·£©
- /********* Begin *********/
- Demo d1 = new Demo(num1);
- System.out.println(d1.toString());
- Demo d2 = new Demo(num1);
- System.out.println(d1.equals(d2));
- /********* End *********/
- // ´´½¨PersonÀà¶ÔÏóp£¬´«ÈëÊäÈëÖµnum2£¬´òÓ¡ÅжÏd1ºÍpÊÇ·ñÏàµÈ£¨Êµ¼ÊÊDZȽϵØÖ·£©
- /********* Begin *********/
- Person p = new Person(num2);
- System.out.println(d1.equals(p));
- /********* End *********/
- }
- }
-
- class Demo {
- private int num;
-
- public Demo(int num) {
- this.num = num;
- }
-
- public boolean equals(Object obj) // Object obj = new Demo()
- {
- if (!(obj instanceof Demo)) // ÅжÏobjÊÇ·ñºÍDemoÊÇͬÀà
- return false;
- Demo d = (Demo) obj; // ½«¸¸ÀàµÄÒýÓÃ(Object)ÏòÏÂת»»Îª×ÓÀà(Demo)
- return this.num == d.num;
- }
-
- public String toString() {
- return "Demo:" + num; // ·µ»Ø¶ÔÏóµÄÖµ£¨Ã¿Ò»¸ö¶ÔÏó¶¼ÓÐ×Ô¼ºµÄÌض¨µÄ×Ö·û´®£©
- }
- }
-
- class Person {
- private int num;
-
- public Person(int num) {
- this.num = num;
- }
- }

第2关 JAVA基础类型包装类-练习
- package case2;
-
- import java.util.Scanner;
-
- public class WrapperTest {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int aa = sc.nextInt();
- String bb = sc.next();
- int c = sc.nextInt();
- String str11 = sc.next();
- String str22 = sc.next();
- // °ü×°ÀàÖС°==¡±ÓëequalsµÄÓ÷¨±È½Ï
- // ÖµµÃ×¢ÒâµÄÊÇ£¬°ü×°ÀàÖеÄequals·½·¨ºÍStringÀàÒ»Ñù£¬¶¼ÊÇÖØдÁËObjectÀàÖеÄequals·½·¨£¬Òò´Ë±È½ÏµÄÊÇÄÚÈݶø²»ÊǵØÖ·£¬
- // ¶ø¡°==¡±±È½ÏµÄÒÀÈ»ÊÇÒýÓñäÁ¿µÄµØÖ·£¬Ö»Êǵ±°ü×°ÀàÐͺÍÓëÖ®Ïà¶ÔÓ¦µÄ»ù±¾ÀàÐͽøÐС°==¡±±È½Ïʱ»áÏÈ×ö×Ô¶¯²ðÏä´¦Àí¡£
- /********* Begin *********/
- Integer a = aa;
- Integer b = new Integer(bb);
- String str1 = str11;
- String str2 = str22;
- System.out.println(a==b);
- System.out.println(a==c);
- System.out.println(b==c);
- System.out.println(a.equals(b));
- System.out.println(str1==str2);
- System.out.println(str1.equals(str2));
- /********* End *********/
- }
- }

第3关 String&StringBuilder&StringBuffer类-练习
- package case3;
-
- import java.util.Scanner;
-
- public class StringTest {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String str = sc.next();
- // String substring(int start,int end)
- // ½ØÈ¡×Ö·û´®£¬´«ÈëµÄÁ½¸ö²ÎÊý·Ö±ðΪҪ½ØÈ¡±ß½çµÄϱê
- // ÔÚjava api ÖÐ,ͨ³£Ê¹ÓÃÁ½¸öÊý×Ö±íʾ·¶Î§Ê±,¶¼ÊǺ¬Í·²»º¬Î²,¼´°üº¬Æðʼϱê¶ÔÓ¦µÄÄÚÈÝ,µ«²»°üº¬½áÊøϱêµÄ´¦¶ÔÓ¦µÄÄÚÈÝ
- // String toUpperCase() ½«µ±Ç°×Ö·û´®ÖеÄÓ¢ÎIJ¿·Öת»»ÎªÈ«´óд
- /********* Begin *********/
-
- String str1 = str.substring(12,str.lastIndexOf("."));
- if(str1.indexOf(".")>0){
- str1 = str1.substring(0,str1.indexOf("."));
- }
- System.out.println(str1);
- System.out.println(str1.toUpperCase());
- /********* End *********/
- }
- }

第4关 Random类
- package case4;
-
- //ÃÜÂëµÄ×Ô¶¯Éú³ÉÆ÷£ºÃÜÂëÓÉ´óд×Öĸ/Сд×Öĸ/Êý×Ö×é³É£¬Éú³ÉÁùλËæ»úÃÜÂë
- import java.util.Random;
- import java.util.Scanner;
-
- public class RandomTest {
- public static void main(String[] args) {
- // ¶¨ÒåÒ»¸ö×Ö·ûÐÍÊý×é
- char[] pardStore = new char[62];
- // °ÑËùÓеĴóд×Öĸ·Å½øÈ¥ °ÑËùÓеÄСд×Öĸ·Å½øÈ¥ °Ñ0µ½9·Å½øÈ¥
- /********* Begin *********/
- for (int i = 0 ; i < 26 ; i++){
- pardStore[i] = (char)('A' + i );
- pardStore[26+i] = (char)('a'+i);
- }
- for(int i = 0 ; i <10 ; i ++ ){
- pardStore[52+i] = (char)('0'+i);
- }
- /********* End *********/
- // ·Ö±ðÒÔ1¡¢2¡¢3×÷ΪÖÖ×ÓÊý Éú³É6λËæ»úÃÜÂë
- Scanner sc = new Scanner(System.in);
- int seed = sc.nextInt();
- /********* Begin *********/
- Random random = new Random(seed);
- String str = "";
- int [] arr = random.ints(6,0,62).toArray();
- for( int i = 0 ; i < 6 ; i ++ ){
- str += pardStore[arr[i]];
-
- }
- System.out.println(str);
- /********* End *********/
- }
- }

第5关 Date类和SimpleDateFormat类的用法
- package case5;
-
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Scanner;
-
- public class DateTest {
- public static void main(String[] args) throws ParseException {
- // ¼üÅ̼ÈëÄãµÄ³öÉúÄêÔÂÈÕ ¸ñʽΪyyyy-MM-dd
- // °Ñ¸Ã×Ö·û´®×ª»»ÎªÒ»¸öÈÕÆÚ
- // ͨ¹ý¸ÃÈÕÆڵõ½Ò»¸öºÁÃëÖµ
- // »ñÈ¡2020Äê10ÔÂ1ÈյĺÁÃëÖµ
- // Á½ÕßÏë¼õµÃµ½Ò»¸öºÁÃëÖµ
- // °Ñ¸ÃºÁÃëֵת»»ÎªÌì ´òÓ¡Êä³ö
- Scanner sc = new Scanner(System.in);
- String line = sc.nextLine();
- /********* Begin *********/
- SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
- Date d1 = sf.parse(line);
- Date d2 = sf.parse("2020-10-01");
- long diff = d2.getTime()-d1.getTime();
- diff = diff/86400000;
- System.out.println("你的出生日期距离2020年10月1日:"+diff+"天");
-
-
-
- /********* End *********/
- }
- }

第6关 Math类
- package case6;
-
- import java.util.Scanner;
- import java.lang.Math;
-
- public class MathTest {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int a1 = sc.nextInt();
- int a2 = sc.nextInt();
- int a3 = sc.nextInt();
- int a4 = sc.nextInt();
- double a5 = sc.nextDouble();
- double a6 = sc.nextDouble();
- double a7 = sc.nextDouble();
- double a8 = sc.nextDouble();
- double a9 = sc.nextDouble();
- double a10 = sc.nextDouble();
- double a11 = sc.nextDouble();
- /********* Begin *********/
- System.out.println(Math.sqrt(a1));
- System.out.println(Math.cbrt(a2));
- System.out.println(Math.pow(a3,a4));
- System.out.println(Math.max(a5,a6));
- System.out.println(Math.min(a5,a6));
- System.out.println(Math.abs(a7));
- System.out.println(Math.ceil(a8));
- System.out.println(Math.floor(a9));
- System.out.println(Math.rint(a10));
- System.out.println(Math.round(a11));
- /********* End *********/
- }
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。