Java 为什么我会犯这些错误?表达式的非法开始
这是我的密码:Java 为什么我会犯这些错误?表达式的非法开始,java,arrays,Java,Arrays,这是我的密码: public class JavaApplication16 { public static void main(String[] args) { //illegal start of expression, ']' expected, <identifier> expected boolean a = groupSum(0, [2,4,8], 10); } public boolean groupSum(int
public class JavaApplication16 {
public static void main(String[] args) {
//illegal start of expression, ']' expected, <identifier> expected
boolean a = groupSum(0, [2,4,8], 10);
}
public boolean groupSum(int start, int[] nums, int target) {
if (start >= nums.length) return (target == 0);
if (groupSum(start + 1, nums, target - nums[start])) return true;
if (groupSum(start + 1, nums, target)) return true;
return false;
}
}
公共类JavaApplication16{
公共静态void main(字符串[]args){
//表达式“]”的开头非法,应为,应为
布尔a=组和(0,[2,4,8],10);
}
公共布尔groupSum(int start,int[]nums,int target){
如果(开始>=nums.length)返回(目标==0);
if(groupSum(start+1,nums,target-nums[start])返回true;
if(groupSum(start+1,nums,target))返回true;
返回false;
}
}
我试着想办法解决这个问题,但我真的很迷茫。请帮助我调试代码。以下是使用int数组的正确方法
boolean a = groupSum(0, new int[] {2, 4, 8}, 10);
您需要将代码更改为如下所示 1-
newint[]{2,4,8}
创建int数组
2-将groupSum方法更改为静态或创建对象形式JavaApplication16
,如下所示JavaApplication16 ja16=new JavaApplication16();ja16.组和(…)代码>
您可以执行以下操作来解决问题
public class JavaApplication16 {
public static void main(String[] args) {
boolean a = groupSum(0, new int[]{2,4,8}, 10);// new int[]{2,4,8} to create array of ints
}
public static boolean groupSum(int start, int[] nums, int target) {// change the method to be static
if (start >= nums.length) return (target == 0);
if (groupSum(start + 1, nums, target - nums[start])) return true;
if (groupSum(start + 1, nums, target)) return true;
return false;
}
}
您没有使用正确的符号来创建数组。创建一个int数组如下所示:newint[]{1,2,3}。
代码中存在的另一个问题是,您正在从静态上下文调用一个非静态方法。您可以通过将groupSum
标记为static来解决此问题。
以下是您的代码的固定版本:
class JavaApplication16 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//illegal start of expression, ']' expected, <identifier> expected
boolean a = groupSum(0, new int[]{2,4,8}, 10);
}
public static boolean groupSum(int start, int[] nums, int target) {
if (start >= nums.length) return (target == 0);
if (groupSum(start + 1, nums, target - nums[start])) return true;
if (groupSum(start + 1, nums, target)) return true;
return false;
}
}
class JavaApplication16{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//表达式“]”的开头非法,应为,应为
布尔a=groupSum(0,新int[]{2,4,8},10);
}
公共静态布尔groupSum(int start,int[]nums,int target){
如果(开始>=nums.length)返回(目标==0);
if(groupSum(start+1,nums,target-nums[start])返回true;
if(groupSum(start+1,nums,target))返回true;
返回false;
}
}
必须将int数组定义为
boolean a = groupSum(0, new int[] {2,4,8}, 10);
另一方面,方法groupSum
是一个实例方法,而main是一个静态方法;不能在静态方法中使用实例方法
您有两种解决方案:
将groupSum
方法定义为静态
创建类的对象JavaApplication16
并使用其方法。
代码如下所示:
public class JavaApplication16 {
public static void main(String[] args) {
JavaApplication16 ja16 = new JavaApplication16();
boolean a = ja16.groupSum(0, new int[] {2,4,8}, 10);
}
public boolean groupSum(int start, int[] nums, int target) {
if (start >= nums.length) return (target == 0);
if (groupSum(start + 1, nums, target - nums[start])) return true;
if (groupSum(start + 1, nums, target)) return true;
return false;
}
}
尝试boolean a=groupSum(0,新int[]{2,4,8},10)
并将方法设置为static
。出现此错误是因为这不是定义数组的正确语法。下面是下一个问题的答案:不建议使用仅代码的答案。请在您的答案中添加一些解释,以澄清错误以及如何预防/解决问题。我更改并添加了解释,谢谢