I will write again what how to use value of boolean. And Tihs time, Write about Application of If statement.
It is loop statement you like. enjoy create logic.
In order to apply the for statement. you need to learn the array.Since I will create it at a later date, please also visit here.
I think best way to learn for statement that use the for statement and use array.
So, I will issue a problem this time so please try to solve it.
see upper Questions(<問題1〜問題4>)
I will write again what how to use value of boolean. And Tihs time, Write about Application of If statement.
It is loop statement you like. enjoy create logic.
ロードマップ
Booleanの扱い方
If文の使い方
If文の使い方〜応用編〜
ループ処理〜For文〜今回
ループ処理〜For文の応用〜
ループ処理〜While文〜
ループ処理〜While文の応用〜
ループ処理〜Do While文〜
ループ処理〜Do While文の応用〜
演算子の整理
演算子の応用
まとめ
Road Map
How to use Boolean
How to use if statement
Training of if statement
How to use for statement This time
Training of for statement
How to use while statement
Training of while statement
How to use do while statement
Training of do while statement
Summary of operator
Training of operator
Summary of all
For文の使い方
まずは、文法です。早い話が、書き方です。以下の様に記述します。
for (「1.初期化」;「2.論理式」; 「3.後処理」) {
// 繰り返す処理
}
<実装サンプル>
カウンター変数(「i」のこと)が0~2までの間ループします。
// 1.「i」はカウンターです、iを0で初期化します。
// 2.「i < 3」がtrueになるならば繰り返します。
// 3.「i++」iをインクリメント(1を足す)
for (int i = 0; i < 3; i++) {
System.out.println("iの値は" + i + "です。");
}
「出力結果」
iの値は0です。
iの値は1です。
iの値は2です。
カウンター変数「i」を0で初期化します。
カウンター変数「i」が3より小さいときはTRUEになるので、もう一度ループします。
後処理でカウンター変数「i」をインクリメント、プラス1します。
これを順番にやると下のような形になります。
カウンター変数の値
論理式の結果
後処理の結果
繰り返し
i = 0
0 < 3 => TRUE
i = 1
行う
i = 1
1 < 3 => TRUE
i = 2
行う
i = 2
2 < 3 => TRUE
i = 3
行う
i = 3
3 < 3 => FALSE
i = 3
行わない
<解説>
How to use for statement
First of all, we have to learn statement. other way. how to write for statement.
for (「1.initialize」;「2.handle next loop」; 「3.post processing」) {
loop logic
}
// 1.「i」 is counter. initialize "i"
// 2.「i < 3」 return true. go to next loop
// 3. 「i++」Increment i (add 1) )
for (int i = 0; i < 3; i++) {
System.out.println("i value is " + i );
}
「result」
i value is 0
i value is 1
i value is 2
サンプルコード(sample code)
これは、プログラム引数を使用してプログラム引数を表示するものです。
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("プログラム引数は設定されていません。");
System.exit(0); // プログラムを正常終了する
}
// ループ文
for (int i = 0; i < args.length; i++) {
System.out.println("プログラム引数は" + args[i] + "です");
}
}
I will write again what how to use value of boolean. And Tihs time, Write about Application of If statement.
It will guradually become more. enjoy create logic.
ロードマップ
Booleanの扱い方
If文の使い方
If文の使い方〜応用編〜 今回
ループ処理〜For文〜
ループ処理〜For文の応用〜
ループ処理〜While文〜
ループ処理〜While文の応用〜
ループ処理〜Do While文〜
ループ処理〜Do While文の応用〜
演算子の整理
演算子の応用
まとめ
Road Map
How to use Boolean
How to use if statement
Training of if statement This time
How to use loop statement 〜While〜
Training of Loop〜While〜
How to use loop statement 〜While〜
Training of Loop〜While〜
How to use loop statement 〜Do While〜
Training of Loop〜Do While〜
Summary of operator
Training of operator
Summary
If文の使い方〜応用〜
前回は、単純なIf文、条件が1つでTrue / Flaseの判定を行いました。
今回は「If文の応用」と称してBooleanの扱い方の応用編をやろうと思います。
Application of If statement
we tried basic If statement. next it Called Application of If statement. that is application of how to use boolean
This time, we will try to make the simple program. and I will show sample program. That is using technique of I wrote in blog.
・using program argument. and if argument is string charactor then print argument to console. if argument is number charactor then print result of calculate in 「+」「-」「*」「/」.
public static void main(String[] args) {
// Check program argument
if (args.length == 2) {
// Case2
int left = Integer.parseInt(args[0]);
int right = Integer.parseInt(args[1]);
System.out.println(left + " + " + right + " = " + left + right);
System.out.println(left + " - " + right + " = " + left - right);
System.out.println(left + " * " + right + " = " + left * right);
System.out.println(left + " / " + right + " = " + left / right);
} else {
System.out.println("Please set 2 arguments");
}
}
<Question1>
Make a program to print that if it's odd number print "it's odd number" and if it's even number print "it's even number".
<Querstion2>
Make a program to calculate area of triangle using 2 program arguments.
<Question3>
Like question2 make a program to calculate area of rectangle.
<Question4>
make a program to print like a sample , using program argument and print splited string.-----------------------------------
1: first harf:XXXXX
2: second harf:XXXXX
if (<result of logical operation1>) {
// processing1
} else if (<result of logical operation2>) {
// processing2
} else {
// processing3
}
perform conditional branching as above. the value of < ... > is "True" or "False".
Is shown blow.
int num = 1;
if (num == 1) {
System.out.println("num is " + num);
} else if (num == 2) {
System.out.println("num is" + num);
} else {
System.out.println("num is not 1 and 2");
}
Valiable "num" is in1 and result of processing display "num is 1". next i try change this code like this.
public static void main(String[] args) {
int num = Integer.parseInt(args[0]);
if (num == 1) {
System.out.println("num is " + num);
} else if (num == 2) {
System.out.println("num is" + num);
} else {
System.out.println("num is not 1 and 2");
}
}
just change program argument, we can change result of processing.
問題の実装方法
問題1
上のプログラムのプログラム引数に"3"を渡してelse文の部分を表示させてください。
change upper program and use program argument of 3 show "num is not 1 and 2"
問題2
上のプログラムの「int num = ...」を「String str = 」に変更してプログラムを動くようにしてください。
でわでわ、良い日を。。。
Change A part of "int num = ..." to "String str = ..." and fix the program.