0
0

More than 1 year has passed since last update.

javaのメソッド

Last updated at Posted at 2023-01-26

メソッドとは

  • コードを分割&整理、共通で利用できる機能
  • 使用する場面:大規模なプログラムを開発する時
  • コード名付けることが可能
  • 何度でもコードを呼び出し可能
  • コードは組み合わせ可能
  • public static voidでメソッドを作ることが可能。
    これをmainメソッド内で呼び出し可能。
     public static void メソッド名(){処理内容}

使用例

import java.util.*;
public class Main{
    public static void main(String[] args){
        int a=(int)(Math.random()*10+1);
        System.out.println("リンゴを"+a+"個買った");
        //メソッドはMath.random()とSystem.out.println()
    }
}

System.out.println(引数)
メソッド内で処理した結果は戻り値として変数aに使える。

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