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

下载本文档

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

文档简介

1、Java 小程序代码/* 第一个程序 */public class Welcomepublic static void main(String args)这是你的第一个程序,欢迎你走入Java的大门 ");/* 学生信息导入 */class StudentTestpublic static void main(String args)Student aStudent = new Student();aStudent.setName(" 张楠 ");aStudent.setStudentNum("20030408");学生的姓名是 :"

2、 + aStudent.getName() + ",学号是 :" + aStudent.getStudentNum();class Peopleprivate 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

3、setStudentNum(String strStudentNum)studentNum = strStudentNum;/* 移位运算符测试*/public class BitMotionpublic static void main(String args)int a = 15;int b = 2;int x = a << b;int y = a >> b;int z = a >> b;/* 测试位的四种运算*/public class BitOperationpublic static void main(String args)int a = 15

4、;int b = 2;int x = a & b;int y = a | b;int z = a b;int r = x;/* 测试 boolean 型数据*/public class BooleanTestpublic static void main(String args)int a = 20;int b = 30;boolean x, y,z;x = (a > b);y = (a < b);z = (a + b) = 50);/* 测试不同数制表现形式及系统的自动转化功能*/public class ByteTestpublic static void main(S

5、tring args)byte x = 22;/十进制byte y = 022;/八进制byte z = 0X22;/十六进制转换成十进制, x=" + x);转换成十进制, y=" + y);转换成十进制, z=" + z);/* 测试 char 型与整数的转换*/public class CharTestpublic static void main(String args)char x = 'M'char y = '120'/请注意数字在输出时被转化成为字符char z = 'V'字符x=" + x)

6、;字符y=" + y);数值Z=" + (x+z);/* 常量的使用*/public class Constantspublic static void main(String args)final double CM_PER = 3.14;double radius = 5;/* 类常量的使用*/public class Constants2static final double CM_PER = 3.14;public static void main(String args)double radius = 5;/* 两个整数相除及求余数*/public class D

7、ividepublic static void main(String args)int a = 15;int b = 2;double c = 2;/* 测试 double 型数据类型*/public class DoubleTestpublic static void main(String args)double x = 22;double y = 42.6D;double z = x*y;型 x=" + x);/* 测试 float 型数值*/public class FloatTestpublic static void main(String args)float x =

8、 22.2F;float y = 42.6F;float z = x*y;/* 测试全局变量的操作*/public class GlobalVarint a = 10;double b = 20;public static void main(String args)GlobalVar globalVar = new GlobalVar();globalVar.print();public void print()/* 测试基本类型的初始化*/public class InitPrimitive1byte a;short b;int c;long d;float e;double f;char

9、 g;boolean h;public static void main(String args)InitPrimitive1 aInit = new InitPrimitive1();aInit.print();public void print()字节型,a=" + a);短整型,b=" + b);整数型,c=" + c);长整型,d=" + d);单精度型,e=" + e);双精度型,f=" + f);字符型,g=" + g);布尔型,h=" + h);/* 测试基本类型的初始化*/public class

10、InitPrimitive2public static void main(String args)InitPrimitive2 aInit = new InitPrimitive2();aInit.print();public void print()byte a;short b;int c;long d;float e;double f;char g;boolean h;字节型,a=" + a);短整型,b=" + b);整数型,c=" + c);长整型,d=" + d);单精度型,e=" + e);双精度型,f=" + f);字

11、符型,g=" + g);布尔型,h=" + h);/* 测试局部变量的操作*/public class LocalVarpublic static void main(String args)LocalVar localVar = new LocalVar();localVar.print();public void print()int a = 10;double b = 20;/* 测试局部变量的操作*/public class LocalVar2public static void main(String args)LocalVar2 localVar = new L

12、ocalVar2();localVar.print();public void print()int a = 10;double b = 20;/* 逻辑运算符测试*/public class LogicSignpublic static void main(String args)boolean x, y, z, a, b;a = 'A' > 'b'b = 'R' != 'r'x = !a;y = a && b;z = a | b;/* 关系运算符测试*/public class RelationTestp

13、ublic static void main(String args)boolean x, y, z;int a = 15;int b = 2;double c = 15;x = a > b;/true;y = a < b;/false;z = a != b;/true;/* 测试自增、自减操作*/public class SelfActionpublic static void main(String args)int x = 10;int a = x + x+;int b = x + +x;int c = x + x-;int d = x + -x;/* 短路现象测试*/pub

14、lic class ShortCircuitpublic static void main(String args)ShortCircuit a = new ShortCircuit();if( a.test1(0) && a.test2(2) && a.test3(2) elsepublic boolean test1(int value1)return value1 < 1;public boolean test2(int value2)return value2 < 2;public boolean test3(int value3)retur

15、n value3 < 3;/* 测试传址引用的实质*/public class StudentString strName;public static void main(String args)Student aStudent = new Student();/ 得到对象 Student 类的一个句柄 aStudentaStudent.setStudentName("张楠 ");Student bStudent = aStudent;/将 aStudent句柄复制给nextStudentbStudent.setStudentName("唐僧 ")

16、;String name = aStudent.getStudentName();/再看一下句柄aStudent的内容是否改变public void setStudentName(String name)strName = name;public String getStudentName()return strName;/* 强制转型测试*/public class TypeTranpublic static void main(String args)int x;double y;x = (int)22.5 + (int)34.7;/y = (double)x;/*强制转型可能引起精度丢失

17、* 测试传值引用的实质*/public class ValueReferenceint a = 10;public static void main(String args)ValueReference aValue = new ValueReference ();aValue.print();public void print()int b = a;/ 我们将 a 的值传给了/* 中断测试*/public class BreakTestpublic static void main(String args)for(int i = 1; i < 20; i+)if(i = 10)brea

18、k;/*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;/*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);/* 输出数字到控制台*/public class Circlepublic stati

温馨提示

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

评论

0/150

提交评论