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

Java新手求助:如何实现句子的三项额外验证规则?

Java Sentence Validation: Adding Your Three New Rules

Hey there! Let's get those three extra validation rules added to your existing Java program. I'll break down each rule's implementation step by step, then show you the full updated code with everything integrated nicely.

Let's go through each new rule:

  • 数字占比低于15%: We need to count how many characters in the sentence are digits, then calculate what percentage that is of the total characters. Make sure to do floating-point division here so we get an accurate percentage.
  • 元音最多9个: Vowels include both uppercase and lowercase a, e, i, o, u. We'll loop through each character in the sentence and count how many match these.
  • 单词最多12个: Split the sentence into words (handling multiple spaces with a regex), then check if the number of words is 12 or fewer.

Updated Code with All Rules

Here's your modified code with all six validation rules (your original three plus the new three) included. I added comments to explain each new part:

import java.util.Scanner;

public class Exercise5 {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Write a sentence:");
        String sent = scan.nextLine();

        // Original validation rules
        boolean startsWithAn = sent.startsWith("An");
        boolean containsApple = sent.contains("apple");
        boolean endsWithDigit = sent.length() > 0 && Character.isDigit(sent.charAt(sent.length() - 1));

        // New validation rule 1: 数字占比低于15%
        int digitCount = 0;
        for (char c : sent.toCharArray()) {
            if (Character.isDigit(c)) {
                digitCount++;
            }
        }
        boolean digitPercentageValid = sent.length() == 0 || ((double) digitCount / sent.length()) < 0.15;

        // New validation rule 2: 元音最多9个
        int vowelCount = 0;
        String vowels = "aeiouAEIOU";
        for (char c : sent.toCharArray()) {
            if (vowels.indexOf(c) != -1) {
                vowelCount++;
            }
        }
        boolean vowelCountValid = vowelCount <= 9;

        // New validation rule 3: 单词最多12个
        // Split on one or more whitespace characters to handle multiple spaces
        String[] words = sent.split("\\s+");
        boolean wordCountValid = words.length <= 12;

        // Check all rules together
        if (startsWithAn && containsApple && endsWithDigit && digitPercentageValid && vowelCountValid && wordCountValid) {
            System.out.println("The sentence is correct.");
        } else {
            System.out.println("Wrong.");
        }

        scan.close(); // Don't forget to close the scanner!
    }
}

A few quick notes:

  • I separated each validation into its own boolean variable to make the code easier to read and debug. If you ever need to adjust a rule, you can find it quickly.
  • Added checks for empty sentence length in endsWithDigit and digitPercentageValid to avoid errors if someone enters nothing.
  • Used Character.isDigit() instead of comparing character values directly—it's more readable and standard Java practice.
  • Closed the Scanner at the end to clean up resources properly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 12:52:28