《Java入门基础代码》由会员分享,可在线阅读,更多相关《Java入门基础代码(10页珍藏版)》请在金锄头文库上搜索。
1、例 1-1 DoubleVariableDemo.javaclass DoubleVariableDemopublic static void main(String args)double dRadius=2.0d;double dArea=Math.PI*dRadius*dRadius;System.out.println(半径为 2 的圆的面积:+dArea); 例 1-2 BooleanConstantDemo.javaclass BooleanConstantDemopublic static void main(String args)final boolean BFLAG=tru
2、e;if(BFLAG)System.out.println(Always Print Out Me!);elseSystem.out.println(Never Print Out Me!);例 1-3 DecisionDemo.javaclass DecisionDemopublic static void main(String args)short x=1,y=2,z;/ 错误代码/ z = x + y;/ 正确代码z=(short)(x+y);System.out.println(z);例 1-4 IntegerConstantDemo . javaclass IntegerConst
3、antDemopublic static void main(String args)final byte nMaxValue_1=100;final short nMaxValue_2=200;final int nMaxValue_3=300;final long nMaxValue_4=400;System.out.println(整数常量的 nMaxValue_1 值+nMaxValue_1);System.out.println(整数常量的 nMaxValue_2 值+nMaxValue_2);System.out.println(整数常量的 nMaxValue_3 值+nMaxVa
4、lue_3);System.out.println(整数常量的 nMaxValue_4 值+nMaxValue_4);/ nMaxValue_1 = nMaxValue_1 + 1; 不能更改常量的值例 1-5 DataConversionDemo.javaclass DataConversionDemopublic static void main(String args)int nOp1=2;float fOp2=2.25f;float fResult=nOp1*fOp2;System.out.println(混合运算结果:+fResult);例 1-6 BasicMath.javacla
5、ss BasicMathpublic static void main(String args)System.out.println(Integer Arithmetic);int a=1+1;int b=a*3;int c=b/4;int d=c-a;System.out.println(c = +c);System.out.println(d = +d);System.out.println(nFloating Point Arithmetic);double da=1+1;double db=da*3;double dc=db/4;double dd=dc-a;System.out.pr
6、intln(dc = +dc);System.out.println(dd = +dd);int x = 42;double y = 42.25;System.out.println(x mod 10 = + x % 10);System.out.println(y mod 10 = + y % 10);例 1-7 BooleanOperatorDemo.javaclass BooleanOperatorDemopublic static void main(String args)int nOperator1=5;int nOperator2=10;if(nOperator14)&(nOpe
7、rator29)System.out.println(nOperator1 大于 4 并且 nOperator2 大于 9);elseSystem.out.println(逻辑关系判断不成立);例 1-8 BoolLogic.javaclass BoolLogicpublic static void main(String args)boolean a=true;boolean b=false;boolean c=a|b;boolean d=a&b;boolean e=ab;boolean f=(!a&b)|(a&!b);System.out.println( a|b = +c);System
8、.out.println( a&b = +d);System.out.println( ab = +e);System.out.println(!a&b|a&!b = +f);例 1-9 MultByTwo.javaclass MultByTwopublic static void main(String args)int i;int num=0xFFFFFFE;for(i=0;i60)System.out.println(他是老年人);elseSystem.out.println(他不是老年人);if(sex.equals(男)System.out.println(他的性别是男);elseS
9、ystem.out.println(她的性别是女);System.out.println(我在学习控制语句);例 1-11 SwitchDemo.javaclass SwitchDemopublic static void main(String args)int x=(int)(Math.random()*10);System.out.println(随机数 x 放大 10 倍后的整数值 :+x);switch(x)case 0:System.out.println(case 0:+x);break;case 1:System.out.println(case 1:+x);break;def
10、ault:System.out.println(default:+x);break;例 1-12 Menu.javaclass Menupublic static void main(String args) throws java.io.IOExceptionchar choice;doSystem.out.println(Help on:);System.out.println( 1. if);System.out.println( 2. switch);System.out.println( 3. while);System.out.println( 4. do-while);Syste
11、m.out.println( 5. forn);System.out.println(Choose one:);choice=(char)System.in.read();while(choice5);System.out.println(n);switch(choice)case 1:System.out.println(The if:n);System.out.println(if(condition) statement;);System.out.println(else statement;);break;case 2:System.out.println(The switch:n);
12、System.out.println(switch(expression) );System.out.println( case constant:);System.out.println( statement sequence);System.out.println( break;);System.out.println( / .);System.out.println();break;case 3:System.out.println(The while:n);System.out.println(while(condition) statement;);break;case 4:Syst
13、em.out.println(The do-while:n);System.out.println(do );System.out.println( statement;);System.out.println( while (condition););break;case 5:System.out.println(The for:n);System.out.print(for(init; condition; iteration);System.out.println( statement;);break;例 1-13 ForDemo.javaclass ForDemopublic stat
14、ic void main(String args)int nSum,nCounter;for(nCounter=0,nSum=0;nCounter2000)break;elsenCounter+;nSum=nSum+nCounter;System.out.println(累加结果:+nSum);例 1-15 Break.javaclass Breakpublic static void main(String args)boolean t=true;first:second:third:System.out.println(Before the break.);if(t)break secon
15、d; System.out.println(This wont execute);System.out.println(This wont execute);System.out.println(This is after second block.);例 1-16 ContinueLabel.javaclass ContinueLabelpublic static void main(String args)outer:for(int i=0;ii)System.out.println();continue outer;System.out.print( +(i*j);System.out.println();例 1-17 UseMethod.javaclass Bint i,j;B(int a,int b)i=a;j=b;B(int a)i=a;j=a;void show(String a)System.out.println(a);int show(int c)System.out.println(c);class UseMeth