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

------- 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 stub
		int 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+"秋季");
		else
			System.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 stub
		for(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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值