BLOG

【プログラマー 研修 Java】ふじの記録1

こんにちは~

11/1より入社しました、ふじです。

文章を書くことに苦手意識を持っていますが頑張って書いていこうと思います。

では早速本日の研修内容を書いていこうと思います~

変数-算術演算子-キャスト

まずは基本のところから。

最初の基礎の部分はちゃちゃちゃと終わらせようと思っていましたが…

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package q1;
import java.util.Scanner;
public class Q4 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("身長(cm):");
int height = scanner.nextInt();
System.out.print("体重(kg):");
double weight = scanner.nextDouble();
System.out.println("");
int bmi = (int)weight / (height * height) * 10000;
System.out.println("BMI:" + bmi);
}
}
package q1; import java.util.Scanner; public class Q4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("身長(cm):"); int height = scanner.nextInt(); System.out.print("体重(kg):"); double weight = scanner.nextDouble(); System.out.println(""); int bmi = (int)weight / (height * height) * 10000; System.out.println("BMI:" + bmi); } }
package q1;

import java.util.Scanner;

public class Q4 {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		System.out.print("身長(cm):");
		int height = scanner.nextInt();
		System.out.print("体重(kg):");
		double weight = scanner.nextDouble();
		System.out.println("");
		int bmi = (int)weight / (height * height) * 10000;
		System.out.println("BMI:" + bmi);
	}

}

これがうまくいかずにBMI値を計算したい変数bmiの値が0になってしまいました…

試行錯誤しても、わからず同期入社のぐっさんに聞いたところ、

int bmi = (int)weight / (height * height) * 10000;

ここが間違えており正しくは、

int bmi = (int)(weight / (height * height) * 10000);

でした、weightのみにキャストをかけていたことが原因でした。

ちょっとのミスでも全然違う結果になってしまうので、プログラミングは本当に注意が必要です…

if文-switch文

こちらではエラーは特にでなかったのですが、冗長なコードが目立ちました。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package q2;
import java.util.Scanner;
public class Q3 {
public static void main(String[] args) {
// スコアの入力
Scanner scanner = new Scanner(System.in);
System.out.print("0から100までスコアを入力:");
int score = scanner.nextInt();
char result;
// スコアの評価
if (score >= 80) {
result = 'A';
} else if (score < 80 && score >= 60) {
result = 'B';
} else if (score < 60 && score >= 40) {
result = 'C';
} else if (score < 40 && score >= 20) {
result = 'D';
} else {
result = 'E';
}
System.out.println("スコアの評価:" + result);
}
}
package q2; import java.util.Scanner; public class Q3 { public static void main(String[] args) { // スコアの入力 Scanner scanner = new Scanner(System.in); System.out.print("0から100までスコアを入力:"); int score = scanner.nextInt(); char result; // スコアの評価 if (score >= 80) { result = 'A'; } else if (score < 80 && score >= 60) { result = 'B'; } else if (score < 60 && score >= 40) { result = 'C'; } else if (score < 40 && score >= 20) { result = 'D'; } else { result = 'E'; } System.out.println("スコアの評価:" + result); } }
package q2;

import java.util.Scanner;

public class Q3 {

	public static void main(String[] args) {
		// スコアの入力
		Scanner scanner = new Scanner(System.in);
		System.out.print("0から100までスコアを入力:");
		int score = scanner.nextInt();
		char result;
		// スコアの評価
		if (score >= 80) {
			result = 'A';
		} else if (score < 80 && score >= 60) {
			result = 'B';
		} else if (score < 60 && score >= 40) {
			result = 'C';
		} else if (score < 40 && score >= 20) {
			result = 'D';
		} else {
			result = 'E';
		}
		System.out.println("スコアの評価:" + result);
	}

}

これの冗長な部分、わかりますか?

そうなんです、if-else if- else文の条件部分が余分なんです。

if文は上から判断していくため、80以上ならA、60以上ならB、40以上ならC、20以上ならD、それ以外ならEという条件のみでいいので、

80未満、60未満、40未満、の条件はいらないんです。

そこまで考えが及ばなかったです…

まだまだ勉強が必要ですね。

 

本日は上記見出しの2つの課題まで終わりました、

次の課題も細かなミスに注意して取り組みます。

 

11月に入って、寒くなってきたので体調に気を付けていきましょう~

ではまた次回のブログでお会いしましょう~

BLOGトップへ戻る