赞
踩
用来解决特定的问题(实现特定的功能)的一段代码,可以反复使用。
public static void 方法名称( ){ //方法主体 }
与main方法平级
在需要调用方法的位置,直接书写方法名();即可实现调用
代码执行到调用方法的位置,会先执行方法内部的代码,再接着执行后续的代码
- package com.qfedu.test1;
- /**
- * 以两种 方式打印诗句 每句诗以后有十个横线
- * @author WHD
- *
- */
- public class Test1 {
- public static void main(String[] args) {
- System.out.println("窗前明月光");
- System.out.println("----------");
- System.out.println("疑是地上霜");
- System.out.println("----------");
-
-
-
- System.out.println("举头望明月");
- for(int i =1;i <= 10;i++) {
- System.out.print("-");
- }
- System.out.println();
-
- System.out.println("低头思故乡");
- for(int i =1;i <= 10;i++) {
- System.out.print("-");
- }
- System.out.println();
-
- }
- }

- package com.qfedu.test1;
- /**
- * 使用方法实现 横线 的打印
- * 方法的概念:用来解决特定的问题(实现特定的功能)的一段代码,可以反复使用。
- * 定义语法:public static void 方法名称(){ // 方法体}
- * 定义位置:与main方法平级
- *
- * @author WHD
- *
- */
- public class Test2 {
-
-
- public static void main(String[] args) {
- System.out.println("窗前明月光");
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。