Help us understand the problem. What is going on with this article?

(メモ)Java for文

More than 1 year has passed since last update.

for文での繰り返し処理

ループ処理・繰り返し処理と呼ばれる処理をまとめます。
Javaでは次の3つが用意されています。

  • for文
  • while文
  • do文(do while)

記述式

  • for(初期値の設定;継続条件;値の操作){処理}

  • while(条件){処理}

  • do{処理}while(条件);

コードを書いてみよう

数字を入力し、その個数だけ*を表示するプログラム

import java.util.Scanner;

class PutKome {

  public static void main(String[] args) {
    Scanner stdIn = new Scanner(System.in);

    System.out.print("何個お米を表示しますか:");
    int n = stdIn.nextInt();

    for (int i = 0; i < n; i++) {
      System.out.print('*');
    }
    System.out.println();
  }
}

【結果】

何個*を表示しますか:5
*****

解説

  1. nの入力値を受け取る
  2. [初期値]iに0を代入する
  3. [継続条件]iがn未満?
  4. Yes:*を出力、[操作]i+1…Noになるまで繰り返す/No:改行

1566876503855.jpg

Ayaka_ramens
RubyとRailsの達人になるべく筋トレ中の ひよっこエンジニア🐥
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした