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?

More than 3 years have passed since last update.

【Java】PaizaのJava入門編(6)まで終えて重要だと感じた内容まとめ。

Last updated at Posted at 2021-10-18

##乱数の生成 Math.random();

public class Lesson036{
    public static void main(String args[]){
        //randomメソッドは実数を生成するのでdouble型の変数を宣言
        double d;
        //randomメソッドで生成した実数を変数dに代入
        d = Math.random();
        //出力
        System.out.println(d);
    }
}

##整数を生成する場合 (int)(Math.random()*10)

public class Lesson037{
    public static void main(String args[]){
        //int型変数iを宣言
        int i;
        //randomメソッドで0以上10未満の整数を生成
        i = (int)(Math.random()*10);
        //出力
        System.out.println(i);
    }
}

#代入演算子

class JSample10_1{
  public static void main(String[] args){
    int num;

    num = 10;
    System.out.println(num);
    num += 5;
    System.out.println(num);//15

    num = 8;
    System.out.println(num);
    num *= 4;
    System.out.println(num);//32
  }

#配列 データ型[] 配列変数名 = new データ型[要素数]

int [] num = {1,2,3,4,5,};
int[] num = new int[5];
class JSample2_1{
  public static void main(String[] args){
    int[] result = new int[3];

    result[0] = 75;
    result[1] = 88;
    result[2] = 82;

    for (int i = 0 ; i < 3 ; i++){
      System.out.println(result[i]);
    }
  }
}

#拡張for文 for (データ型 変数名: コレクション)

String name[] = {"Suzuki", "Katou", "Yamada"};

for (String str: name){
  System.out.println(str);
}

#標準入力 Scanner scan = new Scanner(System.in);

import java.util.Scanner;
 
public class InputSample {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String str1 = scan.nextLine();//行入力を取得
        String str2 = scan.next();//空白文字までを取得
        int num = scan.nextInt();//数値を取得
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(num);
        scan.close();
    }
}

#戻り値

class JSample5_1{
  public static void main(String args[]){
    int kekka;

    kekka = bai(9);
    System.out.println(kekka);

    kekka = bai(5);
    System.out.println(kekka);
  }

  private static int bai(int n){
    return n * 2;
  }
}
0
0
1

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?