You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现符合规则的Java全名输入与条件终止程序?

Complete Implementation for Full Name Input Program

Got it, let's build this out properly. Below is a complete, commented version of the main method that meets all your requirements, plus breakdowns of the key logic:

import java.util.ArrayList;
import java.util.Scanner;

public class FullNameInput {
    public static void main(String[] args) { 
        Scanner keyboard = new Scanner(System.in); 
        System.out.println("Enter up to 10 full names with up to 120 characters and at least two names with at least 4 characters each.\nEnter 'end' to stop once you've entered at least 5 valid names.");
        
        // Store all valid full names as we collect them
        ArrayList<String> validNames = new ArrayList<>();
        
        // Run until we hit 10 valid names or user terminates early (with ≥5 names)
        while (validNames.size() < 10) {
            System.out.print("\nEnter a full name (or 'end' to terminate): ");
            String input = keyboard.nextLine().trim();
            
            // Handle early termination request
            if (input.equalsIgnoreCase("end")) {
                if (validNames.size() >= 5) {
                    System.out.println("Terminating program early. You've entered enough valid names.");
                    break;
                } else {
                    System.out.println("Error: You need at least 5 valid names before you can terminate. Keep going!");
                    continue;
                }
            }
            
            // First check: full name length can't exceed 120 characters
            if (input.length() > 120) {
                System.out.println("Error: Full name is too long! Max 120 characters allowed. Try again.");
                continue;
            }
            
            // Split name into individual parts (handles multiple spaces between names)
            String[] nameParts = input.split("\\s+");
            int longNameCount = 0;
            
            // Count how many name parts are 4+ characters long
            for (String part : nameParts) {
                if (part.length() >= 4) {
                    longNameCount++;
                }
            }
            
            // Second check: at least two parts meet the 4+ character requirement
            if (longNameCount >= 2) {
                validNames.add(input);
                System.out.printf("Success! Added valid name. You've entered %d out of 10.%n", validNames.size());
            } else {
                System.out.println("Error: Full name needs at least two names with 4+ characters. Try again.");
            }
        }
        
        // Wrap up by showing all collected valid names
        System.out.println("\nProgram finished. Here are your valid full names:");
        for (int i = 0; i < validNames.size(); i++) {
            System.out.printf("%d. %s%n", i + 1, validNames.get(i));
        }
        
        keyboard.close();
    }
}

Key Logic Breakdown:

  • Early Termination Guard: We check for "end" first, but only allow it if the user has already entered 5+ valid names—no skipping out early before meeting the minimum.
  • Length Check: We reject overly long inputs immediately to avoid wasting time processing invalid entries.
  • Robust Name Splitting: Using split("\\s+") ensures we handle messy input with multiple spaces (like "Jane Elizabeth Doe") without creating empty string entries.
  • Long Name Validation: We count every name part that's 4+ characters long, and only accept the full name if this count hits at least 2.
  • Automatic Stop: The loop will exit on its own once we hit 10 valid names, as required.

内容的提问来源于stack exchange,提问作者Ruca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:26:53