LoginSignup
0
0

More than 3 years have passed since last update.

型の違う値を型変換して別の変数に代入する方法

Last updated at Posted at 2020-01-26

文法

データ型 変数名 = (データ型) 代入される側の変数名

※型指定/型キャストをする場合、右辺のデータ型を括弧で括る必要があります。忘れがちなので注意(戒め)。

public class Main {
    public static void main(String[] args) {

        double rand = Math.random(); * 100;

        //変数に変数を代入
        int number = (int)rand;

        System.out.println(number);
    }
}

おまけ

変数定義と同時にMath関数を代入することも可能

その場合は、()でくくる。

int game_num = (int)(Math.random() * 10 + 1);
System.out.println("ゲームの本数は" + game_num + "個");
0
0
2

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