简体   繁体   English

关于计算机科学入门测验的问题

[英]Question on Intro to Computer Science quiz

令我困惑的 Java 问题

I recently came across this question on the Saylor Academy intro to CS quiz.我最近在赛勒学院 CS 测验简介中遇到了这个问题。 It's about the syntax in Java.这是关于Java中的语法。 Could someone please explain what I'm missing?有人可以解释一下我缺少什么吗?

Compiler says I am correct.编译器说我是对的。

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.

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