简体   繁体   English

为什么我不能将余数分配给新变量??? 爪哇

[英]Why cant I assign a remainder to a new variable??? JAVA

    int d = year%100;
    int c = year/100;
    int valueA = (int)(((13*monthnumber)-1)/5);
    int valueB = (int) d/4;
    int valueC = (int) c/4;

    int weekDay = (d + valueA + d + valueB + valueC - 2*c);

    int remainder %= weekDay/7; 

im trying to use the modulus assignment operator but keep getting a system error saying that an '=' was expected instead of '%=' 我试图使用模数赋值运算符,但不断出现系统错误,提示应使用'='而不是'%='

code in question is the last line 有问题的代码是最后一行

please help 请帮忙

int remainder %= weekDay/7; 

would be equivalent to 相当于

int remainder = remainder % weekDay/7; 

which makes no sense since you just declared remainder , so it has no previous value. 这是没有意义的,因为您刚刚声明了remainder ,所以它没有以前的价值。

Had you declared the remainder variable earlier, this would work : 如果您早先声明了remainder变量,这将起作用:

remainder %= weekDay/7; 

%= can't be used for variables that haven't had a value assigned yet. %=不能用于尚未分配值的变量。

var %= {value};

is equivalent to 相当于

var = var % {value};

But in the way you're using it, remainder hasn't had a value assigned to it yet. 但是,在您使用它的方式中,剩余值尚未赋值。 So it makes no sense. 因此,这没有任何意义。

因为使用int remainder您需要声明一个变量,并且无法使用%=来实现其初始化

您可能只是想这样做:

int remainder = weekDay % 7;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM