gpt4 book ai didi

c - 输入数字,显示最高、最低、平均值以及输入的数字数量。使用菜单

转载 作者:行者123 更新时间:2023-11-30 21:11:02 26 4
gpt4 key购买 nike

我的小组作业是制作一个程序,允许用户输入任意数量的数字,然后程序会告诉你输入的最高数字、输入的最低数字、平均值、输入的总数和平均值。我们必须使用菜单。

我们已经写好了菜单。我们的大部分计算代码都在案例 A 中(我们必须使用字母)来计算这些东西。但我们不知道如何让程序重复。如果您输入完数字并对“您想输入另一个数字吗”说 N,程序就会关闭。

另外,你如何计算总数?

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define pause system("pause")
#define cls system("cls")
#define pause system("pause")
#define flush fflush(stdin)
#include <ctype.h> // contains toupper

main(){

int num, count = 0, high = 0, low = 0, total = 0;
float avg;
char choice = ' ', again;

printf("\t\t =====================\n");
printf("\t\t == MAIN MENU ==\n");
printf("\t\t =====================\n");
printf("\t\tA. Enter a number.\n");
printf("\t\tB. Display the highest number.\n");
printf("\t\tC. Display the lowest number.\n");
printf("\t\tD. Display the average of all numbers.\n");
printf("\t\tE. Display how many numbers were entered.\n");
printf("\t\tQ. Quit.\n\n\n");
printf("Enter your selection: ");
scanf("%c", &choice);


switch (choice) {
case 'A':
do {
printf("Enter a number: ");
scanf("%i", &num);
if (count = 0) {
high = num;
low = num;
}
else
if (num > high)
num = high;
if (num < low)
num = low;
count++;

printf("Do you want to enter another number? (Y/N): ");
scanf("%c", &again);
} while (again != 'N');
break;
case 'B':
if (count == 0) { printf("Please enter a number first.\n\n");
}
else printf("The highest number is %i\n\n", high);
break;
case 'C':
if (count == 0) printf("Please enter a number first.\n\n");
else printf("The lowest number is %i\n\n", low);
break;
case 'D':
if (count == 0) printf("Please enter a number first.\n\n");
else
avg = total / count;
printf("The average is %.2f\n\n", avg);
break;
case 'E':
printf("You entered %i numbers.\n\n", count);
break;
case 'Q':
printf("Thanks for playing.\n\n");
break;
default:
printf("Invalid Selection.\n\n");
break;
}
pause;
}

最佳答案

您需要包含另一个 while 循环来封装 switch 语句

bool running = 1;

while(running)
{
printf("Enter your selection: ");
scanf("%c", &choice);

switch(choice)
{
...
case 'q':
case 'Q':
running = 0;
break;
}

}

关于c - 输入数字,显示最高、最低、平均值以及输入的数字数量。使用菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26288552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com