gpt4 book ai didi

java - 为什么下面的代码中出现错误 "Syntax error on token "println", = Expected"?

转载 作者:行者123 更新时间:2023-11-30 03:48:14 26 4
gpt4 key购买 nike

在下面的代码中我添加了一些System.out.println语句,以便我每次都能看到变量值的变化。但我现在遇到错误“ token “println”上的语法错误,=预期”,尽管我检查了所有内容并且没有发现任何错误。在添加那些 System.out.println 语句之前,一切正常。

这是我的代码:

public class A{
public static int temp = 4;
//System.out.println("temp = "+temp);

public int sum;
System.out.println("sum = " + sum);
public int y;
System.out.println("y = " + y);
public A(int x){
y = temp - 2 +x;
System.out.println("y = " + y);
sum = temp + 3;
System.out.println("sum = " + sum);
temp-=2;
//System.out.println("temp = "+temp);
}
public void methodB(int m, int n){
int x = 0;
y = y + m + (temp++);
System.out.println("y = " + y);
x = x + 2 + n;
System.out.println("x = " + x);
sum = sum + x + y;
System.out.println("sum = " + sum);
System.out.println(x + " " + y+ " " + sum);
}


}


class B extends A {
public int x;
public int sum;
System.out.println("sum = "+sum);
public B(int p){
super(p);
y = temp + p ;
System.out.println("y = "+y);
sum = p+ temp + 1;
System.out.println("sum = "+sum);
temp-=1;
}
public void methodB(int m, int n){
int y =0;
y = y + this.y;
System.out.println("y = " + y);
x = this.y + 2 + temp;
System.out.println("x = " + x);
super.methodB(x, y);
sum = x + y + super.sum;
System.out.println("sum = " + sum);
System.out.println(x + " " + y+ " " + sum);
}


}

class Test
{
public static void main(String [] args){
A a1 = new A(2);
B b1 = new B(3);
a1.methodB(1, 1);
b1.methodB(1, 2);
a1.methodB(3, 2);
b1.methodB(2, 2);

}
}

最佳答案

不能在方法之外使用打印语句。在A类

System.out.println("temp = " + temp);
System.out.println("sum = " + sum);
System.out.println("y = " + y);

需要移动到方法中。 B类

System.out.println("sum = " + sum);

需要移动到方法中。

如果您想查看这些变量的默认值是什么,只需将这些打印语句首先放入构造函数中即可,如下所示

public A(int x){
System.out.println("temp = " + temp);
System.out.println("sum = " + sum);
System.out.println("y = " + y);

y = temp - 2 + x;
System.out.println("y = " + y);
sum = temp + 3;
System.out.println("sum = " + sum);
temp -= 2;
//System.out.println("temp = " + temp);
}

关于java - 为什么下面的代码中出现错误 "Syntax error on token "println", = Expected"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25090999/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com