LoginSignup
1
2

More than 1 year has passed since last update.

【Java復習1日目】サンプルコードから学び直すJava入門①

Last updated at Posted at 2021-06-24

サンプルコード(Hello, World)

Hello.java
class HelloWorld {
    public static void main(String[] args) {
        String msg = "Hello, World!";

        // 標準出力ストリームを経由したコンソール表示
        System.out.println(msg);
    }
}

System.out

コンソール画面と結びつく、標準出力ストリーム(=Standard Output Stream)。

ストリーム

コンソール画面を含む、外部とのデータの入出力を行う仕組み。


サンプルコード1(Scanner)

InputNotExpected1.java
import java.util.Scanner;

class InputNotExpected1 {
    public static void main(String[] args) {
        // Scannerオブジェクト
        Scanner stdIn = new Scanner(System.in);

        System.out.print("Int x: ");
        // ユーザ入力を促し、変数xに代入
        int x = stdIn.nextInt();

        System.out.print("String y: ");
        // ユーザ入力を促し、変数yに代入
        String y = stdIn.next();

        System.out.print("String z: ");
        // ユーザ入力を促し、変数zに代入
        String z = stdIn.nextLine();

        System.out.println();

        System.out.println("x = " + x);
        System.out.println("y = " + y);
        System.out.println("z = " + z);
    }
}
実行結果
Int x:          // <- nextInt()は改行文字をカウントしない
                // 
  1             // <- nextInt()は空白文字をカウントしない、ユーザの改行入力によって改行される
String y:       // <- next()は改行文字をカウントしない
                // 
  a  b  ;       // <- 最初のカウント文字(=a)をカウントし(後述)、ユーザの改行入力によって改行される
String z:       // <- 自動でスキップされ(スキャン範囲内にカウントするべき文字が既に存在するため)、println()によって1行分改行される
x = 1           // 実行結果: x = 1
y = a           // 実行結果: y = a
z =   b  ;      // 実行結果: z =   b  ;

サンプルコード1から学ぶnext()メソッド

Scanner.next()およびScanner.next<T>()メソッドは、区切り文字(=空白タブ改行)をカウントしない(=ノーカウント文字)。
また、カウント文字が登場し、次にノーカウント文字が登場するまでカウント文字列を保持する。

さらに、スキャン範囲内のストリームに、既にカウント文字が含まれている場合、文字の入力を受け付けない

サンプルコード2(Scanner)

InputNotExpected2.java
import java.util.Scanner;

class InputNotExpected2 {
    public static void main(String[] args) {
        // Scannerオブジェクト
        Scanner stdIn = new Scanner(System.in);

        System.out.print("Int x: ");
        // ユーザ入力を促し、変数xに代入
        int x = stdIn.nextInt();

        System.out.print("String y: ");
        // ユーザ入力を促し、変数yに代入
        String y = stdIn.nextLine();

        System.out.print("String z: ");
        // ユーザ入力を促し、変数zに代入
        String z = stdIn.nextLine();

        System.out.println();

        System.out.println("x = " + x);
        System.out.println("y = " + y);
        System.out.println("z = " + z);
    }
}
実行結果
Int x:                         // <- nextInt()は改行文字をカウントしない
                               // 
  1                            // <- nextInt()は空白文字をカウントしない、ユーザの改行入力によって改行される
String y: String z:   a  b  ;  // <- "String y: "が自動でスキップされ(カーソルが存在する行に改行文字が含まれるため)、ユーザの改行入力によって最終的に改行される
                               // <- println()によって改行される
x = 1                          // 実行結果: x = 1
y =                            // 実行結果: y = (改行文字)
z =   a  b  ;                  // 実行結果: z =   a  b  ;

サンプルコード2から学ぶnextLine()メソッド

スキャナカーソルが存在する行をカウントする。

また、スキャン範囲内のストリームに、既にカウント対象(=改行文字を含む)が含まれている場合、文字の入力を受け付けない

Scanner

プリミティブ型String型のテキストを読み込むメソッドを定義するクラス。

定義

// Scannerオブジェクトの生成
Scanner(InputStream source)
// パラメータ
// source: スキャンを実行する入力ストリーム

// <T>型データの入力を読み込む
// -> 値が不適切である場合はInputMismatchExceptionが発生
<T> Scanner.next<T>()

// 区切り文字より前の入力を読み込み、スキャナのカーソルを現在の行のままにする
String Scanner.next()

// 1行分の入力を読み込み、スキャナのカーソルを次の行に送る
String Scanner.nextLine()

System.in

キーボードと結びつく、標準入力ストリーム(=Standard Input Stream)。


サンプルコード(final変数)

Circle.java
import java.util.Scanner;

class Circle {
    public static void main(String[] args) {
        // 円周率を表すfinal変数
        final double PI = 3.14159265359;

        Scanner stdIn = new Scanner(System.in);

        System.out.print("radius: ");
        double x = stdIn.nextDouble();

        double area = x * x * PI;
        System.out.println(area);
    }
}

final変数

読み込み専用(=イミュータブル)な変数。
読み書き可能(=ミュータブル)な変数と区別するため、大文字で記述する。


サンプルコード(乱数)

Rand.java
import java.util.Random;

class Rand {
    public static void main(String[] args) {
        // Randomオブジェクト
        Random rand = new Random();

        // 0-14のint型乱数
        int num = rand.nextInt(15);

        System.out.println(num);
    }
}

Random

(=シード)に対して線形合同法によって生成された疑似乱数を生成するクラス。

定義

Randomオブジェクトの生成
// シードを指定せずRandomオブジェクトを生成
// -> 他のRandomオブジェクトと異なるシード値が割り当てられる
Random()

// シードを指定してRandomオブジェクトを生成
Random(long seed)
// パラメータ
// seed: シード値

疑似乱数の生成メソッド

Randomクラスメソッド 範囲
nextBoolean() boolean true/false
nextInt() int int型の表現可能範囲
nextInt(int bound) int 0 ≦ x < bound
nextLong() long long型の表現可能範囲
nextDouble() double 0.0 ≦ x < 1.0
nextFloat() float 0.0 ≦ x < 1.0

用語集

用語 内容
式(expression) 変数リテラル変数リテラルを演算子で結合したもの
識別子(identifier) 変数メソッドクラスなどに付与する名前
1
2
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
1
2