我试着从用户那里得到他们想要输入的号码。在菜单上。我已经得到了工作所需的一切,除了这个最低的数字。我不知道从这里到哪里去。我不知道怎样才能在交换机里找到号码。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define PAUSE system("pause")
#define CLEAR system("cls")

main() {
    // Initialize variables
    char choice;
    int sum = 0;
    int avg = 0;
    int high = 0;
    int low = 0;
    int quit = 0;
    int i = 0;
    int num = 0;
    int j = 0;
    int prevNum;

    do{
        printf("What would you like to do\n"
        "A: enter an integer\n"
        "B: show sum\n"
        "C: Show average\n"
        "D: show Highest num\n"
        "E: Show lowest\n"
        "Q: quit\n");
        scanf("%c", &choice);
        CLEAR;

    switch (choice) {
    case 'A':
        printf("Enter an Integer\n");
        scanf("%i", &num);
        j++;
        sum = num + sum;

        if (num > high)
            high = num;

        PAUSE;
        break;

    case 'B':
        printf("The sum of al numbers entered is %i\n", sum);
        PAUSE;
        break;

    case 'C':
        avg = sum / j;
        printf("The average of all numbers entered is %i\n",avg);
        PAUSE;
        break;

    case 'D':
        printf("The Highest number entered is %i\n", high);
        PAUSE;
        break;

    case 'E':

        printf("The lowest number entered is %i\n", low);
        PAUSE;
        break;

    case 'Q':
        quit = 1;
        break;


    } // end switch

 } while (quit != 1);

PAUSE;
} // END MAIN

最佳答案

你可以使用:

if (num < low) {
    low = num;
}

唯一的问题是第一个号码。由于您将low初始化为0,因此用户输入的任何正数都不会低于此值。你需要特别对待第一个号码。您可以为此检查j的值。
然后在A情况下,测试这个变量。
case 'A':
    printf("Enter an Integer\n");
    scanf("%i", &num);
    j++;
    sum = num + sum;

    if (j == 1 || num > high)
        high = num;
    if (j == 1 || num < low)
        low = num;

    PAUSE;
    break;

关于c - 我该怎么做才能使我的程序在do while循环中保持开关中输入的最低编号?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37978328/

10-12 01:31