使用C语言函数实现自定义输入数量的最值查找及问题排查
Let's work through each of your issues and fix them one by one, along with polishing up the program's overall logic:
Issue 1: Unwanted input wait before the second printf
The root cause is this line:
scanf("%d\n",×);
The trailing \n in the scanf format string tells the function to keep reading until it hits a non-whitespace character after the integer. That’s why after entering the number of inputs, you had to type an extra value (like 70 in your output) before the program moved forward.
Fix: Remove the unnecessary \n from the scanf format:
scanf("%d", ×);
Issue 2: Unstable system() function
Your provided code doesn’t include a system() call, but if you were using it for things like clearing the screen, it’s indeed unreliable—it depends entirely on the operating system (system("cls") works on Windows, system("clear") on Linux/macOS, etc.).
Recommendation: Skip using system() for non-critical operations. If you need screen-clearing functionality, you can add platform-specific conditional code, or just omit it entirely for better portability.
Issue 3: Only detects the last input number
Your max() and min() functions have two critical flaws:
- They reset their comparison variables (
a=0,x=100) every time they’re called, so they can’t track the running max/min across multiple inputs. - The
min()function incorrectly returnsnum2instead of the updatedxvalue. - You only call these functions once, passing only the final input value—so they can’t calculate the overall min/max at all.
Fix: Ditch the separate functions for this simple use case. Instead, initialize maxi and mini with the first valid input, then update them directly in each loop iteration as you read new numbers. This is simpler and far more reliable.
Corrected Full Code
#include <stdio.h> int main(){ int times, interval; float mini, maxi, data_Input; printf("How many numbers would you like to type in? : "); scanf("%d", ×); // Fixed issue 1: removed trailing \n // Get first number and validate it (skipped in original code!) printf("Type in the number: "); scanf("%f", &data_Input); while(data_Input < 0 || data_Input > 100){ printf("Invalid Input! Please re-enter a number between 0 and 100: "); scanf("%f", &data_Input); } // Initialize min and max with the first valid number mini = maxi = data_Input; // Process remaining numbers for(interval = 2; interval <= times; interval++){ printf("\nType in the number: "); scanf("%f", &data_Input); // Validate input while(data_Input < 0 || data_Input > 100){ printf("Invalid Input! Please re-enter a number between 0 and 100: "); scanf("%f", &data_Input); } // Update max and min in real time if(data_Input > maxi){ maxi = data_Input; } if(data_Input < mini){ mini = data_Input; } } printf("\nThe Lowest Number is %.2f\n", mini); printf("The Highest Number is %.2f\n", maxi); return 0; }
Key Improvements:
- Fixed the scanf formatting quirk that caused unwanted input waiting
- Added validation for the first input number (your original code missed this)
- Initialized
miniandmaxiwith the first valid input, then updated them iteratively - Combined duplicate validation loops into one for cleaner code
- Fixed small grammar issues in print statements (
How many number→How many numbers)
内容的提问来源于stack exchange,提问作者Exc4pe

