全新java初学者实践教程(3)

2019-08-02 00:33

我们还有个问题没有说,引用类型变量怎么赋值?这个就不是很难了。举个例子:

MyDate a,b; //在内存开辟两个引用空间

a = new MyDate(); //开辟MyDate对象的数据空间,并把该空间的首地址赋给a

b = a; //将a存储空间中的地址写到b的存储空间中 如图5-1

全新java初学者实践教程6(Java SE5.0版)---- 基本语法3

作者:100jq

这节课我们又要讲语法了,这是“百家拳软件项目研究室”这部教程的第6节课,我们这个教程侧重的是实践的内容和语言的重点。在java语言中还有很多细节的东西,请参考sun公司的官方培训教程。我们这里不能一一讲述。这节课我们来给大家提供一些程序流程控制的一些例子供大家学习。计算机怎么做事情,是我们教给他的。我们用它解决实际生活中的问题,所以计算机要描述现实生活中的流程。java语言中提供了4类程序控制语句,来描述流程: 1.循环语句:while,do-while,for

2.分支语句:if-else,switch,

3.跳转语句 break,continue,label: 和return 4.异常处理语句:try-catch-finally,throw

实践:

1.循环语句 while 语句

class While {

public static void main(String args[]) { int n = 10; while(n > 0) { System.out.println(\\+ n); n--;

}

}

}

do…while 语句 class DoWhile {

public static void main(String args[]) { int n = 10; do {

System.out.println(\\+ n); n--;

} while(n > 0); } }

二者区别,do…while至少循环一次,而while的表达式要是为flase的话可以一次也不循环。再通俗一点,do…while就算是括号里的是flase,人家最少也能do一次。

for语句 class ForTick {

public static void main(String args[]) { int n;

for(n=10; n>0; n--)

System.out.println(\\+ n); } }

与上面那两个的区别,for循环执行的次数是可以在执行之前确定

的。通俗一点说吧,看这个例子 for(n=10; n>0; n--)就是在括号里的时候,就已经知道要循环10次了。

还有啊,for循环的部分可以为空的 class ForVar {

public static void main(String args[]) {

int i;

boolean done = false; i = 0;

for( ; !done; ) {

System.out.println(\is \+ i); if(i == 10) done = true; i++; } }

} 循环语句的例子下载

2.分支语句 if/else语句

class IfElse {

public static void main(String args[]) {

int month = 4; // April

String season;

if(month == 12 || month == 1 || month == 2)

season = \

else if(month == 3 || month == 4 || month == 5)

season = \

else if(month == 6 || month == 7 || month == 8)

season = \

else if(month == 9 || month == 10 || month == 11)

season = \ else

season = \Month\

System.out.println(\is in the \+ season + \

} }

//这段程序输出:

//April is in the Spring. // 注意 “||”是或运算

switch语句 class Switch {

public static void main(String args[]) {

int month = 4; String season; switch (month) { case 12: case 1: case 2:

season = \ break; case 3: case 4: case 5:

season = \ break; case 6: case 7: case 8:

season = \ break; case 9: case 10: case 11:

season = \ break; default:

season = \Month\ }

System.out.println(\is in the \+ season + \

}

} 分支语句代码下载

switch语句适合于条件非常多的逻辑

请看上述语句可以混合使用,请看下载例子

循环语句的例子下载 分支语句代码下载 综合使用的代码下载

如有问题请登陆论坛

全新java初学者实践教程7(Java SE5.0版)---- 基本语法4

作者:100jq

昨天我们说有4类程序控制语句,但是才讲了2个。今天讲跳转语句。异常处理语句我们找一节专题来讲。

循环跳转语句 :break [label] //用来从语句、循环语句中跳出。 continue [label] //跳过循环体的剩余语句,开始下一次循环。

这两个语句都可以带标签(label)使用,也可以不带标签使用。标签是出现在一个语句之前的标识符,标签后面要跟上一个冒号(:),标签的定义如下: label:statement; 实践:

1、 break语句 class Break {

public static void main(String args[]) { boolean t = true; first: { second: { third: {

System.out.println(\the break.\

if(t) break second; // break out of second block System.out.println(\won't execute\ }

System.out.println(\won't execute\ }

System.out.println(\is after second block.\ } } }

// 跳出循环

class BreakLoop {

public static void main(String args[]) { for(int i=0; i<100; i++) {

if(i = = 10) break; // terminate loop if i is 10 System.out.println(\\+ i); }

System.out.println(\complete.\ }

} 5个break跳出循环的例子下载 //跳出switch

class SampleSwitch {

public static void main(String args[]) { for(int i=0; i<6; i++) switch(i) { case 0:

System.out.println(\is zero.\ break; case 1:

System.out.println(\is one.\ break; case 2:

System.out.println(\is two.\ break; case 3:

System.out.println(\is three.\ break; default:

System.out.println(\is greater than 3.\ } }

} 这个在昨天的分支语句中,我们就已经学到了。

2、 continue语句 class Continue {

public static void main(String args[]) { for(int i=0; i<10; i++) { System.out.print(i + \\ if (i%2 = = 0) continue; System.out.println(\ } } }


全新java初学者实践教程(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:冠词语法及练习题附答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: