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

使用Array[10]与While循环(模10)配对输入时代码无响应求助

Troubleshooting Your Number Pairing Program

Hey there! Let's figure out why your program isn't responding after input and get that number pairing feature working exactly as you want it.

First, let's break down the common culprits for "no reaction" after entering a number, then walk through a working example that uses the array + while loop + modulo 10 logic you're aiming for.

Common Reasons for No Output/Response

  • Deadly Infinite Loop: If your while loop doesn't properly reduce the input number (e.g., forgetting to divide by 10 each iteration), the loop will run forever—your program will hang instead of producing output.
  • Input Reading Issues: If your code isn't correctly capturing the input number (like handling invalid input, or leaving leftover newline characters in the input buffer), it might be waiting for more input you aren't providing.
  • Unflushed Output Buffer: If you print without a newline (\n) or don't explicitly flush the buffer, your output might be stuck in memory and never show up before the program exits.
  • Missing Pause (But Only If Output Exists): The "program closes immediately" issue is real, but it only hides output that was already generated. If you see nothing at all, this is probably not the root cause.

Working Example Code

Here's an implementation that matches your expected behavior (input 1234556789 → output 1 : 9 2 : 8 3 : 7 4 : 6 5 : 5):

#include <stdio.h>
#include <stdlib.h> // For abs() if handling negative numbers

int main() {
    int inputNum;
    int digitStorage[20]; // Holds individual digits (adjust size if needed)
    int digitCount = 0;

    // Step 1: Get valid input from the user
    printf("Enter a number: ");
    if (scanf("%d", &inputNum) != 1) {
        printf("Oops, that's not a valid number!\n");
        return 1;
    }

    // Handle negative numbers by converting to positive
    int tempNum = abs(inputNum);

    // Step 2: Extract digits using modulo 10 + while loop
    if (tempNum == 0) {
        digitStorage[digitCount++] = 0; // Special case for input 0
    } else {
        while (tempNum > 0) {
            digitStorage[digitCount++] = tempNum % 10; // Grab last digit
            tempNum /= 10; // Remove last digit (critical to avoid infinite loop!)
        }
    }

    // Step 3: Pair first with last, second with second-last, etc.
    for (int i = 0; i <= (digitCount - 1) / 2; i++) {
        int leftDigit = digitStorage[digitCount - 1 - i]; // Left-side digit
        int rightDigit = digitStorage[i]; // Right-side digit
        printf("%d : %d ", leftDigit, rightDigit);
    }
    printf("\n"); // Critical: Flushes the output buffer so text appears

    // Optional: Pause to view output (adjust based on your OS)
    // For Windows: system("pause");
    // For Linux/macOS: 
    // while (getchar() != '\n'); // Clear leftover newline from scanf
    // getchar(); // Wait for user to press enter

    return 0;
}

Key Fixes & Checks for Your Code

  1. Avoid Infinite Loops: Double-check your while loop—you must have a line like tempNum /= 10 to shrink the number each time. Without this, the loop will never end.
  2. Validate Input: Add a check to make sure scanf actually read a number (like the example above) so you don't waste time processing garbage input.
  3. Flush Output: Always end your print statements with \n or use fflush(stdout) to force the text to show up immediately.
  4. Handle Edge Cases: Don't forget about input 0 or negative numbers—small oversights here can break your logic entirely.

If you still run into issues, try adding debug prints (e.g., printf("Current tempNum: %d\n", tempNum); inside the while loop) to see exactly where your code is getting stuck.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:09:23