《2022年Java_小程序代码 .pdf》由会员分享,可在线阅读,更多相关《2022年Java_小程序代码 .pdf(30页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、/* 第一个程序 */ public class Welcome public static void main(String args) System.out.println( 这是你的第一个程序,欢迎你走入Java的大门); /* 学生信息导入 */ class StudentTest public static void main(String args) Student aStudent = new Student(); aStudent.setName(张楠); aStudent.setStudentNum(20030408); System.out.println(学生的姓名是:
2、+ aStudent.getName() + ,学号是: + aStudent.getStudentNum(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 30 页 - - - - - - - - - class People private String name; public String getName() return name; public void setName(String strName) name = strName; class Stude
3、nt extends People private String studentNum; public String getStudentNum() 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 30 页 - - - - - - - - - return studentNum; public void setStudentNum(String strStudentNum) studentNum = strStudentNum; /* 移位运算符测试*/ public c
4、lass BitMotion public static void main(String args) int a = 15; int b = 2; int x = a b; int z = a b; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 30 页 - - - - - - - - - System.out.println(a + + b + = + y); System.out.println(a + + b + = + z); /* * 测试位的四种运算*/
5、public class BitOperation public static void main(String args) int a = 15; int b = 2; int x = a & b; int y = a | b; int z = a b; int r = x; System.out.println(a + & + b + = + x); System.out.println(a + | + b + = + y); System.out.println(a + + b + = + z); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - -
6、- - - - - - 名师精心整理 - - - - - - - 第 4 页,共 30 页 - - - - - - - - - System.out.println( + x + = + r); /* * 测试 boolean型数据*/ public class BooleanTest public static void main(String args) int a = 20; int b = 30; boolean x, y,z; x = (a b); y = (a b; b = R != r; x = !a; y = a & b; z = a | b; System.out.print
7、ln(x = + x); System.out.println(y = + y); System.out.println(z = + z); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 17 页,共 30 页 - - - - - - - - - /* * 关系运算符测试*/ public class RelationT est public static void main(String args) boolean x, y, z; int a = 15; int b = 2;
8、double c = 15; x = a b;/true; y = a b;/false; z = a != b;/true; System.out.println(x = + x); System.out.println(y = + y); System.out.println(z = + z); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 18 页,共 30 页 - - - - - - - - - /* * 测试自增、自减操作*/ public class SelfActio
9、n public static void main(String args) int x = 10; int a = x + x+; System.out.println(a= + a); System.out.println(x= + x); int b = x + +x; System.out.println(b= + b); System.out.println(x= + x); int c = x + x-; System.out.println(c= + c); System.out.println(x= + x); int d = x + -x; System.out.printl
10、n(d= + d); System.out.println(x= + x); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 19 页,共 30 页 - - - - - - - - - /* * 短路现象测试*/ public class ShortCircuit public static void main(String args) ShortCircuit a = new ShortCircuit(); if( a.test1(0) & a.test2(2) & a.test3
11、(2) System.out.println(the statement is true!); else System.out.println(the statement is false!); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 20 页,共 30 页 - - - - - - - - - public boolean test1(int value1) System.out.println(test1 ( + value1 + ); System.out.println
12、(result: + (value1 1); return value1 1; public boolean test2(int value2) System.out.println(test2 ( + value2 + ); System.out.println(result: + (value2 2); return value2 2; public boolean test3(int value3) System.out.println(test3 ( + value3 + ); System.out.println(result: + (value3 3); return value3
13、 3; 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 21 页,共 30 页 - - - - - - - - - /* * 测试传址引用的实质*/ public class Student String strName; public static void main(String args) Student aStudent = new Student();/ 得到对象 Student 类的一个句柄aStudent aStudent.setStudentName(张楠); Sys
14、tem.out.println(aStudent name is + aStudent.getStudentName(); Student bStudent = aStudent;/将 aStudent 句柄复制给 nextStudent bStudent.setStudentName(唐僧); System.out.println(bStudent name is + bStudent.getStudentName(); String name = aStudent.getStudentName();/再看一下句柄 aStudent的内容是否改变System.out.println(afte
15、r bStudent the aStudent name is + name); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 22 页,共 30 页 - - - - - - - - - public void setStudentName(String name) strName = name; public String getStudentName() return strName; /* * 强制转型测试*/ public class TypeTran public sta
16、tic void main(String args) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 23 页,共 30 页 - - - - - - - - - int x; double y; x = (int)22.5 + (int)34.7;/强制转型可能引起精度丢失y = (double)x; System.out.println(x = + x); System.out.println(y = + y); /* * 测试传值引用的实质*/ public class Valu
17、eReference int a = 10; public static void main(String args) ValueReference aValue = new ValueReference (); aValue.print(); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 24 页,共 30 页 - - - - - - - - - public void print() int b = a;/我们将 a 的值传给了 a System.out.println(bef
18、ore changed value a = + a + , b = + b); a=30; System.out.println(after changed valuea = + a + , b = + b); /* * 中断测试*/ public class BreakTest public static void main(String args) for(int i = 1; i 20; i+) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 25 页,共 30 页 - - -
19、 - - - - - - if(i = 10) break; System.out.print( + i); System.out.println(n the Reptation is over!); /* *while 循环控制结构的测试*/ public class BuyHouse public static void main(String args) final double HOUSEFUND = 200000; double salary = 2000; double fund = 0; int years = 1; while (fund HOUSEFUND) 名师资料总结 -
20、 - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 26 页,共 30 页 - - - - - - - - - fund += salary*0.05*12; years+; salary = salary * 1.1; System.out.println(the total years is: + years); System.out.println(the total fund is : + fund); /* *while 循环控制结构的测试*/ public class BuyHouse2 p
21、ublic static void main(String args) final double HOUSEFUND = 200000; double salary = 2000; double fund = 0; int years = 1; do 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 27 页,共 30 页 - - - - - - - - - fund += salary*0.05*12; years+; salary = salary *1.1; while (fun
22、d HOUSEFUND); System.out.println(the total years is: + years); System.out.println(the total fund is : + fund); /* * 输出数字到控制台*/ public class Circle public static void main(String args) for( int i = 1; i = 10; i+) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 28 页,共 3
23、0 页 - - - - - - - - - System.out.print( + i); /* * 逗号运算符*/ public class CommaOperator public static void main(String args) for( int i = 1, j = i + 10; i 5000) System.out.println(the first condition my salary is: + salary); if (salary 4500) System.out.println(the second condition my salary is: + salary); else System.out.println(the else my salary is: + salary); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 30 页,共 30 页 - - - - - - - - -
限制150内