Sign of a number · 数的符号
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Sign of a number
Return a number that describes the sign of n: -1 when n is negative, 1 when it is positive, and 0 when it is exactly zero.
Use if to test the cases. The order matters — handle negative and positive first, then return 0 for what is left.
中文
数的符号
返回一个表示 n 符号的数:当 n 为负时返回 -1,为正时返回 1,恰好为零时返回 0。
用 if 来判断这几种情况。顺序很重要 —— 先处理负数和正数,剩下的情况再返回 0。
Complete int sign(int n) to return -1 if n is negative, 0 if it is zero, and 1 if it is positive. · 完成 int sign(int n):当 n 为负时返回 -1,为零时返回 0,为正时返回 1。
Click Run to see the output here. · 点击“运行”查看此处输出。