黑马程序员—java基础_if、循环语句练习以及函数

来源:互联网 发布:redis session共享 php 编辑:程序博客网 时间:2024/06/10 01:51

------- android培训、java培训、期待与您交流! ----------

1.根据用于指定月份,打印该月份所属的季节。3、4、5—春季;6、7、8—夏季;9、10、11—秋季;12、1、2—冬季

public class IfTest {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubint x=3;if(x>12 || x<1)System.out.println(x+"月份不存在");else if(x>=3 && x<+5)System.out.println(x+"春季");else if(x>=6 && x<=8)System.out.println(x+"夏季");else if(x>=9 && x<=11)System.out.println(x+"秋季");elseSystem.out.println(x+"冬季");}}

2.打印图形:

*****

****

***

**

*

public class ForDemo {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stub//大圈套小圈for(int x=0;x<5;x++)//外循环控制行数{for(int y=x;y<5;y++)//内循环控制列数System.out.print("*");System.out.println();//换行}}}

3.打印99乘法表

public class ChengFaBiao {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubfor(int x=1;x<=9;x++){for(int y=1;y<=x;y++)System.out.print(y+"*"+x+"="+y*x+"\t");System.out.println();}}}


while和do while区别:

while先判断条件,只有条件满足才执行循环

do while先执行循环体,在判断条件。


4.函数

(1)定义格式:

修饰符  返回值类型  函数名(参数类型 形式参数1,参数类型 形式参数2,……)

执行语句;

return 返回值;

当函数运算后,没有具体的返回值时,用特殊的关键字void表示。

当函数的返回值类型是void时,函数中的return语句可以省略。

(2)函数的重载:

当定义的功能相同,但参与运算的未知内容不同,就可以定义一个函数名称以表示该功能,方便阅读,而通过参数列表的不同来区分多个同名函数。

练习:以下哪些与void show(int a,char b,double c){}重载?

1.void show(int x,char y,double z){}与原函数相同。

2.int show(int a,char b){}重载了,因为参数类型不同,重载和返回值类型没关系。

3.double show(int a,char b,double c){}这个函数不可以和给定函数同时存在一个类中。


0 0
原创粉丝点击