我得到解決的問題如下: - 寫一個名爲「is_digit」的函數接受一個字符作爲輸入,並返回一個整數值如果參數輸入是0到9之間的ASCII數字,則爲1。否則,該函數必須返回值0.C發出函數返回1當用戶輸入爲0-9時ASCII
- 在主函數中,調用is_digit,並根據用戶輸入傳入參數。然後主函數必須輸出調用is_digit的結果。
這是我迄今爲止
#include <stdio.h>
char is_digit(int x)
{
if (x > '48' && x < '57') //The ascII range between 0-9 (i think...)
{
return 1;
}
else
//Whatever the user input is it always runs the else
{
return 0;
//Is this what the question means by "function must return a value of 0"
}
}
int main()
{
char x;
printf("input: ");
scanf("%c", &x);
is_digit(x);
}
爲什麼我的程序沒有返回1,如果我的用戶輸入是0-9?
更新版本與考慮到的意見:
#include <stdio.h>
char is_digit(char x)
{
if (x >= 48 && x <= 57)
{
printf("1");
return 1;
}
else
{
printf("0");
return 0;
}
}
int main()
{
char x;
printf("input: ");
scanf("%c", &x);
is_digit(x);
}
測試'if(x>'48'&& x <'57')'不正確。刪除單引號,並使用'> ='而不是'>'(也可用'<='而不是'<') – Aziz
你正在比較一個int與一個字符(各種:兩個元素的字符;不確定這是多麼有效)。請使用和*讀取*您的編譯器警告。 – Evert
哎呀,修正了我的if語句,非常感謝!但我的功能仍然沒有返回1 ..... –