search

Home  >  Q&A  >  body text

java - 麻烦老师看下这段代码有哪些错误,无法运行。

巴扎黑巴扎黑2910 days ago307

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 10:41:04

    Compile in the compilation environment based on the code you provide

    That is, x is defined repeatedly. A variable can only be defined once in the same scope (here refers to the main() method). The above code defines x in both "int x=13;" and "for (int x=0;x<10;x=x+1)", so an error occurs.

    I guess the questioner wants to print the value of x, so remove the "int" keyword in the for loop

    class Demo{
        public static void main(String[] args) {       
            int x=13;
            while ( x >12){ 
                x=x-1;
            }
            for (x=0;x<10;x=x+1){
                System.out.print("x is now "+ x);
            }
        }
    }

    If you still don’t understand the question, you can refer to this blog post

    reply
    0
  • 阿神

    阿神2017-04-18 10:41:04

    int x=13; defines a variable x
    int x=0 and defines another variable x
    repeated definition

    reply
    0
  • PHPz

    PHPz2017-04-18 10:41:04

    Correct answer upstairs,

    reply
    0
  • Cancelreply