Skip to content

Commit

Permalink
Java第二天
Browse files Browse the repository at this point in the history
  • Loading branch information
chao420456 committed Nov 25, 2015
1 parent 0e5c44c commit 8e778f3
Show file tree
Hide file tree
Showing 57 changed files with 958 additions and 0 deletions.
Binary file added day02/ASCII码表.doc
Binary file not shown.
Binary file added day02/Java语言运算符优先级表.doc
Binary file not shown.
Binary file added day02/code/01_常量/ConstantDemo.class
Binary file not shown.
39 changes: 39 additions & 0 deletions day02/code/01_常量/ConstantDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
常量:在程序执行的过程中其值不可以发生改变
举例:π
分类:
A:字面值常量
1,12.5
B:自定义常量(面向对象部分讲解)
字面值常量分类:
A:字符串常量 用""括起来的内容
B:整数常量 所有的整数数据
C:小数常量 所有的带小数的数据
D:字符常量 用单引号括起来的内容
E:布尔常量 只有两个值:true和false
F:空常量 null(数组部分去讲解)
*/
class ConstantDemo {
public static void main(String[] args) {
//字符串常量
System.out.println("HelloWorld");

//整数常量
System.out.println(100);

//小数常量
System.out.println(12.345);

//字符常量
System.out.println('A');
//下面的是错误的
//System.out.println('BC');
System.out.println('1');

//布尔常量
System.out.println(true);
System.out.println(false);
}
}
Binary file added day02/code/02_进制转换/JinZhiDemo.class
Binary file not shown.
19 changes: 19 additions & 0 deletions day02/code/02_进制转换/JinZhiDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
二进制
由0,1组成。以0b开头
八进制
由0,1,…7组成。以0开头
十进制
由0,1,…9组成。整数默认是十进制的
十六进制
由0,1,…9,a,b,c,d,e,f(大小写均可)。以0x开头
*/
class JinZhiDemo {
public static void main(String[] args) {
System.out.println(0b100);//4
System.out.println(0100);//64
System.out.println(100);//100
System.out.println(0x100);//256
}
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added day02/code/02_进制转换/进制图解.bmp
Binary file not shown.
Binary file added day02/code/02_进制转换/进制组成.bmp
Binary file not shown.
Binary file not shown.
Binary file added day02/code/03_变量/变量的组成.bmp
Binary file not shown.
Binary file added day02/code/04_数据类型/Array2Sum.class
Binary file not shown.
16 changes: 16 additions & 0 deletions day02/code/04_数据类型/Array2Sum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
ÐèÇó£º
*/

class Array2Sum {
public static void main(String[] args) {
int[][] arr = {{22,66,44},{77,33,88},{25,45,65},{11,66,99}};
int sum = 0;
for (int i=0; i<arr.length; i++) {
for (int j=0; j<arr[i].length; j++) {
sum +=arr[i][j];
}
}
System.out.println("×ÜÊýΪ£º" +sum);
}
}
Binary file added day02/code/04_数据类型/DataType.class
Binary file not shown.
82 changes: 82 additions & 0 deletions day02/code/04_数据类型/DataType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
为了更好的表达现实世界的事物,Java针对不同的事物提供了不同的数据类型。
数据类型:
基本类型:4类8种
引用类型:类,接口,数组。(后面讲)
基本类型:
整数: 占用的内存空间
byte 1字节
01111111
10000000(1既表示符号位,又表示数值 -128)
short 2字节
int 4字节
long 8字节
浮点数:
float 4字节
double 8字节
字符:
char 2字节
布尔:
boolean 未知。1字节
面试题:
Java中字符可以存储一个汉字吗?
可以。因为Java语言采用的是unicode编码,
而unicode编码的每个字符是两个字节,
所以,java中的字符可以存储一个汉字。
注意:
整数默认是int类型
浮点数默认是double类型
long类型的变量,要加l或者L。
float类型的变量,要加f或者F。
在同一对{}里面,是不能有同名的变量。
*/
class DataType {
public static void main(String[] args) {
//定义变量的格式:
//数据类型 变量名 = 初始化值;

//定义byte类型的变量
byte b = 1;
System.out.println(1);
System.out.println(b);

//定义short类型的变量
short s = 100;
System.out.println(s);

//定义int类型的变量
int i = 100000;
System.out.println(i);

//报错
//int j = 2147483648;
//System.out.println(j);

//定义long类型的变量
long l = 2147483648L;
System.out.println(l);

//定义float类型的变量
float f = 12.34F;
System.out.println(f);

//定义double类型的变量
double d = 23.56;
System.out.println(d);

//定义char类型的变量
char ch = 'a';
System.out.println(ch);

//定义boolean类型的变量
boolean flag = true;
System.out.println(flag);
}
}
Binary file added day02/code/04_数据类型/DataType2.class
Binary file not shown.
42 changes: 42 additions & 0 deletions day02/code/04_数据类型/DataType2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
使用变量的注意事项:
A:作用域
变量定义在哪一级大括号中,哪个大括号的范围就是这个变量的作用域。
相同的作用域中不能定义两个同名变量。
B:初始化值
没有初始化值不能直接使用
C:定义变量的问题
在一行上建议只定义一个变量
可以定义多个,但是不建议
*/
class DataType2 {
//int i = 100;

public static void main(String[] args) {
int i = 200;

//int j;
//System.out.println(j);

//int a = 10;int b = 20;

//int a = 10;
//int b = 20;

//int a,b;

//int x=10,y=20;
//int x = 10;
//int y = 20;

//定义变量
//数据类型 变量名 = 初始化值;
//其实我们还可以改变一下,只要在使用该变量前赋值即可。
int j;
//........
j = 200;
System.out.println(j);
}
}
Binary file added day02/code/04_数据类型/MyArrayDemo.class
Binary file not shown.
38 changes: 38 additions & 0 deletions day02/code/04_数据类型/MyArrayDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.Scanner;

class MyArrayDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("请输入三个数据:");
int[] arr;
int i = 0;
arr = new int[3];
System.out.println(arr);

while(true) {
arr[i] = sc.nextInt();
i++;
if (i >= arr.length) {
break;
}
}

/*
for (i = 0; i<arr.length; i++) {
arr[i] = sc.nextInt();
//System.out.println(arr[i]);
}
*/

for (int j=0; j<arr.length; j++) {
System.out.println("arr[" +j +"]=" +arr[j]);
}

int temp = 0;
for (int j=1; j<arr.length; j++) {
temp = arr[j-1]>=arr[j]?arr[j-1]:arr[j];
}
System.out.println("arr数组中最大值为:" +temp);
}
}
69 changes: 69 additions & 0 deletions day02/code/04_数据类型/MyArrayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
需求:一维数组的练习
分析:
*/
import java.util.Scanner;

class MyArrayTest {
public static void main(String[] args) {
int arr = {1,2,3,4,5,6,7,8};
String strArray = {"","星期一","星期二","星期三","星期四","星期五","星期六","星期日"};

//查表法
Scanner sc = new Scanner(System.in);
System.out.println("请输入查表法数字(1-7):");
int index = sc.nextInt();
System.out.println("你要查找的星期为:" +strArray[index]);
}

//遍历
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] +", ");
}
System.out.print(arr[i]);
}
System.out.println("]");
}

//最值
public static int getMax(int[] arr) {
int max = arr[0];
for (int i=0; i<arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}

//反转
public static void reverse(int[] arr) {
for (int start=0,end=arr.length; start<=end; start++,end--) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}
}

//元素第一次出现的索引
public static int indexOfArray(int[] arr, int key) {
int index = -1;
for (int i=0; i<arr.length; i++) {
if (key == arr[i]) {
index = i;
break;
}
}
return index;
}

//排序
//public static void OrderOfArray(int[] arr) {

//}

}
Binary file added day02/code/05_类型转换/DataTypeDemo.class
Binary file not shown.
29 changes: 29 additions & 0 deletions day02/code/05_类型转换/DataTypeDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
+是一个运算符, 我们应该能够看懂,做数据的加法。
参与运算的数据,要求类型一致。
boolean类型不能转换为其他的数据类型。
隐式转换:
A:byte,short,char-->int-->long-->float-->double
B:byte,short,char相互之间不转换,他们参与运算首先转换为int类型
*/
class DataTypeDemo {
public static void main(String[] args) {
//类型一样的数据
int a = 10;
int b = 20;
System.out.println(a + b);
int c = a + b;
System.out.println(c);
System.out.println("--------------");

//定义变量
byte by = 3;
int i = 4;
System.out.println(by + i);
int j = by + i;
System.out.println(j);
}
}
Binary file added day02/code/05_类型转换/DataTypeDemo2.class
Binary file not shown.
13 changes: 13 additions & 0 deletions day02/code/05_类型转换/DataTypeDemo2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
强制转换:从大到小,不建议使用,因为可能有精度的丢失。
目标类型 变量名=(目标类型)(被转换的数据);
*/
class DataTypeDemo2 {
public static void main(String[] args) {
//定义变量
byte by = 3;
int i = 4;
byte bb = (byte)(by + i);
System.out.println(bb);
}
}
Binary file added day02/code/05_类型转换/DataTypeDemo3.class
Binary file not shown.
29 changes: 29 additions & 0 deletions day02/code/05_类型转换/DataTypeDemo3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
+:做加法操作
A:针对数据操作
B:针对字符进行做+
ASCII码表
'0' 48
'A' 65
'a' 97
C:针对字符串进行做+
在字符串的操作中,叫字符串连接符
*/
class DataTypeDemo3 {
public static void main(String[] args) {
System.out.println('a');
System.out.println('a'+1);
System.out.println('A'+0);
System.out.println('0'+0);
System.out.println("-----------");


System.out.println('a'+'b');
System.out.println("a"+"b");

System.out.println("hello"+'a'+1); //左边字符串,往后拼的都是字符串
System.out.println('a'+1+"hello");
System.out.println("5+5="+5+5);
System.out.println(5+5+"=5+5");
}
}
Binary file added day02/code/05_类型转换/DataTypeTest.class
Binary file not shown.
Loading

0 comments on commit 8e778f3

Please sign in to comment.