输入可被5整除数字计数逻辑及代码实现困惑问询
Fixing the Counter Logic for Your Number Input Task
Hey there! Let's work through fixing your counter issue so it correctly tracks the numbers that meet your conditions. First, let's recap what you need to achieve to make sure we're on the same page:
- Keep asking for numbers until 2 numbers divisible by 5 are entered
- If any number entered is divisible by 3, reset the counter back to 0
- Your current code reads two numbers at a time but only increments the counter once, which doesn't account for each number individually
Step-by-Step Fix
The main issue is that you're processing two numbers per loop iteration but only checking the counter once. Instead, we should evaluate each number separately as it's entered. Here's how to adjust your code:
int counter = 0; // Loop until we have 2 valid numbers divisible by 5 while (counter < 2) { Console.WriteLine("Enter number"); int number = int.Parse(Console.ReadLine()); // First check if the number is divisible by 3 - reset counter if true if (number % 3 == 0) { counter = 0; Console.WriteLine("Number divisible by 3 - counter reset to 0"); } // If not divisible by 3, check if it's divisible by 5 else if (number % 5 == 0) { counter++; Console.WriteLine($"Valid number! Counter now: {counter}"); } // For numbers that don't meet either condition, do nothing to the counter else { Console.WriteLine("Number doesn't meet conditions - counter stays the same"); } } Console.WriteLine("You've entered 2 numbers divisible by 5! Loop ended.");
Key Changes Explained
- We switched to a
whileloop (though you could use ado-whiletoo) that runs as long ascounteris less than 2. This makes the loop condition clear and intuitive. - We read one number at a time instead of two, which lets us evaluate each number immediately and update the counter right away.
- For every number:
- First check divisibility by 3: if true, reset
counterto 0 (this takes priority over counting divisible-by-5 numbers) - If it's not divisible by 3, check if it's divisible by 5: if true, increment the counter
- Numbers that don't fit either condition leave the counter unchanged
- First check divisibility by 3: if true, reset
If You Still Want to Read Two Numbers Per Iteration
If you need to keep reading two numbers each time, you can adjust the code to check both a and b individually:
int counter = 0; do { Console.WriteLine("Enter first number"); int a = int.Parse(Console.ReadLine()); Console.WriteLine("Enter second number"); int b = int.Parse(Console.ReadLine()); // Check first number if (a % 3 == 0) { counter = 0; } else if (a % 5 == 0) { counter++; // Early exit if we hit 2 before checking the second number if (counter >= 2) break; } // Check second number (only if counter isn't already 2) if (counter < 2) { if (b % 3 == 0) { counter = 0; } else if (b % 5 == 0) { counter++; } } } while (counter < 2); Console.WriteLine("You've entered 2 numbers divisible by 5! Loop ended.");
This way, each number is evaluated on its own, and the counter is updated or reset correctly for both inputs.
内容的提问来源于stack exchange,提问作者KacaMat
相关产品推荐
相关产品推荐

