尝试在 Java 中使用静态变量时出现标识符异常
Getting an identifier exception when trying to use a static variable in Java
我正在学习递归,我正在尝试创建一个没有循环的 reverseString
方法。声明要操作的变量时,出现以下两个错误:
MyClass.java:2: error: <identifier> expected
int static count = 1;
^
MyClass.java:2: error: <identifier> expected
int static count = 1;
^
2 errors
有谁知道为什么?这是我的代码:
public class MyClass {
int static count = 1;
public static String reverseString(String str) {
String reverse = "";
String sub = str.substring(str.length() - count, str.length() - count + 1);
reverse += sub;
if (sub.length() != 1) {
return reverseString(str.substring(0, str.length() - count));
} else {
return reverse;
}
count++;
}
public static void main(String args[]) {
System.out.println(reverseString("Hello"));
}
}
修饰符(如 static
)应位于类型之前:
static int count = 1;
我正在学习递归,我正在尝试创建一个没有循环的 reverseString
方法。声明要操作的变量时,出现以下两个错误:
MyClass.java:2: error: <identifier> expected
int static count = 1;
^
MyClass.java:2: error: <identifier> expected
int static count = 1;
^
2 errors
有谁知道为什么?这是我的代码:
public class MyClass {
int static count = 1;
public static String reverseString(String str) {
String reverse = "";
String sub = str.substring(str.length() - count, str.length() - count + 1);
reverse += sub;
if (sub.length() != 1) {
return reverseString(str.substring(0, str.length() - count));
} else {
return reverse;
}
count++;
}
public static void main(String args[]) {
System.out.println(reverseString("Hello"));
}
}
修饰符(如 static
)应位于类型之前:
static int count = 1;