我想寫,有很多比較略大於小於switch語句ç
Write a program in 「QUANT.C」 which 「quantifies」 numbers. Read an integer 「x」 and test it, producing the following output: x greater than or equal to 1000 print 「hugely positive」 x from 999 to 100 (including 100) print 「very positive」 x between 100 and 0 print 「positive」 x exactly 0 print 「zero」 x between 0 and -100 print 「negative」 x from -100 to -999 (including -100) print 「very negative」 x less than or equal to -1000 print 「hugely negative」 Thus -10 would print 「negative」, -100 「very negative」 and 458 「very positive」.
然後我試着用開關來解決它,但它沒有工作代碼,做我必須使用if語句來解決它,還是有一種方法可以使用switch來解決它?
#include <stdio.h>
int main(void)
{
int a=0;
printf("please enter a number : \n");
scanf("%i",&a);
switch(a)
{
case (a>1000):
printf("hugely positive");
break;
case (a>=100 && a<999):
printf("very positive");
break;
case (a>=0 && a<100):
printf("positive");
break;
case 0:
printf("zero");
break;
case (a>-100 && a<0):
printf("negative");
break;
case (a<-100 && a>-999):
printf("very negative");
break;
case (a<=-1000):
printf("hugely negative");
break;
return 0;
}
'switch'只能處理具有常量積分值的精確比較。你將不得不使用'if'和'else'。 –
定義'沒有工作' – deW1
*七*編譯錯誤應該告訴你*某事*(和1警告:'控制到達非空函數結束)。 – usr2564301