99

0x00 前言

目前在学习JAVA核心技术卷1基础知识原版第10版+极客时间的JAVA课程。
由于极客时间讲的有点点跟不上,所以过来啃书,本篇是第三章,写的代码及知识点都会写在本篇博客作为记录。

0x01 JAVA程序书写格式

java程序固定格式

public class test {
public static void main(String[] args){
代码块
}
}

固定的格式如上,main是定义一个程序的入口,public class test,这里test是文件名,文件名必须是test不然会报错,是对应的,大小写也必须一致

0x02 print和println的区别

下面是测试代码。。。不太懂英文,写不出很骚气的英文名称

public class ceshi {
public static void main(String[] args){
System.out.print("hello");//输出无换行
//System.out.println("hello");//输出有换行
}
}

其实感觉是没啥差别的,就是print没有换行,println有换行。

0x03 数据类型

java中共有8种数据类型

整数型

int 占用4字节 值域是-21474836482147483647
short 占用2个字节 值域是-32768
32767
long 占用8个字节 值域是-92233720368547748089223372036854774807
byte 占用1个字节 值域是-128
127
java中整数默认是int类型,如果是长整型,需要在值后面加上小写的l或者大写L,推荐使用大写的L,容易区分

浮点型

float 占用4个字节 有精度 值域是±340282346638528859811704183484516925440
double 占用8个字节 精度是float的一倍
java中浮点型默认是的double类型,如需要使用float需在值后面加上F或者f表示使用float输出

char类型

char原本用来表示单个字符,但是可以用来表示Unicode,另外有一些Unicode需要使用两个char值
‘A’表示编码值65
“A”表示包含字符串A,两个并不相等
char的值可以用十六进制来表示,其范围从\u0000-\uffff
例:\u2122表示注册符号™,\u03c0表示希腊字母Π
除了转义字符\u,还有一些其他的用于表示特殊字符的转义序列,所有这些转义序列都可以出现在加引号的字符字面量或字符串中。
例:”\u2122”或”Hello\n”,转义序列\u还可以出现下加引号的字符常量或者字符串之外,其他的转义序列不可以。

public class lizi {
public static void main(String\u005B\u005D args) {
代码块
}
}

符合语法规则,\u005B代表[ \u005D代表],就组成了String[]

特殊转义字符
\b 退格 \u0008
\t 制表 \u0009
\n 换行 \u000a
\r 回车 \u000b
" 双引号 \u0022
' 单引号 \u0027
\ 反斜杠 \u005c

布尔型(boolean)

布尔值分为true和flase 占用四个字节
在其他编程语言中 0是代表flase,非0代表true,java中不是这样,整数型和布尔型无法相互转换

java中各数据类型所占用的字节空间

public class BigNumber {
public static void main(String[] args){
long bigvar = 99999999999L; //超长整数型 long占用8个byte,值域是-9223372036854774808~9223372036854774807

System.out.println(bigvar);

byte bytevar = 32; //byte占用1个byte,值域是 -128~127
System.out.println(bytevar);

short shortvar = 30000;//short占用2个byte,值域是-32768~32767
System.out.println(shortvar);

int intvar = 5546123; //int占用4个byte,值域是-2147483648~2147483647。Java中整数缺省(默认)是int类型
System.out.println(intvar);

//浮点(小数)类型
float floatvar = 100.10555454f;// float–有精度,值域复杂±340282346638528859811704183484516925440
System.out.println(floatvar);
double doublevar = 100.10555454;//double–精度是float的一倍,占用8个byte。Java中小数缺省(默认)是double类型
System.out.println(doublevar);

//布尔和字符数据类型
boolean booleanvar = true;//boolean占用4个byte,值域是true,false
System.out.println(booleanvar);
boolean fooleanvar = false;
System.out.println(fooleanvar);

char charvar = 'a';
System.out.println(charvar);//char占用2个byte,值域是所有字符(最多65535个)
}
}

0x04 变量

声明变量:变量声明需要以 变量类型 变量名称 ; 然后以分号结尾,变量名不能以数字开头。
一般都是符号、字母开头,当然中间或者结尾可以是数字,列如:test1、te2st,这样也是可以,但是建议不要这样声明变量。。。

变量初始化

变量初始化其实就是赋一个值给变量,这样就完成了变量的初始化,在实际写程序中其实也是推荐在声明变量的时候直接初始化,如直接声明在后面初始化,可能会产生漏洞。。。。此处就不讲了(其实我自己也不是太懂。。)

public class ceshi {
public static void main(String[] args){
int $te2st = 10;//变量初始化
System.out.println($te2st);
}
}
最后输出10

常量

常量由final来定义,常量表示变量的固定值,以后无法修改常量值,只能被设定一次。常量名称一般使用全大写。

public class changliang {
public static void main(String[] args){
final double CM_PER_INCH = 2.54;//定义常量
double paperWidth = 8.5;
double paperHergit = 11;
System.out.println("Paper size in crntimenters:" + "宽:"+
paperWidth * CM_PER_INCH + " by " + "高:" + paperHergit * CM_PER_INCH);

}
}

类常量

一般需要在一个类的多个方法中使用,此时需要用到类常量,类常量定义在main方法外部,如果使用public定义说明类中的所有方法都可使用此常量

public class changliang {
//定义类常量,可以在类的多个方法中使用
public static final double CM_PER_INCH = 2.54;
public static void main(String[] args){
double paperWidth = 8.5;
double paperHergit = 11;
System.out.println("Paper size in crntimenters:" + "宽:"+
paperWidth * CM_PER_INCH + " by " + "高:" + paperHergit * CM_PER_INCH);

}
}

0x05 运算符

java中+、-、*、/代表加减乘除运算,当参与/运算的两个数都为整数时,代表整数除法,否则是浮点除法。
整数求余操作(也可以说是取模)用%来运算

public class MathCalc {
public static void main(String[] args){
System.out.println(1+2);
System.out.println(1-4);
System.out.println(3*2);
System.out.println(5/2.5);
System.out.println(1+2);

}
}

数学函数与常量

计算一个数值的平方根,使用sqrt方法,这里注意是java.lang.Math.sqrt()

public class Math {
public static void main(String[] args){
double x = 4;
double y = java.lang.Math.sqrt(x);//计算平方根
System.out.println(y);
}
}

幂次方计算,java中没有幂计算,需要借助Math中的pow方法

public class Math {
public static void main(String[] args){
double x = 4;
double a = 5;
double c = java.lang.Math.pow(x,a);
System.out.println(c);
}
}

floorMod方法计算后值为0~11(若是负除数会得到一个负数结果,不过这种情况很少发生)
Math常用的三角函数

Math.sin
Math.cos
Math.tan
Math.atan
Math.atan2

指数函数以及反函数和自然对数以及以10为底数的对数:

Math.exp
Math.log
Math.log10

表示Π和e常量的近似值:

Math.E
Math.PI

导入java的数学库,不需要在数学方法前面加上一堆前缀

improt static java.lang.Math.*;
public class Math {
public static void main(String[] args){
double x = 4;
double a = 5;
double y = sqrt(x);//计算平方根
System.out.println(y);

double c = pow(x,a);
System.out.println(c);
}
}

数值类型之间的转换

整数转换为浮点型,会丢失部分精度

public class zhuanhuan {
public static void main(String[] agrs){
int n = 123456789;
float f = n;//将整数型n转换为float类型
System.out.println(f);//精度丢失
}
}
print:1.23456792E8

下图中,实箭头代表无信息丢失,虚箭头代表存在精度丢失的转换

整数型在转换时无精度丢失,整数转换浮点型存在精度丢失

强制类型转换

在需要进行强制转换数值前面加上(数据类型)

import static java.lang.Math.*;
public class qzzhuanhuan {
public static void main(String[] args) {
double x = 12.432432;
int nx = (int) x;
System.out.println(nx);
}
}
print: 12

结合赋值和运算符

public class ceshi {
public static void main(String[] args){
int x = 10;
x += 10;
System.out.println(x);
}
}
print: 20

自增与自减运算

public class zhuanhuan {
public static void main(String[] agrs){
int a = 10;
int b = 2;
b = 2 * ++b;//b=2,先*2=4,自增=6,自增是增加原来的数,也就是+2
a = 2 * a++;//a=10,运算结束后在自增,运算结束后=20,已经得出了20,之后在自增,因为程序就运行一次,所以自增结果未输出,所以得20
System.out.println(a);
System.out.println(b);
}
}
print: a=20,b=6

关系和boolean运算

计算两个数是否相等或不相等

public class boolean1 {
public static void main(String[] args){
int a = 3;
int b = 10;
//是否相等
System.out.println(a==b);//false
//是否不相等
System.out.println(a!=b);//true
//a大于b
System.out.println(a>b);//false
//a小于b
System.out.println(a<b);//true
}

位运算符

处理整数类型时,可以直接对组成整数类型的各个位完成操作,这意味可以使用掩码技术得到整数中的各个位,位运算符包括:
按位并:&(and) 两个二进制值比较如果有一位是false则为false,如果两个都为true则为true。0为false,1为ture

例子:两个值按照一位一位比较
1111
1010
结果为1010

1111
1111
结果为1111

按位或:|(or) 两个二进制值比较,如果有一个为真,则结果为真,如果两个为假则为假

例子:
1111
0000
结果为1111

1010
0101
结果为1111

按位异或:^(xor) 两个二进制比较,如果两个值不一样为真,两个值一样为false

例子:
1010
1010
结果为0000

1010
0101
结果为1111

按位取反:~(not) 按照位取反,如果为1则得0,如果为0则得1。

例子:
1001
结果为0110

小例子

public class byteyunsuan {
public static void main(String[] args){
//251
int a = 0xFB; //二进制1111 1011
//244
int b = 0xF4; //二进制1111 0100
//255
int c = 0xFF; //二进制1111 1111
System.out.println(a & b); //240
System.out.println(a | b); //255
System.out.println(a ^ b); //15
System.out.println(~c); //-256
}
}

复盘: a and b 两个二进制值比较如果有一位是false则为false,如果两个都为true则为true。0为false,1为ture
a = 1111 1011
b = 1111 0100
结果: 1111 0000
11110000 == 240

a or b 两个二进制值比较,如果有一个为真,则结果为真,如果两个都为假则为假
a = 1111 1011
b = 1111 0100
结果: 1111 1111
11111111 == 255

a xor b 两个二进制比较,如果两个值不一样为真,两个值一样为false
a = 1111 1011
b = 1111 0100
结果: 0000 1111
00001111 == 15

~c 按位取反
c = 1111 1111
结果: 0000 0000
00000000 == -256
按理来说值应该为0,但是按位取反,255取反后为-256,0为正整数,从-1开始到-256,正数从0开始

位移运算符

多用于高效除2,左移为*2,右移为/2,>>带符号移动,>>>不带符号移动。。。目前还没搞清楚>>>这个是怎么运算的,留一个坑,等以后来补(坑已填,此处已修改,之前搞反了,以为>>是不带符号,>>>是带符号)

public class BitShift {
public static void main(String[] args){
int a = 0x400;//100 0000 0000 === 1024
System.out.println(a);
System.out.println(a >> 1);//向右移动一位 010 0000 0000 == 512
System.out.println(a >> 2);//向右移动两位 001 0000 0000 == 256

System.out.println(a << 1);//向左移动一位 1000 0000 0000 == 2048
System.out.println(a << 2);//向左移动两位 10000 0000 0000 == 4096


int b = -0x400;
System.out.println(b); //1024
System.out.println(b >> 1); // -512
System.out.println(b >> 2); //-256

System.out.println(b << 1); //-2048
System.out.println(b << 2); //-4096

System.out.println(b >>> 1);
System.out.println(b >>> 2);
}
}

-0x400 负数以补码形式填充,1为负数,0为正数
100 0000 0000
011 1111 1111
>>>1 整体右移1位
1011 1111 1111
111111111111

运算符级别

有括号先算括号里面的,同级别先算左边的

a && b || c === (a && b) || c

+=是右结合运算
a += b += c === a += (b += c) === a = a + (b = b + c)

运算符优先级别

枚举类型

枚举相当于一个集合,自己定义数值放到一个集合中。

0x06 字符串

java中没有内置字符串类型,定义一个字符串可以使用String定义字符串变量,用””包含输出内容

public class meiju {
public static void main(String[] args){
String e = "";//输出空字符串
String test = "Hello mode";//输出Hello mode
System.out.println(e);
System.out.println(test);
}
}

子串(截取)

substring 从一个大的字符串中截取一个小字符串

public class meiju {
public static void main(String[] args){
String test = "nihao,shijie";
String s = test.substring(0 , 5);//从0开始截取到第5位,但不会包含第五位的字符
System.out.println(s);//输出:nihao
}
}

拼接

拼接使用+号

public class meiju {
public static void main(String[] args){
String test = "nihao";
String nice = ",shijie";
String s = test + nice;
System.out.println(s);
}
}
print:nihao,shijie

当字符串和任何一个非字符串进行拼接时,后者都会转换为字符串

public class meiju {
public static void main(String[] args){
String test = "nihao";
int a = 12;
String test1 = test + a;
System.out.println(test1);
print: nihao12
//多个字符串放在一起,可以使用定界符
String all = String.join("/" , "T" , "i" , "m" , "e");
System.out.println(all);
}
print:T/i/m/e

不可变字符串

java里面没有内置的字符串函数,如果要修改字符串,需要先截取子串,然后拼接

public class meiju {
public static void main(String[] args){
String test = "nihao";
String test1 = test.substring(0,3) + "Time";//截取nih三位字符,在拼接Time
System.out.println(test1);//nihTime
}
}

这里说下截取位数,从0-3,应该是截取四位,但是java中是截取到3这个位置,niha,但是本身又不包含3位置的字符a,所以0-3截取的应该是nih三位字符

检查字符串是否相等

equals方法,这里可以使用变量,也可以使用字符串

public class meiju {
public static void main(String[] args){
String s = "Hello";
System.out.println("hello".equals(s));

}
}
print:false 因为大小写不一致,所以不相等

空串和null

public class meiju {
public static void main(String[] args){
String s = "";
System.out.println(s.equals(""));//检查为否为空字符串
//print:true
String t = null;
System.out.println(t == null);//检查字符串是否为null
//print:true

//检查字符串不为null也不是空串
System.out.println(s !=null && s.length() !=0);
//print:false
}
}

码点和代码单元

统计字符串长度,以及输出字符串指定位置的字符

public class meiju {
public static void main(String[] args){
String test = "Time";
System.out.println(test.length());//统计字符串长度,print:4

int a = test.codePointCount(0 , test.length());
System.out.println(a);//统计字符串长度,print:4

//返回代码单元的第n个位置
char s = test.charAt(0);
char t = test.charAt(3);
System.out.println(s);//T
System.out.println(t);//e

int index = test.offsetByCodePoints(0 , 1);//返回索引
//int cp = test.codePointCount(index);
System.out.println(index);
}
}

String Api

java.lang.String;

构建字符串

import java.lang.String;
public class meiju {
public static void main(String[] args){
//构建字符串
StringBuilder b = new StringBuilder();
//添加新字符串
b.append("ch");
b.append("str");
System.out.println(b);//print chstr
//添加变量c字符串
String c = "yes";
b.append(c);
System.out.println(b);//print:chstryes
String s = b.toString();

//StringBuilder()//构造一个空的字符串构建器
//int length()//返回构建器或缓冲器中的代码单元数量
//StringBuilder append(String str)//追加一个字符串并返回自身
//StringBuilder append(char c)//追加一个代码单元并返回自身
//StringBuilder appendCodePoint(int cp)//追加一个代码点,并将其转换为一个或两个代码单元并返回自身
//void setCharAt(int i,char c)//讲第i个代码单元设置为c
//StringBuilder insert(int offset,String str)//在offset位置插入一个字符串并返回自身
//StringBuilder delete(int startIndex,int endIndex)//删除偏移量从StartIndex到-endIndex-1的代码单元并返回自身
//String toString()//返回一个与构建器或缓冲器内容相同的字符串
}
}

0x07 输入输出

读取输入

import javax.lang.model.element.NestingKind;
import java.util.Scanner;

public class print {
public static void main(String[] args){
//构造Scanner对象,并与标准输入流System.in关联
Scanner in = new Scanner(System.in);

System.out.print("What is your name?");
String name = in.nextLine();//输入一行
//如输入Time,会输出Time
System.out.println(name);//用户输入字符换行后输出
String n = in.next();
System.out.println(n);//基本同上,具体细节未了解

//读取整数
System.out.println("How old are you? ");
int age = in.nextInt();
System.out.println(age);

//读取浮点数
System.out.println("How float are you? ");
double a = in.nextDouble();
System.out.println(a);
}
}

小栗子

import javax.lang.model.element.NestingKind;
import java.util.*;

public class print {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("What is your name?");//time
String name = in.nextLine();

System.out.println("How old are you? ");//23
int age = in.nextInt();

System.out.println("Hello," + name + ". Next tear,you'll be " + (age+1));
}
}
print: Hello,time. Next tear,you'll be 24

读取密码Console

import java.io.Console;

public class console {
public void testConsole() {
//获取console对象
Console con = System.console();
if (con != null){
System.out.println();//输出空
String name = con.readLine("name: ");//控制台提示信息
char[] pwd = con.readPassword("password:");//控制台提示信息
System.out.println("Hello! \nYour name is: " + name);//
System.out.println("Your password is: " + new String(pwd));
} else {
System.out.println("Couldn't get Console instance, maybe you're running this from within an IDE?");
System.exit(0);
}
}
public static void main(String[] args){
new console().testConsole();
}
}

控制台输入

public class Scanner {
public static void main(String[] args){
//Scanner //用给定的输入流创建一个Scaner对象
//String nextLine()//读取输入的下一行内容
//String next()//读取输入的下一个单词(以空格作为分隔符)
// int nextInt()
/*
double nextDouble() 读取并转换下一个表示整数或浮点数的字符序列
boolean hasNext() 检测输入中是否还有其他的单词
boolean hasNextInt()
boolean hasNextDouble() 检查是否还有表示整数或浮点数的下一个字符序列
*/
/*
java.lang.System;
static Console console()
如果有可能进行交互操作,通过控制台窗口为交互的用户返回一个Console对象,否则返回Null
*/
/*
java.io.Console
static char[] readPassword(String poompt,Object);
static String readLine(String prompt, Object)
显示字符串Prompt并且读取用户输入,直到输入行结束
*/
}
}

格式化输出

printf转换符

printf标志

printf格式说明符语法

public class Format {
public static void main(String[] args){
double x = 10000.0 / 3.0;////x对应的数据类型所允许的最大非0数字位数。
System.out.print(x);//输出3333.3333333333335

//printf可以用8个字符的宽度和小数点后两个字符的精度打印x,
System.out.printf("%8.2f" , x);//输出3333.33
//printf,可以使用多个参数
String name = "Time";
int age = 23;
System.out.printf(" Hello , %s .Next Year, you'll be %d" ,name ,age);//Hello , Time .Next Year, you'll be 23
}
}

日期转换符


import java.util.Date;
public class Format {
public static void main(String[] args){
//格式化输出日期
System.out.printf("%tc" , new Date());//周日 7月 21 00:39:02 CST 2019
//24小时制
System.out.printf("\n%tT" , new Date());//00:39:40

//采用一个格式化的字符串指出要被格式化的参数索引,索引必须紧跟在%后面,并以$终止
System.out.printf("\n%1$s %2$tB %2$te, %2$tY", "Due date:" , new Date());//Due date: 七月 21, 2019
}
}

文件输入输出

注意:在写入文件名时要加上绝对路径,或使用其他方法定位到文件路径所在,不然会报错

package Hello;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Paths;
import java.util.Scanner;

public class input {
public static void main(String[] args) throws IOException{
//读取文件,将内容放到in中
Scanner in = new Scanner(Paths.get("D:\\java\\HelloWorld\\src\\file.txt") , "UTF-8");
//写入文件
PrintWriter out = new PrintWriter("D:\\java\\HelloWorld\\src\\myfileout.txt" , "UTF-8");
out.print("test out");//将内容写入out中
}
/*
java.util.Scanner;5.0版本
Scanner(File f) 构造一个从给定文件读取数据的Scanner
Scanner(String data) 构造一个从给定字符串读取数据的Scanner

java.io.PrintWriter; 1.1 版本
PrintWriter(String fileName) 构造一个将数据写入文件的PrintWriter 文件名由参数指定

java.nio.file.Paths; 7版本
static Path get(String pathname) 根据给定的路径名构造一个path

*/
}

0x08 控制流程

块作用域(block)

可以嵌套多个块,但是不能在嵌套的多个块中声明同一个名称的变量

public class block {
public static void main(String[] args){
int x = 10;
int y = 10;
if ( x == y){
//这是错误的用法
int x = 11;
}
}
}

条件语句(if)

条件判断,判断某的条件是否成立,可以嵌套多个if判断语句,可以增加多个条件进行判断

public class control {
public static void main(String[] args){
//判断x==y
int x = 10;
int y = 11;
if (x == y) {
System.out.println("相等");
}else {
System.out.println("不相等");
}
}
}

循环

while
import java.util.*;
import java.io.InputStream;
import java.util.Scanner;

public class control {
public static void main(String[] args){
//循环while
//计数器
Scanner in = new Scanner(System.in);

System.out.println("How much money do you need to retire? ");
double goal = in.nextDouble();//获取输入的浮点类型值

System.out.println("How much money will you contribute every year? ");
double payment = in.nextDouble();//获取输入的浮点类型值

System.out.println("Interest rate in %: ");
double InterestRate = in.nextDouble();//获取输入的浮点类型值

double balance = 0; //创建浮点型变量
int years = 0;//创建整型变量

//循环
while (balance < goal) {//如果balance小于输入的goal值,条件成立,开始循环
balance += payment; //balance = balance + payment; 先加等于
//创建一个浮点型变量,将balance×InterestRate/100得出的值赋给新建的变量interest
double interest = balance * InterestRate /100;
balance += interest;//balance = balance + interest;在将balance和interest相加,值赋给balance
years++;//years自增
}

System.out.println("You can retire in " + years + " years.");//获取循环的次数
}
}
do while
import java.util.*;
import java.io.InputStream;
import java.util.Scanner;

public class control {
public static void main(String[] args){
//do while 先满足一个条件,在循环
Scanner in = new Scanner(System.in);
System.out.println("How much money do you need to retire? ");
double goal = in.nextDouble();//获取输入的浮点类型值
System.out.println("How much money will you contribute every year? ");
double payment = in.nextDouble();//获取输入的浮点类型值
System.out.println("Interest rate in %: ");
double InterestRate = in.nextDouble();//获取输入的浮点类型值
double balance = 0; //创建浮点型变量
int year = 0;//创建整型变量

String input = null;//创建一个字符串函数

//循环
do {
balance += payment; //balance = balance + payment; 先加等于
//创建一个浮点型变量,将balance×InterestRate/100得出的值赋给新建的变量interest
double interest = balance * InterestRate /100;
balance += interest;//balance = balance + interest;在将balance和interest相加,值赋给balance
year++;//years自增
//输出循环次数和balance最后得出的值
System.out.printf("After year %d , your balance is %,.2f%n" , year , balance);
System.out.print("Ready to retire? (Y/N)");
input = in.next();//输入Y/N,赋值给input
}
while (input.equals("N"));//获取input的值,如果等于N,重复执行循环
}
}

确定循环(for)

import java.util.*;
import java.io.InputStream;
import java.util.Scanner;
public class for1 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("How much money do you need to retire? ");
int k = in.nextInt();//获取输入整数类型值,
System.out.println("How much money will you contribute every year? ");
int n = in.nextInt();//获取输入整数类型值
//for循环
int t = 1;
for (int i = 1; i<=k; i++){
t = t * (n - i + 1) / i;
System.out.println("Your odds are 1 in " + t + ". Good luck!");
}
}
/*
复盘:
假定:输入的k=2 n=10
i=1,1<=2,条件成立,执行循环体,t=1 t=1*(10-1+1)/1,t=10,输出,i=1,i自增1。
i=2,2<=2,条件成立,执行循环体,t=10 t=10*(10-2+1)/2, t=45,输出,i=2,i自增1。
i=3,3<=2,条件不成立,循环结束
*/
}

多重循环(switch)

import java.util.*;
import java.util.Scanner;

public class multiple {
public static void main(String[] args){
//switch多重循环
Scanner in = new Scanner(System.in);
System.out.println("Select an option (1,2,3,4):");
int t = in.nextInt();//获取输入的整数值
//开始循环
switch (t){
case 1:
System.out.println("这是第一层");//当值为1,输出代码
break;//退出循环
case 2:
System.out.println("这是第二层");
break;
case 3:
System.out.println("这是第三层");
break;
case 4:
System.out.println("这是第四场");
break;
}
}
}

终止循环

brack	退出循环
continue 终止循环

0x09 大数值

BigInteger 任意精度的整数运算
BigDecimal 任意精度的浮点数运算
add和multiply是进行大数值的+和*

import java.math.*;
import java.util.*;
public class BigInteger1 {
public static void main(String[] args){
BigInteger a = BigInteger.valueOf(100);//将普通数值转换为大数值
System.out.println(a);//100。。没啥区别
BigInteger b = BigInteger.valueOf(13);
BigInteger c = a.add(b);//c = a + b
BigInteger d = c.multiply(b.add(BigInteger.valueOf(2)));//d = c * (b + 2)
System.out.println(b);//13
System.out.println(c);//113
System.out.println(d);//1695
}
}

彩票小栗子

import java.math.*;
import java.util.*;
import java.util.Scanner;

public class BigInteger1 {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.println("How many nuber do you need to draw? ");
int k = in.nextInt();
System.out.println("How many nuber do you need to draw? ");
int n = in.nextInt();

BigInteger t = BigInteger.valueOf(1);

for (int i = 1; i <= k; i++) {
t = t.multiply(BigInteger.valueOf((n - i + 1)).divide(BigInteger.valueOf(i)));
System.out.println("Your odds are 1 in " + t + ". Good luck!");
}
/*
print:
How many nuber do you need to draw?
10
How many nuber do you need to draw?
20325
Your odds are 1 in 20325. Good luck!
Your odds are 1 in 206542650. Good luck!
Your odds are 1 in 1399119911100. Good luck!
Your odds are 1 in 7107529148388000. Good luck!
Your odds are 1 in 28884998459048832000. Good luck!
Your odds are 1 in 97804604782339345152000. Good luck!
Your odds are 1 in 283828963078348779631104000. Good luck!
Your odds are 1 in 720641737255927551483373056000. Good luck!
Your odds are 1 in 1626488400986628483697972987392000. Good luck!
Your odds are 1 in 3303397942403842450390583137393152000. Good luck!
*/
}

}

BigInteger API

BigInteger add(BigInteger other)
BigInteger subtract(BigInteger other)
BigInteger multiply(BigInteger other)
BigInteger divide(BigInteger other)
BigInteger mod(BigInteger other)
返回这个大整数另外一个大整数的other的和、差、积、商以及余数。
int compareTo(BigInteger other)
如果这个大整数与另一个大整数other相等,返回0;如果这个大整数小于另外一个大整数other,返回负数;否则,返回整数。
static BigInteger valueOf(long x)
返回值等于x的大整数

java.math.BigInteger;1.1版本
BigDecima1 add(BigDecima1 other)
BigDecima1 subtract(BigDecima1 other)
BigDecima1 multiply(BigDecima1 other)
BigDecima1 divide(BigDecima1 other RoundlingMode mode) 5.0
返回这个大实数与另一个大实数other的和、差、积、商。要想计算商,必须给出舍入方式
RoundingMode.HALF_UP是在学校中学习的四舍五入方式,它适用于常规的计算。

int compareTo(BigDecima1 other)
如果这个大实数与另一个大实数相等,返回0,如果这个大实数小于另一个大实数,返回负数,否则,返回整数。
static BigDecima1 valueOf(long x)
static BigDecima1 valueOf(long x,int scale)
返回值为x或x/10(scale)的一个大实数

0x0A 数组

public class array {
public static void main(){
//声明整数型数组
//int[] a;
//初始化数组,创建一个可以存储100个整数的数组
int[] a = new int[100];
for (int i = 0; i <= a.length; i++){
a[i] = i;
}
//创建存储10个字符串的数组
String[] names = new String[10];
for (int i=0; i<10; i++) {
names[i] = "";
}
}
}

for each

public class shuzu {
public static void main(String[] args) {
//声明整数型数组
//int[] a;
//初始化数组,创建一个可以存储100个整数的数组
int[] a = new int[100];
int t = 0;
for (int e : a){
e = t++; //将t自增的值赋给e
System.out.println(e);//输出e,会从输出从下标0开始一直到99结束的100个整数值
}
}
//同上
for (int i=0; i<=a.length; i++) {
System.out.println(a[i]);
}
}

数组初始化和匿名数组

public class niming {
public static void main(String[] args){
//创建数组同时初始化值
int[] small = {2,3,4,5};
System.out.println(small[0]);
for (int e:small ) {
System.out.println(small[e]);
}
//重新初始化数组
small = new int[]{21,232,435};

//简写
int[] an = {12,232,434,543};
small = an;
}
}

数组拷贝

import java.lang.reflect.Array;
import java.util.*;
public class copy {
public static void main(String[] args){
int[] small = {2,3,4,5,6,7};
int[] luck = small;
small[5] = 32;
//System.out.println(luck[5]);

int[] ck;
ck = Arrays.copyOf(small, small.length);//复制small数组值到新数组ck中
for (int e : small
) {
System.out.println(ck[e]);//遍历循环
}
//luck = Array.copyOf(small, small.length-1);

}
}

命令行参数

每一个java的程序都会带String[] args参数的main方法,这个参数表明将接收到一个字符串数组,也就是命令行参数
在idea中运行

import java.lang.reflect.Array;
import java.util.*;
public class copy {
public static void main(String[] args){
int[] small = {2,3,4,5,6,7};
int[] luck = small;
small[5] = 32;
//System.out.println(luck[5]);

int[] ck;
ck = Arrays.copyOf(small, small.length);//复制small数组值到新数组ck中
for (int e : small
) {
System.out.println(ck[e]);//遍历循环
}
//luck = Array.copyOf(small, small.length-1);

}
}
print: hello,!

在命令行中运行

import java.lang.reflect.Array;
import java.util.*;
public class copy {
public static void main(String[] args){
int[] small = {2,3,4,5,6,7};
int[] luck = small;
small[5] = 32;
//System.out.println(luck[5]);

int[] ck;
ck = Arrays.copyOf(small, small.length);//复制small数组值到新数组ck中
for (int e : small
) {
System.out.println(ck[e]);//遍历循环
}
//luck = Array.copyOf(small, small.length-1);

}
}
运行命令:java dos -h cruel world
print: Hello, cruel world!

数组排序

使用Arrays.sort方法

import java.util.*;
public class drawing {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("How many number do you need to draw? ");
int k = in.nextInt();//获取输入的值

System.out.println("What is the highest number you can draw? ");
int n = in.nextInt();//获取输入的值

int[] numbers = new int[n];
for (int i=0; i<numbers.length-1; i++){
numbers[i] = i + 1;//如果n=10,程序循环20-1次,并且将每次循环i的值+1 赋给number数组中i
System.out.println("第" + i + "次i的值:" + i);
/*
第0次i的值:0
第1次i的值:1
第2次i的值:2
第3次i的值:3
第4次i的值:4
第5次i的值:5
第6次i的值:6
第7次i的值:7
第8次i的值:8
第9次i的值:9
第10次i的值:10
第11次i的值:11
第12次i的值:12
第13次i的值:13
第14次i的值:14
第15次i的值:15
第16次i的值:16
第17次i的值:17
第18次i的值:18
*/
}

int[] result = new int[k];
for (int i=0; i< result.length-1; i++){
int r = (int) (Math.random() * n);//定义r变量,生成一个随机值*n的值,并且强制转换为整型
result[i] = numbers[r];//将r的值赋给result数组中的i
numbers[i] = numbers[r];//将r的值赋给number数组中的i
numbers[r] = numbers[n - 1];//将number中n的值-1赋给numbwe数组中的r
n--;//n自减1
}
Arrays.sort(result);//将result中的数值型进行排序
System.out.println("Bet the following. It'll make you rich!");
int t = 0;
for (int r : result
) {
t++;
System.out.println("第" + t + "次r的值: " + r);//遍历r的值
}
/*
Bet the following. It'll make you rich!
第1次r的值: 0
第2次r的值: 0
第3次r的值: 0
第4次r的值: 0
第5次r的值: 3
第6次r的值: 3
第7次r的值: 3
第8次r的值: 3
第9次r的值: 9
第10次r的值: 9
第11次r的值: 10
第12次r的值: 10
第13次r的值: 12
第14次r的值: 13
第15次r的值: 13
第16次r的值: 14
第17次r的值: 14
第18次r的值: 15
第19次r的值: 16
第20次r的值: 18
*/
}
}

多维数组

假设建立一直数值表,用来显示不同利率下投资10000会增长多少,利息每年兑现,而且又被用于投资
小栗子的数值表

public class duowei {
public static void main(String[] args){
//定义常量
final double STARTRATE = 10;
final int NRATES = 6;
final int NYEARS = 10;

double[] rate = new double[NRATES];//初始化6个数组单元rate[0,0,0,0,0,0]
for (int j=0; j<rate.length; j++){
rate[j] = (STARTRATE + j) /100.0;//每次循环10+j次并且除以100的值赋给rate数组
}
double[][] balaces = new double[NYEARS][NRATES];//定义二维数组

for (int j = 0; j<balaces[0].length; j++){
balaces[0][j] = 10000;//将10000赋给balaces[0][10000,10000,10000,10000,10000,10000]
}
//嵌套循环
for (int i=1; i<balaces.length; i++){
for (int j=0; j<balaces[i].length; j++){
double old = balaces[i - 1][j];//balaces第0个下标下的0下标的值赋给old.old=10000
double eat = old * rate[j];//10000 * rate[j]的值赋给eat
balaces[i][j] = old + eat;//old+eat的值赋给balaces[0][0]
}
}
for (int j=0; j<rate.length; j++){
System.out.printf("%9.0f%%" , 100 * rate[j]);//格式化输出,100*rate[j]的值
}

//遍历循环,以格式化方式输出balaces中所有的数组
System.out.println();
for (double[] row : balaces
) {
for (double b : row
) {
System.out.printf("%10.2f" , b);
}
System.out.println();
}

/*
10% 11% 12% 13% 14% 15%
10000.00 10000.00 10000.00 10000.00 10000.00 10000.00
11000.00 11100.00 11200.00 11300.00 11400.00 11500.00
12100.00 12321.00 12544.00 12769.00 12996.00 13225.00
13310.00 13676.31 14049.28 14428.97 14815.44 15208.75
14641.00 15180.70 15735.19 16304.74 16889.60 17490.06
16105.10 16850.58 17623.42 18424.35 19254.15 20113.57
17715.61 18704.15 19738.23 20819.52 21949.73 23130.61
19487.17 20761.60 22106.81 23526.05 25022.69 26600.20
21435.89 23045.38 24759.63 26584.44 28525.86 30590.23
23579.48 25580.37 27730.79 30040.42 32519.49 35178.76
*/
}
}

不规则数组

public class Array {
public static void main(String[] args){
//定义常量
final int NMAX = 10;
int[][] odds = new int[NMAX + 1][];//定义二维数组,并且将一维数组的单元+1,赋给odds
for (int n=0; n<=NMAX; n++){
odds[n] = new int[n + 1];//循环初始化一维数组,并且将下标为n的单元值+1
}
//嵌套循环
for (int n=0; n<odds.length; n++){
for (int k=0; k<odds[n].length; k++){
int lot = 1;//定义整型变量初始化值为1
for (int i=1; i<=k; i++){
lot = lot * (n - i + 1) / i;//1 * (0-1+1) 赋给lot;lot此时为2
}
odds[n][k] = lot;//将2赋给odds[0][0]
}
}
//遍历循环输出odds二维数组的所有值
for (int[] row : odds
) {
for (int odd : row
) {
System.out.printf("%4d" , odd);
}
System.out.println();
}
/*
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
*/
}
}

第三章终于过完了。。。。