永发信息网

C语言的switch语句

答案:2  悬赏:40  手机版
解决时间 2021-05-02 13:40
  • 提问者网友:川水往事
  • 2021-05-01 20:29

#include<stdio.h>
#include<math.h>
main()
{
int num;
float V,I,R;
printf("This program will calculate DC voltage,current,or resistance given the other two values\n");
printf("Enter a choice [0=Quit,1=Voltage,2=Current,3=Resistance]:");
scanf("%d",&num);
printf("Calculate Voltage value\n");
printf("========================\n\n");
printf("Enter current value in milliamps:");
scanf("%f",&I);
printf("Enter resistance value in kilo-ohms:");
scanf("%f",&R);
if(R<0)
{
printf("Invalid input!Resistance must be positive or zero.\n");
}
else
{
V=I*R;
printf("The voltage value = %.2f volts\n",V);
}

}

===========================================================

我的这个语句只能算第一个,无法算如果选0,2,3

怎么用一个SWITCH来完成整个句子

V=I*R 当R>=0时

I=V/R 当R>0时

R=V/I 当I不等于0时

最佳答案
  • 五星知识达人网友:山河有幸埋战骨
  • 2021-05-01 21:16
这个是完整程序。。可能某些细节,比如错误提示信息可能未正确显示,您可以先看看。。

#include<stdio.h>
#include<math.h>
void main()
{
int num;
float V,I,R;
printf("This program will calculate DC voltage,current,or resistance given the other two values\n");
printf("Enter a choice [0=Quit,1=Voltage,2=Current,3=Resistance]:");
scanf("%d",&num);
switch (num)
{
case 0:
exit(1);
case 1:
printf("Calculate Voltage value\n");
printf("========================\n\n");
printf("Enter current value in milliamps:");
scanf("%f",&I);
printf("Enter resistance value in kilo-ohms:");
scanf("%f",&R);
if(R<0)
{
printf("Invalid input!Resistance must be positive or zero.\n");
}
else
{
V=I*R;
printf("The voltage value = %.2f volts\n",V);
}
break;
case 2:
printf("Calculate Current value\n");
printf("========================\n\n");
printf("Enter voltage value in milliamps:");
scanf("%f",&V);
printf("Enter resistance value in kilo-ohms:");
scanf("%f",&R);
if(R<=0)
{
printf("Invalid input!Resistance must be positive.\n");
}
else
{
I=V/R;
printf("The Current value = %.2f volts\n",I);
}
break;
case 3:
printf("Calculate Resistance value\n");
printf("========================\n\n");
printf("Enter Voltage value in milliamps:");
scanf("%f",&V);
printf("Enter Current value in kilo-ohms:");
scanf("%f",&I);
if(I==0)
{
printf("Invalid input!Current must not be zero.\n");
}
else
{

R=V/I;
printf("The Resistance value = %.2f volts\n",R);
}
break;
default:
printf("Invalid input!");
break;
}
}
全部回答
  • 1楼网友:末日狂欢
  • 2021-05-01 22:17

#include<stdio.h> #include<math.h> main() { int num; float V,I,R; printf("This program will calculate DC voltage,current,or resistance given the other two values\n"); printf("Enter a choice [0=Quit,1=Voltage,2=Current,3=Resistance]:"); scanf("%d",&num); printf("Calculate Voltage value\n"); printf("========================\n\n"); printf("Enter current value in milliamps:"); scanf("%f",&I); printf("Enter resistance value in kilo-ohms:"); scanf("%f",&R); if(R<0) { printf("Invalid input!Resistance must be positive or zero.\n"); } else

switch(num) {

case 0:

exit(1);

case 1: V=I*R; printf("The voltage value = %.2f volts\n",V);

break;

case 2:

if(R>0)

I=V/R;

printf("The current value = %.2f A\n",I);

break;

case 3:

if(i!=0)

R=V/I;

printf("The resistance value = %.2f kilo-ohms\n",R);

break; }

}

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯