[英]Question on Intro to Computer Science quiz
That is a blatant mistake on their part.这对他们来说是一个明显的错误。 I do not think any Java version accepts this code - ie, this is a compilation error and your answer is correct.
我不认为任何 Java 版本都接受此代码 - 即,这是一个编译错误,您的答案是正确的。 You can run it anywhere to verify this:
您可以在任何地方运行它来验证这一点:
>cat test.java
public class test {
public static void main(String[] argv) {
int x+= 1;
}
}
>java test.java
test.java:3: error: ';' expected
int x+= 1;
^
1 error
error: compilation failed
Its true when the value of x is set during variable declaration the expression above will not result into an error the code below explains it better当在变量声明期间设置 x 的值时它是真的,上面的表达式不会导致错误下面的代码更好地解释它
public class HelloWorld{
public static void main(String []args){
int x = 5;
x += 1;
System.out.println(x);
//the value of x would be six ie original value of x plus 1
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.