1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package j1_java_basic.J20250525;
- /**
- * @author WanJl
- * @version 1.0
- * @title Demo07
- * @description
- * @create 2025/5/25
- */
- public class Demo07 {
- /*
- 没有参数、没有返回值的方法
- 输出hello world
- 没有参数 方法名后面的括号()里面是空的。调用方法的时候不需要传参数
- 没有返回值 方法前面的是void,表示没有返回值,也就不需要设置返回值类型。
- */
- public static void printHelloWorld(){
- System.out.println("hello world");
- }
- /*
- 有参数 需要在创建方法的时候,设置【形式参数】,在调用方法的时候需要传【实际参数】
- 没有返回值
- 输出n个hello world
- */
- public static void printHelloWorld2(int n){
- for (int i = 0; i < n; i++) {
- System.out.println("hello world");
- }
- }
- /*
- 没有参数
- 有返回值
- 定义一个方法,调用该方法会【返回】一个hello world
- 如果方法是带有返回值的,那么在调用该方法的时候,需要按照方法的返回值类型定义变量进行接收。
- */
- public static String getHelloWorld(){
- return "hello World";
- }
- /*
- 有参数
- 有返回值
- 定义一个加法运算的方法,传入两个整数,返回这两个整数的和
- */
- public static int add(int a,int b){
- int sum=a+b;
- return sum;
- }
- public static void main(String[] args) {
- printHelloWorld2(5);
- //调用了方法,需要接收方法运行后的返回值结果
- String h=getHelloWorld();
- System.out.println(h);
- int s = add(15, 7);
- System.out.println(s);
- }
- }
|