Java小程序代码_第1页
Java小程序代码_第2页
Java小程序代码_第3页
Java小程序代码_第4页
Java小程序代码_第5页
已阅读5页,还剩18页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Java小程序代码/*第一个程序*/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(学生的姓名是:+ aS

2、tudent.getName() + ,学号是:+ aStudent.getStudentNum();class People(private String name;public String getName()return name;)public void setName(String strName)name = strName;)class Student extends Peopleprivate String studentNum;public String getStudentNum()return studentNum;)public void setStudentNum(S

3、tring strStudentNum)studentNum = strStudentNum;)/*移位运算符测试*/public class BitMotion public static void main(String args)(int a = 15;int b = 2;int x = a b;int z = a b;System.out.println(a + + b + = + y);System.out.println(a + + b + = + z);)/*测试位的四种运算*/public class BitOperation(public static void main(S

4、tring args)(int a = 15;int b = 2;int x = a & b;int y = a | b;int z = a A b;int r = -x;System.out.println(a + & + b + = + x);System.out.println(a + | + b + = + y);System.out.println(a + A + b + = + z);System.out.println(+ x + = + r);)/*测试boolean型数据*/public class BooleanTest(public static void main(St

5、ring 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.println(x = + x);System.out.println(y = + y);System.out.println(z = + z);)/*关系运算符测试*/public class RelationTest(public static void main(String args)(boolean x, y, z;int a = 15;int b = 2

6、;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);)/* 测试自增、自减操作* /public class SelfAction(public static void main(String args)(int x = 10;int a = x + x+;System.out.println(a= + a);System.out.println(x= + x

7、);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.println(d= + d);System.out.println(x= + x);/* 短路现象测试* /public class ShortCircuitpublic static void main(String args)ShortCircuit a = n

8、ew ShortCircuit();if( a.test1(0) & a.test2(2) & a.test3(2)System.out.println(the statement is true!); elseSystem.out.println(the statement is false!);public boolean test1(int value1)System.out.println(test1 ( + value1 + );System.out.println(result: + (value1 1);return value1 1;public boolean test2(i

9、nt 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 3;)/*测试传址引用的实质*/public class Student(String strName;public static vo

10、id main(String args)(Student aStudent = new Student();/得到对象 Student 类的一个句柄aStudentaStudent.setStudentName(张楠)System.out.println(aStudent name is + aStudent.getStudentName();Student bStudent = aStudent;/ 将 aStudent 句柄复制给 nextStudentbStudent.setStudentName(唐僧)System.out.println(bStudent name is + bStu

11、dent.getStudentName();String name = aStudent.getStudentName();/ 再看一下句柄 aStudent 的内 容是否改变System.out.println(after bStudent the aStudent name is + name);)public void setStudentName(String name)(strName = name;public String getStudentName()(return strName;)/* 强制转型测试* /public class TypeTran(public stati

12、c void main(String args)(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 ValueReference(int a = 10;public static void main(String args)(ValueReference aValue = new ValueReference ();aValue.print(

13、);public void print()(int b = a;/ 我们将a的信传给了 aSystem.out.println(before changed value a = + a + , b = + b);a=30;System.out.println(after changed value a = + a + , b = + b);/*中断测试*/public class BreakTest(public static void main(String args)(for(int i = 1; i 20; i+)(if(i = 10)break;System.out.print( +

14、i);)System.out.println(n the Reptation is over!);)/*while循环限制结构的测试*/public class BuyHousepublic static void main(String args)final double HOUSEFUND = 200000;double salary = 2000;double fund = 0;int years = 1;while (fund HOUSEFUND)fund += salary*0.05*12;years+;salary = salary * 1.1;)System.out.printl

15、n(the total years is: + years);System.out.println(the total fund is : + fund);)/*while循环限制结构的测试*/public class BuyHouse2public static void main(String args)final double HOUSEFUND = 200000;double salary = 2000;double fund = 0;int years = 1;dofund += salary*0.05*12;years+;salary = salary *1.1;)while (fund HOUSEFUND);System.out.println(the total years is: + years);System.out.println(the total fund is : + fund);)/*输出数字到限制台*/public class Circlepublic static void main(String args)for( int i = 1; i = 10; i+)System.out.print( + i);/*逗号运算符*/public class CommaOperatorpublic static void m

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论