[英]in C why the program is not reconizing the second if statement or upper case char variable?
編寫一個程序,詢問用戶角度(以度為單位)。 然后,要求用戶鍵入字母。 如果用戶鍵入小寫字母,則將角度的正弦顯示為小數點后四位。 如果用戶鍵入大寫字母,則將角度的余弦顯示到小數點后四位。
所以這就是我到目前為止所擁有的,為什么程序無法識別大寫並打印余弦?
#include<stdio.h>
#include<math.h>
#define PI 3.14159265
main()
{
int a;
double x,y;
char b;
printf("What is the angle in degrees?\n");
scanf("%i",&a);
printf("Type a letter!\n");
scanf("%i",&b);
x=sin(a*PI/180);
y=cos(a*PI/180);
if (b>=97 | b<=122)
{
printf("The sine of %i is %.4f.\n",a,x);
}
if (b>=65 && b<=90)
{
printf("The cosine of %i is %.4f.\n",a,y);
}
return 0;
}
因為if(b>= 97 | b <= 122)
將始終為true。
應該是if(b>=97 && b<=122)
,它將b
限制在小寫字母的范圍內。
就我個人而言,我更喜歡將其寫為if (97 <= b && b <= 122)
,這樣很容易看到其范圍。
您是否認為使用庫<ctype.h>
會更容易?
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#define PI 3.14159265
int main()
{
int a;
double x,y;
char b;
printf("What is the angle in degrees?\n");
scanf("%d", &a);
printf("Type a letter!\n");
scanf(" %c", &b);
x=sin(a*PI/180);
y=cos(a*PI/180);
if (isupper(b))
{
printf("The sine of %d is %.4f.\n",a,x);
}
else
{
printf("The cosine of %d is %.4f.\n",a,y);
}
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.