java源代码经典入门案例

class Demo
{
public static void main(String[] args)
{
System.out.println("hello E盘");
}
}




class Demo
{
public static void main(String[] args)
{
System.out.println("hello E盘");
}
}




/*
需求:练习一个hello world程序。

思路:
1,定义一个类,因为java程序都定义类中,java程序都是以类的形式存在的,类的形式其实就是一个字节码文件最终体现。
2,定义一个主函数。为了让该类可以独立运行。
3,因为演示hello world,在控制台上看到该字样,所以需要使用输出语句完成。

步骤:
1,用class关键字来完成类的定义,并起一个阅读性强的类名。
2,主函数:public static void main(String[] args)这时固定格式的。jvm认识。
3,使用输出语句:System.out.println("hello world");

代码仅仅是思想的一种体现形式。

*/
class Demo
{
//定义一个主函数,为了保证程序的独立运行。
public static void main(String[] args)
{
System.out.println("hello world");//这是输出语句,用于将括号中的数据打印到控制台上,ln可以在数据的结尾处换行。
}
}




class OperateDemo
{
public static void main(String[] args)
{

//算术运算符。 + - * / %(取余,模运算) +(连接符)
// ++(自增:就在原有数据基础上+1,在赋给原有数据) --

//int x = 6370;
//x = x / 1000 * 1000;
//System.out.println(x);

// System.out.println(5%2);

// System.out.println(3+"2");

//System.out.println("5+5="+(5+5));//"5+5=5"+5 "5+5=55"

//int a = 4,b = 5;

//System.out.println("a="+a+",b="+b);//a=4,b=5;

int a = 3,b;
//a++;//a = a+1;
// b = a++;
b = (a++)+(++a)+(a++)+a;
// 3 5 5 6
System.out.println("a="+a+",b="+b);

int i = 3;
i = i++;
System.out.println("i="+i);


}
}




class OperateDemo2
{
public static void main(String[] args)
{
//赋值运算符。= += -= *= /= %=
// int a,b,c;
// a = b = c = 4;

//int a = 4;
//a+=2;//a = a + 2;

short s = 3;
//s+=4;
s = (short)(s + 4);


System.out.println("s="+s);
}
}




class VarDemo
{
public static void main(String[] args)
{
//数据类型 变量名 = 初始化值;
byte b = 3;
short s = 4000;
int x = 12;
long l = 123l;

float f = 2.3f;

double d = 3.4;

char ch = '1';

boolean bl = true;
bl = false;

{
int z = 9;
System.out.println(z);
}

System.out.println(z);

//System.out.println(y);
}
}




class VarDemo2
{
public static void main(String[] args)
{

// int x = 3;
// byte b = 5;
// x = x + b;

// byte b = 3;
// b = (byte)(b + 200);//强制类型转换。



//System.out.println((char)('a'+1));
// System.out.println('你'+0);//unicode国际标准码表。


byte b = 4;
//b = 3+7;
byte b1 = 3;

byte b2 = 7;

//b2 = 127;
int x;
//b = b1 + b2;
int x1 =Integer.MAX_VALUE ;
int x2 =2;
x = x1+x2;

//System.out.println(x);



int n = 8;
n = 9;
n = 19;

//System.out.println(n);



}
}




class DoWhileDemo
{
public static void main(String[] args)
{

/*
do
{
执行语句;
}while(条件表达式);

*/
int x = 1;
do
{
System.out.println("x="+x);
x++;
}
while (x<1);

/*
do while语句的特点:无论条件是否满足,循环体至少执行一次。

*/

int y = 1;
while(y<1)
{
System.out.println("y="+y);
y++;
}
}
}





class ForDemo
{
public static void main(String[] args)
{
/*

for(初始化表达式;循环条件表达式;循环后的操作表达式)
{
执行语句;(循环体)
}



for(int x = 1; x<3; x++)
{

System.out.println("x="+x);

}
*/
int x = 1;
for(System.out.println("a");x<3; System.out.println("c"))
{
System.out.println("d");
x++;
}

// for(int a=0,b=0; a<3; a++,b--)

//a d c d c
}
}

class ForTest
{
public static void main(String[] args)
{
/*
用for完成累加。
*/
int sum = 0;
for(int x=1; x<=10; x++)
{
sum = sum + x;
}

System.out.println("sum="+sum);

/*
for和while的特点:
1,for和while可以互换。
2,格式上的不同,在使用上有点小区别。
如果需要通过变量来对循环进行控制,该变量只作为循环增量存在时,区别就体现出来了。
*/

//打印1~10十个数字
int x = 1;
while(x<5)
{
System.out.println("x="+x);
x++;
}
System.out.println("x===="+x);

for(int y=1; y<5; y++)
{
System.out.println("y="+y);
}
System.out.println("y====="+y);


//无限循环最简单的形式。
// while(true){}

// for(;;){}

什么时候使用循环结构呢?
当对某些代码执行很多次时,使用循环结构完成。

当对一个条件进行一次判断时,可以使用if语句。
当对一个条件进行多次判断时,可以使用while语句。

注意:
在使用循环时,一定要明确哪些语句需要参与循环,哪些不需要。
循环通常情况下,需要定义条件,需要控制次数。



}
}




class IfDemo
{
public static void main(String[] args)
{
// System.out.println("Hello World!1");
// System.out.println("Hello World!2");
// System.out.println("Hello World!3");
// System.out.println("Hello World!4");

/*
if语句的第一种格式:
1,
if(条件表达式)
{
执行语句;
}
*/

int x = 1;
if(x>1)
{

if(x<2)
{

System.out.println("yes");

}
}
System.out.println("over");

}
}




class IfDemo2
{
public static void main(String[] args)
{
/*
if语句的第二种格式



if(条件表达式)
{
执行语句;
}
else//否则
{
执行语句;
}

*/
int x = 1;
if(x>1)
{
System.out.println("yes");
}
else
{
System.out.println("no");
}
System.out.println("Hello World!");


int a = 3,b;
/*
if(a>1)
b = 100;
else
b = 200;
*/
b = a>1?100:200;//三元运算符就是if else 语句简写格式。

// 简写格式什么时候用?
// 当ifelse运算后,有一个具体的结果时,可以简化写成三元运算符。
System.out.println("b="+b);
}
}



class IfDemo3
{
public static void main(String[] args)
{
{//局部代码块可以定义局部变量的生命周期。
int a = 3;
//a 的运算。
System.out.println(a+4);
}

/*
if语句第三种格式:
if(条件表达式)
{
执行语句;
}
else if (条件表达式)
{
执行语句;
}
……
else
{
执行语句;
}

*/
int x = 3;
if(x>1)
System.out.println("a");
else if(x>2)
System.out.println("b");
else if(x>3)
System.out.println("c");
else
System.out.println("d");


int y = 3;
if(y>1)
System.out.println("a1");
if(y>2)
System.out.println("b1");
if(y>3)
System.out.println("c1");
else
System.out.println("d1");


if(x==1)

if(y==1)

System.out.println("a");

else
System.out.println("b");


else

if(y==1)

System.out.println("c");

else

System.out.println("d");


// if(false);


{//局部代码块。
int m = 89;
System.out.println("Hello World!..."+m);
}

System.out.println("over....."+m);
}
}



class IfTest
{
public static void main(String[] args)
{

/*
需求:根据用户指定的具体数据,判断该数据对应的星期。
1-星期一Monday

思路:
用户输入无法获取但是那只是具体数据的一种获取手段而已。
而我们要做的功能仅仅是对用户指定的数据进行对应星期的打印而已。

所以具体的数据不确定,完成可以使用变量来表示。

我们只对变量进行操作即可。至于变量的值,可以有用户决定。

因为数据的不确定性,所以要对数据进行判断。
使用if语句。


*/

int week = 10;

if(week==1)
System.out.println(week+"对应中文是星期一");
else if(week==2)
System.out.println(week+"对应中文是星期二");
else if(week==3)
System.out.println(week+"对应中文是星期三");
else if(week==4)
System.out.println(week+"对应中文是星期四");
else if(week==5)
System.out.println(week+"对应中文是星期五");
else if(week==6)
System.out.println(week+"对应中文是星期六");
else if(week==7)
System.out.println(week+"对应中文是星期日");
else
System.out.println(week+"没有对应

的星期");

}
}





class IfTest2
{
public static void main(String[] args)
{

/*
一年有四季。
春季:3 4 5
夏季:6 7 8
秋季:9 10 11
冬季:12 1 2
根据用户输入的月份,给出对应的季节。


*/

int month = 8;

if(month<1 || month>12)
System.out.println(month+"月没有对应的季节");
else if(month>=3 && month<=5)
System.out.println(month+"月是春季");
else if(month>=6 && month<=8)
System.out.println(month+"月是夏季");
else if(month>=9 && month<=11)
System.out.println(month+"月是秋季");
else
System.out.println(month+"月是冬季");



/*
if(month==3 || month==4 || month==5)
System.out.println(month+"月是春季");
else if(month==6 || month==7 || month==8)
System.out.println(month+"月是夏季");
else if(month==9 || month==10 || month==11)
System.out.println(month+"月是秋季");
else if(month==12 || month==1 || month==2)
System.out.println(month+"月是冬季");
else
System.out.println(month+"月没有对应的季节");
*/

}
}


class
{
public static void main(String[] args)
{
;
System.out.println("Hello World!");
}
}
if和switch的应用:

if:
1,对具体的值进行判断。
2,对区间判断。
3,对运算结果是boolean类型的表达式进行判断。

switch:
1,对具体的值进行判断。
2,值的个数通常是固定的。
对于几个固定的值判断,建议使用switch语句,因为switch语句会将具体的答案都加载进内存。
效率相对高一点。




class OperateDemo3
{
public static void main(String[] args)
{

/*
比较运算符,运算完的结果必须是true或者false。
*/
// System.out.println(3>2);//true
// System.out.println(3==2);//false

// 22 x<5

/*
逻辑运算符有什么用?
用于连接 两个boolean类型的表达式。

&:与
|:或

&:符号的运算特点:

true & true = true;
true & false = false;
false & true = false;
false & false = false;
&:运算规律:
&运算的两边只有有一个是false,结果肯定是false。
只有两边都为true,结果才是true。

|:运算特点:
true | true = true;
true | false = true;
false | true = true;
false | false = false;
|:运算规律:
|运算的两边只要有一个是true,结果肯定是true。
只有两边都为false。结果是false。



^:异或 :和或有点不一样。
^:运算特点。
true ^ true = false;
true ^ false = true;
false ^ true = true;
false ^ false = false;
^异或的运算规律:
^符号的两边结果如果相同,结果是false。
两边的结果不同,结果是true。

!:非运算,判断事物的另一面。
!true = false
!false = true;

!!true = true;


面试题:
&&:
和&运算的结果是一样的。

但是运算过程有点小区别。

&:无论左边的运算结果是什么,右边都参与运算。
&&:当左边为false时,右边不参与运算的。


||:
和|运算的结果是一样的。但是运算过程有点小区别。

|:无论左边的运算结果是什么,右边都参与运算。
||:当左边为true时,右边不参与运算的。
*/

int x = 1;

// System.out.println(x>2&x<5);
// System.out.println(x<2|x>5);

// x>2 && x<5
}
}




class OperateDemo4
{
public static void main(String[] args)
{

// System.out.println(6&3);
// System.out.println(6|3);
// System.out.println(~6);

System.out.println(3<<2);//3左移两位。

}
}



class OperateDemo5
{
public static void main(String[] args)
{

int x = 0,y;

y = (x>1)?100:200;
System.out.println("y="+y);

//获取两个整数中的较大的整数。

int a,b;
int max = a>b?a:b;

//获取三个整数中的较大的整数。

int o,p,q;

int temp = o>p?o:p;
int max2 = temp>q?temp:q;

}
}



class OperateTest
{
public static void main(String[] args)
{
//最有效率的方式算出2乘以8等于几?


// System.out.println(2<<3);

// 对两个整数变量的值进行互换 (不需要第三方变量)
int a = 3,b = 5;
System.out.println("a="+a+",b="+b);

/*
开发时,使用第三方变量的形式,因为阅读性强。
int c ;
c = a;
a = b;
b = c;
*/


//这种方式不要用,如果两个整数的数值过大,会超出int范围,会强制转换。数据会变化。
/*
a = a + b; //a = 3 + 5;a = 8;
b = a - b; //3+5-5 = 3;b = 3;
a = a - b; //3+5-3 = 5;a = 5;
*/

/*
面试的时候用。
a = a ^ b; //a = 3 ^ 5;
b = a ^ b; //b = (3^5)^5; b = 3;
a = a ^ b; //a = (3^5)^3; a = 5;
*/
System.out.println("a="+a+",b="+b);
}
}



class OperateTest2
{
public static void main(String[] args)
{

/*
对一个整数的最后一个字节,高四位和低四位进行换位。

int num = 0101-1100 & 255;
1100-0101

61 = 0011-1101
1101-0011 = 211


*/
int num = 61;

int n1 = num & 15;//低四位
int n2 = num & (15<<4);//高四位


int n = n1<<4 | n2>>>4;


System.out.println("n="+n);


}
}



class SwitchDemo
{
public static void main(String[] args)
{

/*
switch(表达式)
{
case 取值1:
执行语句;
break;
case 取值2:
执行语句;
break;
…...
default:
执行语句;
break;
}

*/

int x = 2;
switch(x)//byte,short,int,char.
{
default:
System.out.println("d");
//break;
case 4:
System.out.println("a");
//break;
case 1:
System.out.println("b");
break;
case 3:
System.out.println("c");
break;

}

int a = 4,b =2;
char opr = '%';

switch(

opr)
{
case '+':
System.out.println(a+b);
break;
case '-':
System.out.println(a-b);
break;
case '*':
System.out.println(a*b);
break;
case '/':
System.out.println(a/b);
break;
default:
System.out.println("无法运算,符号不支持");
break;
}


}
}


class SwitchTest
{
public static void main(String[] args)
{


/*
用户输入的数据对应的出星期。
*/
int week = 1;
switch(week)
{
default:
break;
case 1:
System.out.println(week+"对应的是星期一");
break;
case 2:
System.out.println(week+"对应的是星期二");
//break;
//...以此类推。
}

/*
季节的示例。
*/
int month = 13;

switch(month)
{
case 3:
case 4:
case 5:
System.out.println(month+"月对应的是春季");
break;
case 6:
case 7:
case 8:
System.out.println(month+"月对应的是夏季");
break;
case 9:
case 10:
case 11:
System.out.println(month+"月对应的是秋季");
break;
case 12:
case 1:
case 2:
System.out.println(month+"月对应的是冬季");
break;
default:
System.out.println(month+"月没有对应的季节");
//break;
}

// System.out.println("Hello World!");
}
}




day03
学习结果:
1,逻辑运算符什么时候用?


2,&和&&有什么区别?


3,如何理解位运算符中 & 和 |,^,你认为什么时候会用到?

4,>> <<移位运算符的特点是什么?什么时候用?


5,三元运算符和if else语句的区别?使用场景?


6,if和switch和循环结构的应用场景。


7,switch语句都有哪些特点?


8,for和while的区别。


9,局部代码块有什么用?什么时候用?






class Test
{
public static void main(String[] args)
{
/*
3000米长的绳子,每天减一半,问,需要多少天,绳子会小于5米?
思路:
1,绳子长度在变化,定义变量记录绳子长度的变化,而且这个变量的初始化值时3000.
既然不考虑小数部分,所以可以定义int类型变量
2,多少天,不确定,是一个变量.
3,每天减一半,这时对绳子长度的运算,只要可以减一半,就算一天。
这就让我想到了一个小思想,就是传说中的计数器思想。
4,天数的递增,减一半的动作在重复的执行,使用循环结构。
5,直到小于5米,重复动作结束。这就是循环的条件。

*/

int day = 0;

for(int x=3000;x>=5; x = x/2)
{
day++;

}

System.out.println("day="+day);



System.out.println("Hello World!");


//将这个运算过程画出来。 你从这个代码运算规律中发现了什么?总结了什么思想?


for(int x=0; x<3; x++)
{
for(int y=0; y<5; y++)
{
System.out.print("*");


}
System.out.println();
}
}
}


class WhileDemo
{
public static void main(String[] args)
{

/*
while(条件表达式)
{
执行语句;
}

*/
int x = 1;
while(x<3);
{
System.out.println("x="+x);

x++;
}


}
}


class WhileTest
{
public static void main(String[] args)
{

/*

练习:
获取1到10 10个数字的和。

0 + 1
1 + 2
3 + 3
6 + 4
10 + 5
思路:
1,每次参与加法的数值不确定。
2,每次的出现的和数据也不确定。
3,发现参与加法运算的数值有递增规律。
4,每一次都是加法运算在重复,并且都是和再加上下一个数值。

步骤:
1,定义一个变量,记录住参与加法运算的数据。
2,定义一个变量,记录中每一次的出现的和。
3,对于记录参与加法运算的数据进行自增。
4,因为加法运算需要重复,就要想到循环结构。
*/


//累加思想。

int x = 1;//记录参与加法的数据。
int sum = 0;//记录住每一次的和。
while(x<=100)
{
sum = sum + x;

x++;
}

System.out.println("sum="+sum);

}
}



class WhileTest2
{
public static void main(String[] args)
{

/*
练习:
1~100之间 6的倍数出现的次数。
要求:自己独立完成思想的书写。和步骤的文字描述。


*/

// 计数器思想
int x = 1;
int count = 0;
while(x<=100)
{
if(x%6==0)
count++;
// System.out.println("x="+x);
x++;
}

System.out.println("count="+count);
}
}

相关文档
最新文档