我目前正在尝试练习我在大学学到的功能,以备即将到来的考试。我正在尝试使用以下功能; if
、switch
、scanf
、printf
、for
。
但是,当我尝试在控制台上执行程序时,一旦我将输入放入变量 n
中,switch 函数就会在控制台上不显示任何输出,并且程序结束。
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(void) {
int n,i,x,y,z;
float res;
printf("Please input x,y,z using (,)\n");
scanf("%d,%d,%d",&x,&y,&z);
printf("You inputed the following numbers: x=%d, y=%d, z=%d\n",x,y,z);
printf("Which of the following equations would you like to run?\n");
printf("x+y/z ?? (1)\n");
printf("sqrt(z)+(x^3/y) ?? (2)\n");
printf("The average number of the three entered ?? (3)\n");
printf("Print the word 'test' as many times as is the sum of x,y,z ?? (4)\n");
scanf(" %d",n);
switch(n)
{
case 1:
if(z!=0)
{
res=(float)(x+y)/z;
printf("The result is: %f \n",res);
}
else
{
printf("Division by zero?!\n");
}
break;
case 2:
if((z>=0)&&(y!=0))
{
res=(float)sqrt(z)+((float)pow(x,3)/y);
printf("The result is: %f \n",res);
}
else if(z<0)
{
printf("Square root attempted to use negative integer\n");
}
else if(y=0)
{
printf("You cannot devide with zero\n");
}
else
{
printf("what?");
}
break;
case 3:
res=x+y+z/(float)3;
printf("The average of x+y+z is: %f\n",res);
break;
case 4:
res=x+y+z;
for(i=1; i<res; ++i)
{
printf("test\n");
}
break;
default:
printf("Please input a number between 1-4!!!\n");
}
return 0;
}
最佳答案
问题可能出在这个语句上
scanf("%d",n);
将其更改为
scanf("%d",&n);
说明: scanf("%d",n);
不允许用户在控制台上输入,因为缺少地址运算符 &
关于C,我的 switch 语句在控制台上没有输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53326063/