Java:使用short而不是int

时间:2017-04-24 16:47:50

标签: java short

我编写了一个函数来测试使用short的短语

//rating bar
static void starLight(String name, int resId) {
    RatingBar name = (RatingBar) findViewById(resId);
    name.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float 
                rating, boolean fromUser) {
            int whole = Math.round(rating);

            Drawable progress = ratingBar.getProgressDrawable();
            if (whole == 1) {
                DrawableCompat.setTint(progress, 
                    ResourcesCompat.getColor(getResources(), R.color.colorGreen, null));
            }
            if (whole == 2) {
                DrawableCompat.setTint(progress, 
                    ResourcesCompat.getColor(getResources(), R.color.colorOrange, null));
            }
            if (whole == 3) {
                DrawableCompat.setTint(progress, 
                    ResourcesCompat.getColor(getResources(), R.color.colorRed, null));
            }
        }
    });
}

我已经用最大尺寸调用了函数,即:

public static short shortFactorial(short n)
{
    short lIndex = 1;
    short lResult = 1;

    for(lIndex = 1;lIndex <= n;lIndex++)
    {
        lResult = (short)(lIndex * lResult);
    }

    return(lResult);
}

谁能告诉我为什么结果是1?

1 个答案:

答案 0 :(得分:3)

由于@HostListener('window:resize', ['$event']) onResize(event: any) { console.log(event.target.innerWidth); this.deviceWidth = event.target.innerWidth; } 的最大值为32767,short溢出为负值,因此您的循环不会迭代。在初始化(short)33000时,它会返回1.