2017/11/6~2017/11/12学习Java总结
1.方法调用
public class ????{
public static void main(String[] args){
xxxx;
}
static public void xxxx{
}
}
2.判断输入是否是字母或数字
public static void main(String[] args) {
String str = "123456sdfse";
for(int i=0;i<str.length();i++){
System.out.println(str.charAt(i));
if(!Character.isDigit(str.charAt(i))){
System.out.println("这个字符不是数字字符串");
break;
}
}
}
3.ASCII值
信息在计算机上是用二进制表示的,这种表示法让人理解就很困难。因此计算机上都配有输入和输出设备,这些设备的主要目的就是,以一种人类可阅读的形式将信息在这些设备上显示出来供人阅读理解。为保证人类和设备,设备和计算机之间能进行正确的信息交换,人们编制的统一的信息交换代码,这就是ASCII码表,它的全称是“美国信息交换标准代码”。
public static void main(String[] args) {
String s = " ";
for(int i=0;i<s.length();i++){
System.out.println(+s.charAt(i));
}
}
4.string转int类型
String str;
int ??? = Integer.parseInt(str);
5.Scanner输入和BufferedReader输入
Scanner 一个可以使用正则表达式来分析基本类型和字符串的简单文本扫描器。
BufferedReader 从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。
Scanner in = new Scanner(System.in);
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
6.阶乘值
阶乘是基斯顿·卡曼(Christian Kramp,1760~1826)于 1808 年发明的运算符号,是数学术语。一个正整数的阶乘(factorial)是所有小于及等于该数的正整数的积,并且0的阶乘为1。自然数n的阶乘写作n!。1808年,基斯顿·卡曼引进这个表示法。亦即n!=1×2×3×...×n。阶乘亦可以递归方式定义:0!=1,n!=(n-1)!×n。
public static void main(String[] args) {
int sum = 0;
for(int i=1;i<=10;i++){
int x=1;
for(int j=1;j<=i;j++){
x=x*j;
}
sum=sum+x;
}
System.out.println("sum="+sum);
}
7.字符串的拼接,类型转换,重载
重载,简单说,就是函数或者方法有相同的名称,但是参数列表不相同的情形,这样的同名不同参数的函数或者方法之间,互相称之为重载函数或者方法。
public class ChongZai {
public static void main(String[] args){
String s1 = null,s2 = null,ss,si,sf;
int i1,i2;
float f1,f2;
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
try{
System.out.println("输入第一个字符串:");
s1 = strin.readLine();
System.out.println("输入第二个字符串:");
s2 = strin.readLine();
}catch(Exception e){
System.out.println(e.getMessage());
}
try{
i1 = Integer.parseInt(s1);
i2 = Integer.parseInt(s2);
f1 = Float.parseFloat(s1);
f2 = Float.parseFloat(s2);
ss = strAdd(s1,s2);
si = strAdd(i1,i2);
sf = strAdd(f1,f2);
System.out.println("输入的两个字符串相加结果为:"+ss);
System.out.println("输入的两个字符串转换为整数相加结果为:"+si);
System.out.println("输入的两个字符串转换为浮点数相加结果为:"+sf);
}catch(Exception e){
System.out.println(e.getMessage());
System.out.println("类型输入不符合要求。");
}
}
重载,类型转换
public static String strAdd(String str1,String str2){
return str1+str2;
}
public static String strAdd(int int1,int int2){
return String.valueOf(int1 + int2);
}
public static String strAdd(float float1,float float2){
return String.valueOf(float1+float2);
}
8.异常处理
异常处理,是编程语言或者=计算机硬件里的一种体制,用于处理软件或信息系统中出现的异常状况(即超出程序执行流程的某些特殊条件)。
try{
程序代码
}catch(异常类型1 异常的变量名1){
程序代码
}
9.求最大公约数和最小公倍数
最大公约数:指两个或多个整数共有约数中最大的一个
最小公倍数:指两个或多个整数公有的倍数,其中除0以外最小的一个公倍数
public static void main(String args[]){
Scanner s1=new Scanner(System.in);
System.out.println("请输入第一个正整数");
int m=s1.nextInt();
System.out.println("请输入第二个正整数");
int n=s1.nextInt();
if(m<n){
int temp=n;
n=m;
m=temp;
}
System.out.println("最大公约数是:"+caculate(m,n));
System.out.println("最小公被数是:"+m*n/caculate(m,n));
}
public static int caculate(int m,int n){
int temp ;
if(m%n==0){
temp=n;
;
}else{
temp=caculate(n,m%n);
}
return temp;
}
10.水仙花数
指一个n位数(n>=3),它的每位上的数字的n次幂之和等于它本身(例如:1^3+5^3+3^3=153)
public static void main(String[] args) {
System.out.println("水仙花数是:");
for(int i=100;i<1000;i++){
int a = i/100;
int b = i/10%10;
int c = i%10;
if(i == a*a*a+b*b*b+c*c*c)
System.out.println(+i);
}
}
11.输入字符串,计算每种类型的个数
public static void main(String[] args) {
System.out.println("请输入字符:");
BufferedReader buff= new BufferedReader(new InputStreamReader(System.in));
String str = "";
try {
str = buff.readLine();
} catch (IOException e) {
e.printStackTrace();
}
int shuzi = 0;
int zimu = 0;
int kongge = 0;
int qita = 0;
for(int i=0;i<str.length();i++){
char c = str.charAt(i);
if(c>='0'&&c<='9'){
shuzi++;
}
else if((c>='a'&&c<='z')||(c>='A'&&c<='Z')){
zimu++;
}
else if(c==' '){
kongge++;
}
else{
qita++;
}
}
System.out.println("字符中数字的个数为:"+shuzi);
System.out.println("字符中字母的个数为:"+zimu);
System.out.println("字符中空格的个数为:"+kongge);
System.out.println("字符中其它字符的个数为:"+qita);
}
12.FileInputStream类 读取一个文档
FileInputStream是Java语言中抽象类InputStream用来具体实现类的创建对象。FileInputStream可以从文件系统中的某个文件中获得输入字节,获取的文件可用性取决于主机环境。
FileInputStream的构造方法需要指定文件的来源,通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
public static void main(String[] args) {
byte[] buf = new byte[2056];
try{
FileInputStream fil = new FileInputStream("所需读取文件的位置+文件名");
int bytes = fil.read(buf,0,2056);
String str = new String(buf,0,bytes);
System.out.println(str);
}catch (Exception e) {
e.printStackTrace();
}
}
13.FileOutputStream类 写入一个文档
FileOutputStream,意为文件输出流,是用于将数据写入File或 FileDescriptor的输出流。
public static void main(String[] args) {
byte[] buf = new byte[2056];
try{
FileInputStream fil = new FileInputStream("所需读取的文件的位置+文件名");
int bytes = fil.read(buf,0,2056);
String str = new String(buf,0,bytes);
FileOutputStream fil2 = new FileOutputStream("需要写入的文件的位置+文件名");
fil2.write(str.getBytes());
}catch (Exception e) {
e.printStackTrace();
}
}
14.素数
质数(prime number)又称素数,有无限个。质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数。
求101-200之间的素数
public static void main(String[] args) {
int x=0;
Vector v = new Vector();//集合,用来装所有的素数
for(int i=101;i<=200;i++)
{
boolean b=true;//标签为b为真
for(int j=2;j<i;j++)
{
if(i%j==0)//求余数是否为0
{
b = false;//如果为0, 将标签设置为false
break; //可以整除就跳出这个循环
}
}
if(b==true)//
{
v.add(i);//将符合要求的i加到集合里
x++; // 素数个数加1
}
}
System.out.println("100到200中间有 "+x+" 个素数");
System.out.println("素数为:/n"+v);
}
15.定义一个表示学生信息的类Stdenct,要求如下:
(1)类Stenduct的成员变量:
no表示学号;name表示姓名;sex表示性别;age表示年龄;java表示java课程成绩。
(2)类Student带参数的构造方法:
在构造方法中通过形参完成对成员变量的赋值操作。
(3)类Student的方法成员:
getNo():获得学号;
getName():获得姓名;
getSex():获得性别;
getAge():获得年龄;
getJava():获得java课程成绩;
(4)根据类Student的定义,创建五个该类的对象,输出每个学生的信息,计算并输出这五个学生Java语言成绩的平均值,以及计算并输出他们Java语言成绩的最大值和最小值。
class Student{
private String no;
private String name;
private String sex;
private int age;
private double java;
private double userName;
public Student(String no, String name, String sex, int age, int java) {
this.no=no;
this.name=name;
this.sex=sex;
this.age=age;
this.java=java;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getJava() {
return java;
}
public void setJava(double java) {
this.java = java;
}
public double getUserName() {
return userName;
}
public void setUserName(double userName) {
this.userName = userName;
}
void show(){
System.out.println("学号"+getNo());
System.out.println("姓名"+getName());
System.out.println("性别"+getSex());
System.out.println("年龄"+getAge());
System.out.println("Java成绩"+getJava());
}
public static void main(String[] args) {
double avrage,K1,K2,K3,K4,K5;
Student A1=new Student("12345","赵一","男",20,90);
Student A2=new Student("12346","郭二","女",21,89);
Student A3=new Student("12347","张三","男",22,88);
Student A4=new Student("12348","李四","女",23,87);
Student A5=new Student("12349","王五","男",24,86);
A1.show();
System.out.println();
A2.show();
System.out.println();
A3.show();
System.out.println();
A4.show();
System.out.println();
A5.show();
System.out.println();
K1=A1.getJava();
K2=A2.getJava();
K3=A3.getJava();
K4=A4.getJava();
K5=A5.getJava();
avrage=(K1+K2+K3+K4+K5)/5;
System.out.println("Java平均成绩:"+avrage);
double a[]={K1,K2,K3,K4,K5};
double max=0,min=0,ck;
for(int i=0;i<4;i++){
max=K1;
if(max<a[i+1]){
ck=max;
max=a[i+1];
a[i+1]=ck;
}
}
System.out.println("Java的最高分为:"+max);
for(int i=0;i<4;i++){
min=K1;
if(min>a[i+1]){
ck=min;
min=a[i+1];
a[i+1]=ck;
}
}
System.out.println("Java的最低分为:"+min);
}
}
16.(1)编写一个圆类Circle,该类拥有:
①一个成员变量
Radius(私有,浮点型); // 存放圆的半径;
②两个构造方法
Circle() // 将半径设为0
Circle(double r ) //创建Circle对象时将半径初始化为r
③ 三个成员方法
double getArea( ) //获取圆的面积
double getPerimeter( ) //获取圆的周长
void show( ) //将圆的半径、周长、面积输出到屏幕
class Circle{
private double radius;
private double area;
private double perimter;
public Circle(double radius){
this.radius=radius;
}
public Circle(){
this.radius=0;
}
public double getRadius() {
return radius;
}
public double getArea() {
return area;
}
public double getPerimter() {
return perimter;
}
public void setRadius(double radius) {
this.radius = radius;
}
public void setArea(double area) {
this.area = area;
}
public void setPerimter(double perimter) {
this.perimter = perimter;
}
void show(){
System.out.println("该圆的半径为:"+getRadius());
System.out.println("该圆的面积为:"+getArea());
System.out.println("该圆的周长为:"+getPerimter());
}
public void compute(){
this.area = this.radius * this.radius * 3.14;
this.perimter = this.radius * 2 * 3.14;
}
public static void main(String[] args) {
Circle circle = new Circle(5);
circle.compute();
circle.show(); 半径为5,输出面积和周长。
Circle circle1 = new Circle(10);
circle1.compute();
circle1.getArea();
System.out.println(circle1.getArea()); 半径为10,输出面积。
circle1.setRadius(15);
circle1.compute();
System.out.println(circle1.getPerimter()); set 修改半径为15,输出周长。
}
}
!!! 继承 extends
继承是面向对象最显著的一个特性。继承是从已有的类中派生出新的类,新的类能吸收已有类的数据属性和行为,并能扩展新的能力。Java继承是使用已存在的类的定义作为基础建立新类的技术,新类的定义可以增加新的数据或新的功能,也可以用父类的功能,但不能选择性地继承父类。
(2)编写一个圆柱体类Cylinder,它继承于上面的Circle类。还拥有:
①一个成员变量
double hight(私有,浮点型); // 圆柱体的高;
②构造方法
Cylinder (double r, double h) //创建Circle对象时将半径初始化为r
③ 成员方法
double getVolume() //获取圆柱体的体积
void showVolume() //将圆柱体的体积输出到屏幕
编写应用程序,创建类的对象,分别设置圆的半径、圆柱体的高,计算并分别显示圆半径、圆面积、圆周长,圆柱体的体积。
public class Cylinder extends Circle{
private double hight;
private double volume;
public Cylinder(){
this.hight=0;
}
public Cylinder(double hight,double radius){
super(radius);
this.hight=hight;
}
public void compute(){
super.compute();
volume = getArea()*hight;
}
public double getVolume(){
return volume;
}
public void show(){
super.show();
System.out.println("圆柱的高为:"+hight);
System.out.println("圆柱的体积为:"+getVolume());
}
public static void main(String[] args) {
Cylinder cylinder = new Cylinder(1, 78);
cylinder.compute();
cylinder.show(); 高为1,半径为78时,圆柱的体积。
}
}
2017/11/6~2017/11/12学习Java总结相关推荐
- 2017年8月11日草原天路沽源太仆寺旗3日游
2017年8月11日下午,驾车前往张家口,夜宿张家口.费用:住宿198+高速费70 2017年8月12日,早起前往草原天路,在高速野狐岭出口堵车好久,过了野狐岭后堵车情况好转.下午从桦皮岭开出后左转走 ...
- 分享Silverlight/WPF/Windows Phone/HTML5一周学习导读(11月28日-12月4日)
分享Silverlight/WPF/Windows Phone/HTML5一周学习导读(11月28日-12月4日) 本周Silverlight学习资源更新 Silverlight HttpUtil 封 ...
- 工作总结 2017年8月11日 转换为datatime
string strr = "2017年8月11日"; Console.WriteLine((Convert.ToDateTime(strr)).ToString("yy ...
- 把chrome请出系统盘,还让它自动更新(2017年8月11日有更新)
2017年8月11日更新 这两天发现,要想把chrome请出系统盘并让它自动更新,有比最初回答(在下面)更简单的办法. 办法就是一句话:在安装前,用mklink把两个连接做好.链接做好后,再安装chr ...
- 【语文考试】古诗文阅读常见考点答题角度整理——2017年3月11日
[诗歌阅读] <1>鉴赏诗歌的形象 1.人物形象 爱恨情长:献身边塞.反对征伐:豪放洒脱.傲岸不羁:矢志报国.慷慨愤世:心怀天下.忧国忧民 寄情山水.归隐田园:友人送别.思念故乡:怀才不遇 ...
- 盘点 | 2017 年关于 Python 的 12 件大事
点击上方"CSDN",选择"置顶公众号" 关键时刻,第一时间送达! 作者 | 汤哥在北京 [编者按]Python 被称为是最接近 AI 的语言.几乎所有的深度学 ...
- 2017年 第8届 蓝桥杯 Java B组 省赛真题详解及总结
蓝桥杯 Java B组 省赛真题详解及小结汇总[2013年(第4届)~2020年(第11届)] 注意:部分代码及程序 源自 蓝桥杯 官网视频(历年真题解析) 郑未老师. 2013年 第04届 蓝桥杯 ...
- 12.10 Nginx访问日志 12.11 Nginx日志切割 12.12 静态文件不记录日志和过期时间
- 12.10 Nginx访问日志 - 12.11 Nginx日志切割 - 12.12 静态文件不记录日志和过期时间# 12.10 Nginx访问日志 - 日志的格式- vim /usr/local/ ...
- U3D笔记11:47 2016/11/30-15:15 2016/12/19
U3D笔记11:47 2016/11/30-15:15 2016/12/19 技术BLOG:http://www.unity.5helpyou.com/2373.html#comment-43108 ...
最新文章
- 华为计算产品备件查询助手
- 传央行闭门会议将出台两项举措 等同降准150基点
- Python基础day05 作业解析【10道 函数作业题】
- mpvue 中控制swiper滑动,禁止滑动,只允许左滑动,不允许右滑
- 使用GDB进行调试 -- 1 应用场景
- IT营大地老师2017最新node.js视频教程avi版本(课件+源码+视频)
- 收文和发文管理流程分析
- mysql合并两个表_MYSQL如何合并两个表
- area 估算函数(simpson)
- 耗时5小时用纯HTML和CSS写成的博学谷
- jsp中List,ArrayList的使用方法
- MySQL高可用方案MGR+consul组合测试
- 让代码飞起来,可以很清晰的分清代码 彩虹括号
- 一秒给黑白照片上色、模糊图片变清晰
- 数组的push()、pop()、shift()和unshift()方法
- python列表中的元素可以是不同类型_Python列表中所有元素必须为相同类型的数据。...
- Linux 磁盘分区及文件系统挂载
- 南阳理工学院计算机应用基础答案,南阳理工学院国家省校级精品课程(精品资源共享课程)一览表.doc...
- MySQL 5.7调优参数详解
- 统计 | 统计功效 | R语言