2
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 1 year has passed since last update.

java初心者が行く~Scanner~

Posted at

はじめに

java初心者が備忘録的な感じで書いています.
間違ったこと書いていたらコメントで指摘してくれると助かります.

Scanner

入力を求めるときはc言語だとscanfだけでよかったのにjavaはなんか長くねと最初は思ってた.
改善前はいちいち

int input = new java.util.Scanner(System.in).nextInt();

と書いていて正直面倒だった.
しかし複数回Scannerを使う場合以下の方法で書けば楽になる.

import java.util.Scanner;

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int input = scanner.nextInt();

なにをしているかというと,まずjava.util.Scannerをimportし,その後scannerインスタンスを生成し,コンストラクタの引数にSystem.inを指定する.最後にscannerインスタンスのnextIntメソッドを呼び出しているという流れである.

scannerが一つだけだと書く分量はあまり変わらないが,複数回各場面ではかなり有効になるのではないでしょうか,,,

2
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
2
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?