我正在尝试制作一个程序,以便老师可以键入数字,并且它将以C输出相应的字母等级。这就是到目前为止,我正在尝试使控制台程序保持打开状态,直到老师输入退出或点击X图标。感谢您的帮助。
#include <stdio.h>
#include<stdlib.h>
#define exit -1
int main(void) {
int grade;
printf("Enter student grade: ");
scanf_s("%d", &grade);
printf("Students grade is: %d", grade);
while (grade != exit) {
if (grade < 65)
{
printf("\nStudent grade is: F\n");
}
else if (grade <= 69)
{
printf("\nStudent grade is: D\n");
}
else if (grade <= 79)
{
printf("\nStudent grade is: C\n");
}
else if (grade <= 89)
{
printf("\nStudent grade is: B\n");
}
else if (grade <= 100)
{
printf("\nStudent grade is: A\n");
}
else if (grade == exit) {
#define EXIT_SUCCESS 0;
}
break;
}
printf("Enter student grade: ");
scanf_s("%d", &grade);
printf("Students grade is: %d", grade);
return 0;
}
答案 0 :(得分:0)
弄清楚了,谢谢您的帮助。
#include <stdio.h>
#include<stdlib.h>
#define exit -1
int main(void) {
int grade;
printf("Enter student grade , or '-1' to exit \n");
scanf_s("%d", &grade);
while (grade != exit) {
printf("Students grade is: %d", grade);
if (grade < 65)
{
printf("\nStudent grade is: F\n");
}
else if (grade <= 69)
{
printf("\nStudent grade is: D\n");
}
else if (grade <= 79)
{
printf("\nStudent grade is: C\n");
}
else if (grade <= 89)
{
printf("\nStudent grade is: B\n");
}
else if (grade <= 100)
{
printf("\nStudent grade is: A\n");
}
else if (grade == exit) {
exit;
}
printf("Enter student grade , or '-1' to exit \n");
scanf_s("%d", &grade);
}
fgetc(stdin);
return 0;
}
答案 1 :(得分:-1)
答案前的几件事:提交代码之前,请做一些研究,学习和理解此代码。我并不是说您愿意,但是和我一起上学的许多学生会盲目地复制并粘贴作业,从长远来看,这只会伤害您。它并没有您想的那么复杂,一旦您学习了逻辑,它就会变得更容易。
我建议您学习一些有关此作业的资源:Convert char array to a int number in C,https://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm
如果要在同一数组中输入单词“ exit”或int,则需要将其写入char数组。想法是将字符串转换为整数(如果是数字)。您可以使用sscanf或atoi之类的工具来执行此操作。
如果要键入“ exit”,则区分大小写。您可以使用for循环将所有大小写变为小写或大写。就我而言,我接受用户输入并将其与我的数组exit_Array进行比较。如果它返回0(它匹配),它将进入下一个退出程序的阶段。
如果是整数,则进入阶段1,即评分。分级后,它会将相位降低1,因此变为0(再次进入第一个循环)。它将一遍又一遍地执行此操作,直到用户输入“退出”为止。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char input_Array[3];
char exit_Array[] = "exit";
int grade;
int phase = -1;
phase++;
while(phase == 0)
{
printf("Enter student grade: ");
scanf("%s", input_Array);
// Converts all letters to lower case so it can be Exit, eXiT, etc.
for(int i = 0; i <= strlen(input_Array); i++)
{
input_Array[i] = tolower(input_Array[i]);
}
// Exit Input
if(strcmp(input_Array, exit_Array) == 0)
{
printf("Goodbye!");
exit(0);
}
else
{
// Convert String to Number
grade = atoi(input_Array);
}
phase++;
while(phase == 1)
{
if (grade < 65)
{
printf("Student grade is: F\n");
}
else if (grade <= 69)
{
printf("Student grade is: D\n");
}
else if (grade <= 79)
{
printf("Student grade is: C\n");
}
else if (grade <= 89)
{
printf("Student grade is: B\n");
}
else if (grade <= 100)
{
printf("Student grade is: A\n");
}
phase--;
}
}
return 0;
}