我的主要递归+错误
问题描述:
我使用java bluejay试图解决递归问题。我的主要递归+错误
问题:给定一个自然数(高于0) 和int的数组,该方法需要判断该数组是否包含总结数字以获得给定int的方法。 示例{4,5} int = 13 >> 4 + 4 + 5 = 13 = true 示例{4,5} int = 3 >>> false。
的第二个问题是,当我用我的主要它说“无法找到符号 - 方法 isSumOf(INT [],INT),但我确实有这种方法......
这里是我的课:
public class Ex14
{
static int allways;
static int index = -1;
public static boolean isSumOf(int[] s, int n){
if(index == -1) index = s.length - 1;
return isSumOf(s, n, index);
}
private static boolean isSumOf(int[] s, int n, int index){
int temp = s[index];
if(allways == n) {return true;}
if(allways > n && index != 0) {allways -= temp + s[index - 1];
if(allways/s[index - 1] == s.length) index --;}
if(allways > n && index == 0) return false;
if(allways < n) allways += temp; return isSumOf(s, n);
}
}
我的主类:
public class bdikot
{
public static void main(String[] args){
int [] hi= new int[1];
hi[0]=3;
System.out.println(isSumOf(hi,7));}
}
在此先感谢阿萨夫:-)
答
对于你的第二个问题,我想你需要改变:
System.out.println(Ex14.isSumOf(hi,7));
由于isSumOf
存在于不同类比你的主要方法。你
可能也想看看:
https://en.wikipedia.org/wiki/Programming_style
https://google.github.io/styleguide/javaguide.html
由于以下会让你的代码更容易阅读别人。
该方法属于不同的类。如果它位于不同的类中,则需要指定类名称。 –
顺便说一句,请正确格式化您的代码。阅读很尴尬。 –