day08_basicclass

本文介绍了Java中的System类,包括获取当前时间、启动垃圾回收和结束JVM的方法,以及标准输入输出流。同时讲解了Scanner类用于键盘输入的操作,如nextInt和nextLine。接着,概述了Math类提供的数学运算,如绝对值、幂运算和随机数生成。最后,讨论了String的构造方法和常用方法,如substring和indexOf。
摘要由CSDN通过智能技术生成

1 System

     * System类:操作系统
	 * 方法:
	 *    1: static long currentTimeMillis() ;获取当前时间的毫秒值::当前时间距离历元(1970-1-1)之间的毫秒值
	 *    2: static void gc();  启动垃圾回收器对象
	 *    3static void exit(int status) ;结束jvm的运行::参数是0 正常结束 参数是非0 异常结束
	 *    
	 * 属性:
	 *    static PrintStream err :错误输出流:输出内容到控制台console
	 *    static PrintStream out :标准输出流:输出内容到控制台console
	 *    
	 *    static InputStream in  :标准输入流
	 *    
	 * Scanner类:监控器类
	 * 构造方法: Scanner(InputStream in) 
	 * 普通方法: int nextInt();
	 *           String next();
	 *           String nextLine(); 
	 *           
	 * 阻塞方法:当程序执行到此方法时  程序处于等待状态 等待系统执行某个操作 只有当进行此操作了 程序才继续往下执行
public class Demo01SystemScanner {
	public static void main(String[] args) {
		   /*
		   System.out.println(111);
		   Student.t.hehe(1);
		   //System.exit(0);
		   long l=System.currentTimeMillis();
		   System.out.println("获取当前时间:"+l);
		   System.out.println("获取当前时间:"+l/1000/3600/24/365);//52
		   
		   System.out.println(222);
		   */
		
		    /*
		    //两个不同的输出流 再使用同一个console
		    //out输出流和err输出流不是同一个线程:
		   System.out.println("out 11");
		   System.err.println("err 11");
		   System.out.println("out 12");
		   System.err.println("err 12");
		   System.out.println("out 13");
		   System.err.println("err 13");
		   System.out.println("out 14");
		   System.err.println("err 14");
		   //System.out.println(1/0);
		    * */
           
		  //获取键盘输入的信息:
		  //创建一个scanner对象:流监控器对象 指定被监控的流是系统输入流
		  Scanner sr=new Scanner(System.in);
		  /*
		  //获取一个整数
		  System.out.println("请在键盘输入一个整数:");
		  int n=sr.nextInt();//InputMismatchException
		  System.out.println("n="+n);
		  */
		  System.out.println("请在键盘输入一个字符串:");
		  String str1=sr.next();//读取的是空格/提交
		  System.out.println("str1="+str1);
		  
		  System.out.println("请在键盘输入一行字符串:");
		  String str2=sr.nextLine();//读取的是换行
		  System.out.println("str2="+str2);//""
		  //scanner的next和nextLine方法不能混合使用
	}
}
class Teacher{
	void hehe(int a) {System.out.println("老师额和和:::");}
}
class Student{
	static Teacher t=new Teacher();
}
  • 练习:
猜当前时间的后两位:78   记录其猜对使用的次数
//猜字游戏
public static int caiZi() {
    //获取当前时间的毫秒值 的后2位
    long l=System.currentTimeMillis();
    int n=(int)(l%100);
    System.out.println("当前时间::"+l);

    //定义变量记录次数
    int ciShu=0;
    //创建scanner监控器对象
    Scanner sr=new Scanner(System.in);
    int max=99,min=0;
    while(true) {
        System.out.println("请输入您的猜测:(一个整数"+min+"-"+max+")");
        int m=sr.nextInt();
        ciShu++;
        if(m<n) {
            min=m;
            System.out.println("太小了!");
        }else if(m>n) {
            max=m;
            System.out.println("太大了!");
        }else {
            System.out.println("您猜对"+n+"使用了:"+ciShu+"次!");
            return ciShu;
        }
    }
}

2 Math

     * Math:数学运算相关的方法:
	 * 属性:常量值:
	 * 		E:自然对数
	 * 		PI: 圆周率
	 * 方法:
	 *     1 绝对值
	 *       static double abs(double a)  
	 *     2 幂运算
	 *       static double cbrt(double a)  
	 *       static double sqrt(double a)
	 *       static double pow(double a, double b) 
	 *     3 近似值
	 *        static double ceil(double a) 
	 *        static double floor(double a)
	 *        static double rint(double a)
	 *        static long round(double a) 
	 *     4 随机
	 *        static double random()             
package day09_basic_class;

public class Demo02Math {
	public static void main(String[] args) {
		 System.out.println("Math.PI="+Math.PI);
		 System.out.println("Math.E="+Math.E);
		 
		 //求绝对值
		 System.out.println(Math.abs(-1));
		 
		 //幂运算
		 for (int i = 1; i < 10; i++) {
			 System.out.println("Math.cbrt("+i+")="+Math.cbrt(i));
			 System.out.println("Math.pow("+i+",1.0/3)="+Math.pow(i,1.0/3));
			 System.out.println("Math.sqrt("+i+")="+Math.sqrt(i));
			 System.out.println("Math.pow("+i+",0.5)="+Math.pow(i,0.5));
			 System.out.println("Math.pow("+i+",2)="+Math.pow(i,2));
		 }
		 //近似值
		 for (int n = 100; n <200; n++) {
			  double m=n/10.0;
			  System.out.println("Math.ceil("+m+"):获取大于等于m的最小整数:"+Math.ceil(m));
			  System.out.println("Math.floor("+m+"):获取小于等于m的最大整数:"+Math.floor(m));
			  System.out.println("Math.rint("+m+"):获取四舍六入五取偶:"+Math.rint(m));
			  System.out.println("Math.round("+m+"):获取四舍五入:"+Math.round(m));
		 }
		 //随机[0,1)
		 for (int i = 0; i <10; i++) {
			System.out.println(i+"::::"+Math.random());
		 }
		 //随机一个0-10的整数:
		 for (int i = 0; i <10; i++) {
			    //[0,11)
				//System.out.println(i+"::::"+Math.random()*11);
				//再强制转化为int::[0,10]
				System.out.println(i+"::::"+(int)(Math.random()*11));
		 }
		 //随机一个10-20的整数:
		 for (int i = 0; i <10; i++) {
			    //[0,11)
				//System.out.println(i+"::::"+Math.random()*11);
				//再强制转化为int::[0,10]
				//System.out.println(i+"::::"+(int)(Math.random()*11));
			    //起始值由0变成10
			     System.out.println(i+"::::"+(int)(Math.random()*11+10));
		 }
		 //随机一个a-b的整数
		 int a=4,b=13;
		 for (int i = 0; i <100; i++) {
			     System.out.println(i+"::::::"+(int)(Math.random()*(b-a+1)+a));
		 }
		 //随机整数a-b: 乘以范围 加上起始值 强制转化为int
	}
}

3 String

构造方法

     * String:字符串
	 * 注意1final类 不能被继承
	 * 注意2:创建String对象有两种方式:方式1new String("") 方式2""
	 * 注意3:字符串对象是常量:字符串对象一旦创建 字符序列不能更改
	 * 
	 * 构造方法:
	 *    1 无参数:
	 *         String() :创建一个空字符序列的字符串对象:类似于 ""
	 *    2 参数是数组:byte char int
	 *       String(byte[] bytes) 
	 *       String(byte[] bytes, String charsetName) 
	 *       String(byte[] bytes, int offset, int length) 
	 *       String(byte[] bytes, int offset, int length, String charsetName) 
	 *       
	 *       String(char[] value) 
	 *       String(char[] value, int offset, int count) 
	 *       
	 *       String(int[] codePoints, int offset, int count) 
	 *   3 参数是字符串对象:
	 *       String(String original) :获取一个和参数字符串对象相同序列的副本对象  

普通方法

     * 普通方法:
	 * 获取:
	 *    1.1 char charAt(int index)  :根据下标获取字符
	 *    1.2 byte[] getBytes()       :获取当前字符串对应的字节数组::默认编码集
	 *        byte[] getBytes(String charsetName)   :获取当前字符串对应的字节数组::指定编码集
	 *    1.3 int indexOf(int ch) :获取 参数字符第一次出现的位置:如果没有找到返回-1
	 *        int indexOf(int ch, int fromIndex) :从fromIndex位置处开始找  获取 参数字符第一次出现的位置:如果没有找到返回-1
	 *        int indexOf(String str) :获取 参数字串第一次出现的位置:如果没有找到返回-1
	 *        int indexOf(String str, int fromIndex)
	 *        int lastIndexOf(int ch) :倒着找 :获取 参数字串第一次出现的位置:如果没有找到返回-1 
	 *        int lastIndexOf(int ch, int fromIndex) 
	 *        int lastIndexOf(String str) 
	 *        int lastIndexOf(String str, int fromIndex)         
	 *    1.4 int length() : 获取字符个数 
	 *    1.5 String[] split(String regex)  :字符串切割
	 *    1.6 String substring(int beginIndex)  :截取子串:从beginIndex开始到结尾
	 *        String substring(int beginIndex, int endIndex)   :截取子串:从beginIndex开始到endIndex-1处结束
	 *    1.7 char[] toCharArray()  : 获取当前字符串对应的字符数组  
	 * 判断:
	 *    2.1 int compareTo(String anotherString)  :   拿当前字符串和参数字符串逐个字符按编码表作比较::相同返回0 当前字符串大返回正数 否则返回负数    
	 *        int compareToIgnoreCase(String str)  : 不区分大小写
	 *    2.2  boolean contains(CharSequence s)    :判断当前字符串是否包含参数字符串::必须是连续
	 *    2.3  boolean endsWith(String suffix)     : 判断当前字符串是否以参数字符串结尾 
	 *         boolean startsWith(String prefix)   : 判断当前字符串是否以参数字符串开头
	 *    2.4  boolean equals(Object anObject)     : 判断当前字符串对象和参数字符串对象的字符序列是否相同
	 *         boolean equalsIgnoreCase(String anotherString):不区分大小写 
	 *    2.5  boolean isEmpty()  :判断是否为空字符串 ::""
	 * 转换:
	 *    3.1 String concat(String str)  :字符串拼接  等价于+
	 *    3.2 String replace(char oldChar, char newChar):使用 newChar替换所有的 oldChar
	 *        String replace(String target, String replacement)  
	 *    3.3 String trim()  : 去除两边的空格
	 *    3.4 String toLowerCase()  : 所有字符小写
	 *        String toUpperCase()  : 所有字符大写 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解释下列代码#include <iostream> #include <string> using namespace std; class Date { public: Date(int year, int month, int day) : year_(year), month_(month), day_(day) {}; friend ostream& operator<<(ostream& os, const Date& date) { os << date.year_ << "-" << date.month_ << "-" << date.day_; return os; } private: int year_; int month_; int day_; }; class Person { public: Person(const string& name, const Date& birthday) : name_(name), birthday_(birthday) {}; virtual ~Person() {}; protected: string name_; Date birthday_; }; class Teacher : public Person { public: Teacher(const string& id, const string& name, const Date& birthday, const string& major, const string& affiliation) : Person(name, birthday), id_(id), major_(major), affiliation_(affiliation) {}; void PrintInfo() const { cout << "Name: " << name_ << endl; cout << "Birthday: " << birthday_ << endl; cout << "Teacher ID: " << id_ << endl; cout << "Teaching Major: " << major_ << endl; cout << "Affiliation: " << affiliation_ << endl; cout << "The basic information: " << id_ << ' ' << name_ << ' ' << birthday_ << ' ' << major_ << ' ' << affiliation_ << endl; } private: string id_; string major_; string affiliation_; }; class Student : public Person { public: Student(const string& id, const string& name, int score, const Date& birthday) : Person(name, birthday), id_(id), score_(score) {}; void PrintInfo() const { cout << "Name: " << name_ << endl; cout << "Birthday: " << birthday_ << endl; cout << "Student ID: " << id_ << endl; cout << "Student Score: " << score_ << endl; cout << "The basic information: " << id_ << ' ' << name_ << ' ' << score_ << endl; cout << birthday_ << endl; } private: string id_; int score_; }; int main() { Date student_birthday(1976, 5, 27); //修改学生出生日期 Student student("2023007", "kxiong", 92, student_birthday); student.PrintInfo(); Date teacher_birthday(1998, 1, 7); //修改教师出生日期 Teacher teacher("20210058", "xsong", teacher_birthday, "Computer Science", "CTBu"); teacher.PrintInfo(); return 0; }
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值