double y = [(100-totalGrade)*weightCategory]+(totalGrade*x);
这是一个有效的方程式吗?主要是想知道括号是否使用正确
No, the brackets are not used correctly. the square brackets ([
]
) are reserved for accessing array elements.
You want to use nested parenthesis:
double y = ((100-totalGrade)*weightCategory)+(totalGrade*x);
but in your equation, as alex pointed out:
double y = (100-totalGrade)*weightCategory+totalGrade*x;
is sufficient.
[
and ]
are not accepted in Java as the brackets you see in math, they are used to declare arrays. You must use (
and )
in their places.
这就够了:
double y = (100-totalGrade)*weightCategory+totalGrade*x;
不,如果您需要嵌套括号,您只需继续使用(
and )
,确保正确关闭每一对。
和字符用于下标运算符[
,]
例如在数组中,例如 main 方法的参数String[] args
。
如果您遵循 BIDMAS/BODMAS 的规则,您会发现您的情况实际上不需要外括号。但是,如果您将来需要它们,请使用嵌套括号,如下所示:
int example = (1+2)*((3+4)*5);