段階2終わりました

This commit is contained in:
2026-09-10 16:07:33 +09:00
parent a85256b39d
commit 709e454380

View File

@@ -42,9 +42,6 @@ public class StringCheck {
"↓↓↓↓↓↓ 投稿されました ↓↓↓↓↓↓" "↓↓↓↓↓↓ 投稿されました ↓↓↓↓↓↓"
}; };
// 投稿内容(前後の空白を除去したあとの文字列)を保持する変数。 // 投稿内容(前後の空白を除去したあとの文字列)を保持する変数。
// while文を抜けたあとでも使うので、while文の外で宣言しておく。 // while文を抜けたあとでも使うので、while文の外で宣言しておく。
String str = ""; String str = "";
@@ -62,18 +59,10 @@ public class StringCheck {
System.out.println(messages[0]); System.out.println(messages[0]);
str = inputString(); str = inputString();
// ---- 1日目 段階3:比較演算子で真偽値を求める ----
// 1日目の時点ではif文がまだ使えなかったため、この2つの真偽値は
// 画面に表示して目視確認するだけだった。
// 2日目からは、この真偽値をそのままif文の条件として使う。
boolean isEmpty = (str.length() == 0);
boolean isTooLong = (str.length() > 140);
// ============================================================ // ============================================================
// 2日目 段階2:if文による検証ロジック // 2日目 段階2:if文による検証ロジック
// ============================================================ // ============================================================
if (isEmpty) { if (isEmptyInput(str)) {
// 何も入力されていない(または空白のみ入力された)場合 // 何も入力されていない(または空白のみ入力された)場合
System.out.println(messages[1]); System.out.println(messages[1]);
// continue で、while文の先頭(プロンプト表示)に戻り、 // continue で、while文の先頭(プロンプト表示)に戻り、
@@ -81,7 +70,7 @@ public class StringCheck {
continue; continue;
} }
if (isTooLong) { if (isTooLongInput(str)) {
// 141文字以上入力された場合 // 141文字以上入力された場合
System.out.println(messages[2]); System.out.println(messages[2]);
// continue で、while文の先頭に戻る。 // continue で、while文の先頭に戻る。
@@ -102,10 +91,23 @@ public class StringCheck {
System.out.println(messages[3]); System.out.println(messages[3]);
System.out.println(str); System.out.println(str);
} }
public static String inputString() { public static String inputString() {
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
return scanner.nextLine().strip(); return scanner.nextLine().strip();
} }
public static boolean isEmptyInput(String str) {
boolean isEmpty = (str.length() == 0);
return isEmpty;
}
public static boolean isTooLongInput(String str) {
boolean isTooLong = (str.length() > 140);
return isTooLong;
}
} }