ここでは、実務で使える do...while ループのテンプレートを、
- Javaクラス構造
- メニュー・リトライ・入力のパターン
を組み合わせて整理します。
そのままコピーして使いやすい形です。
実務向け do...while テンプレート(Java)
package com.example.util;
import java.util.Scanner;
/**
* 実務向け do...while テンプレート
* - メニュー処理
* - 入力バリデーション
* - リトライ処理
*/
public class DoWhileTemplate {
private Scanner sc = new Scanner(System.in);
// -----------------------------
// メニュー処理パターン
// -----------------------------
public void runMenuLoop() {
int choice;
do {
printMenu();
choice = sc.nextInt();
sc.nextLine(); // 改行消費
handleMenuChoice(choice);
} while (choice != 0);
}
private void printMenu() {
System.out.println("===== メニュー =====");
System.out.println("1: 表示");
System.out.println("2: 保存");
System.out.println("0: 終了");
System.out.println("==================");
System.out.print("選択してください: ");
}
private void handleMenuChoice(int choice) {
switch (choice) {
case 1:
System.out.println("データを表示しました。\n");
break;
case 2:
System.out.println("データを保存しました。\n");
break;
case 0:
System.out.println("終了します。\n");
break;
default:
System.out.println("無効な番号です。\n");
}
}
// -----------------------------
// 入力バリデーションパターン
// -----------------------------
public int getValidatedInput(String prompt, int min, int max) {
int value;
do {
System.out.print(prompt);
value = sc.nextInt();
sc.nextLine(); // 改行消費
if (value < min || value > max) {
System.out.println("入力値が範囲外です。もう一度入力してください。\n");
}
} while (value < min || value > max);
return value;
}
// -----------------------------
// リトライ処理パターン
// -----------------------------
public boolean executeWithRetry(RetryableTask task, int maxRetry) {
int attempt = 0;
boolean success = false;
do {
attempt++;
try {
task.run();
success = true;
} catch (Exception e) {
System.err.println("処理失敗: " + e.getMessage());
if (attempt < maxRetry) {
System.out.println("再試行します... (" + attempt + "/" + maxRetry + ")\n");
}
}
} while (!success && attempt < maxRetry);
return success;
}
// -----------------------------
// リトライ用のタスクインターフェース
// -----------------------------
public interface RetryableTask {
void run() throws Exception;
}
// -----------------------------
// Scannerクローズ
// -----------------------------
public void close() {
sc.close();
}
// -----------------------------
// メイン(テスト用)
// -----------------------------
public static void main(String[] args) {
DoWhileTemplate template = new DoWhileTemplate();
// メニュー処理
template.runMenuLoop();
// 入力バリデーション
int age = template.getValidatedInput("年齢を入力(1〜120): ", 1, 120);
System.out.println("入力された年齢: " + age + "\n");
// リトライ処理
template.executeWithRetry(() -> {
// 擬似的に失敗する処理
if (Math.random() < 0.7) {
throw new Exception("ランダム失敗");
}
System.out.println("処理成功!");
}, 3);
template.close();
}
}
Javaテンプレート解説
| パターン | メソッド | 特徴 |
|---|---|---|
| メニュー | runMenuLoop() | 選択肢を表示してループ、0 で終了 |
| 入力チェック | getValidatedInput(prompt, min, max) | 範囲チェック付きの入力、最初の入力を必ず受ける |
| リトライ | executeWithRetry(task, maxRetry) | 任意処理を最大 maxRetry 回まで再試行、例外処理対応 |
| 汎用 | RetryableTask | ラムダ式で処理を渡せるので柔軟 |
実務で便利なポイント
- テンプレート化しておくと、CLIツールやテストプログラムで毎回
do...whileを書く必要がない - リトライ処理はラムダで渡せるので、通信・ファイル・DBなど色んな処理で使える
- 入力バリデーションは最小限のチェックで再利用可能
- Scannerのクローズもまとめて管理できる
