0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

関数を定義して呼び出す

Last updated at Posted at 2024-10-12

例1

public class Main{
    public static void main(String[] args) {
        function(); // 関数を呼び出している
    }

    // ここで関数を定義している
    public static void function() {
        System.out.println("Hello!");
    }
}

結果

Hello!

mainはmain(変えられない)
String[] argsは、配列の型と配列名を指定している

function()の部分は自分で任意の関数名を付けることができる


例2

public class Main{
    public static void main(String[] args) {
        numbers(); // 関数を呼び出している
    }

    // ここで関数を定義している
    public static void numbers() {
        int[] array = new int[]{11, 22, 33, 44};

        // arrayの中身を1つずつ出力する
        for(int value: array){
            System.out.println(value);
        }
    }
}

結果

11
22
33
44
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?