关于文本单词长度统计程序及Do While循环退出的技术问询
Hey there! Let's work through that Do While loop issue you're hitting with your word length stats program. First, let's recap the core requirement to make sure we're aligned:
We need to read multi-line text, then output a table counting how many words exist of each length (1-letter, 2-letter, etc.), treating tokens like
'tisas a single 4-letter word (per your example).
Common Do While Loop Pitfalls (and Fixes)
Most issues with Do While loops in this scenario boil down to incorrect loop initialization, missing control variable updates, or misdefined termination conditions. Let's break down the most likely problems and how to fix them.
1. Uninitialized Control Variable
If you're starting your loop before setting the initial input value, you'll hit errors or unexpected exits. For example, this flawed VB code will fail on the first loop check:
' ❌ Bad: inputLine is uninitialized when we check the condition Dim inputLine As String Do While inputLine <> "" inputLine = InputBox("Enter line (empty to end)") ' process line Loop
Fix: Initialize the input before the loop, or use a Do...Loop While structure to handle the first input correctly:
' ✅ Good: Get input first, then loop while it's not empty Dim inputLine As String inputLine = InputBox("Enter a line of text (empty line to finish)") Do While inputLine <> "" ' Process this line (we'll cover word counting next) inputLine = InputBox("Enter next line (empty to end)") Loop ' OR use Do...Loop While for cleaner flow: Do inputLine = InputBox("Enter line (empty to end)") If inputLine <> "" Then ' Process line here End If Loop While inputLine <> ""
2. Forgetting to Update the Control Variable
If you don't re-read input inside the loop, you'll end up in an infinite loop (or exit immediately). Double-check that you're fetching new input every iteration of the loop.
3. Mishandling Word Splitting & Counting
Even if your loop works, you need to make sure you're splitting words correctly (including tokens with apostrophes like 'tis). Here's a complete example of processing each line and counting word lengths:
' Initialize a dictionary to track counts per word length Dim lengthCounts As New Dictionary(Of Integer, Integer) Dim inputLine As String Do inputLine = InputBox("Enter line (empty to end)") If inputLine <> "" Then ' Split line into words (ignore empty spaces from multiple spaces) For Each word As String In inputLine.Split({" "}, StringSplitOptions.RemoveEmptyEntries) Dim wordLen As Integer = word.Length ' Update count for this length If lengthCounts.ContainsKey(wordLen) Then lengthCounts(wordLen) += 1 Else lengthCounts(wordLen) = 1 End If Next End If Loop While inputLine <> "" ' Output the results in order (from 1-letter up to longest word) Dim maxLength As Integer = If(lengthCounts.Count > 0, lengthCounts.Keys.Max(), 0) For i As Integer = 1 To maxLength Dim count As Integer = If(lengthCounts.ContainsKey(i), lengthCounts(i), 0) Console.WriteLine($"{i}-letter words: {count}") Next
Testing this with your example phrase:
"Whether 'tis nobler in the mind to suffer"
Will give exactly the output you specified:
- 1-letter words: 0
- 2-letter words: 2
- 3-letter words: 1
- 4-letter words: 2
- 5-letter words: 0
- 6-letter words: 2
- 7-letter words: 1
Quick Checks for Your Existing Code
- Did you initialize your input variable before the loop starts?
- Are you updating the input variable inside the loop to avoid infinite loops?
- Are you splitting words correctly (not stripping apostrophes or ignoring valid tokens)?
- Is your loop condition checking for the right termination signal (e.g., empty line)?
If you can share a snippet of your current loop code, I can help pinpoint the exact issue—but the fixes above should cover most common Do While problems for this use case.
内容的提问来源于stack exchange,提问作者comp

