Java 错误:尝试返回arraylist时类型不兼容
我正在努力解决欧拉计划。 我有一个如何完成这个问题的想法,但我遇到了一个问题 错误我已经从不同的问题中搜索了如何使用arraylist 仍然面临问题Java 错误:尝试返回arraylist时类型不兼容,java,Java,我正在努力解决欧拉计划。 我有一个如何完成这个问题的想法,但我遇到了一个问题 错误我已经从不同的问题中搜索了如何使用arraylist 仍然面临问题 import java.util.ArrayList; public class Level_12 { /* The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2
import java.util.ArrayList;
public class Level_12 {
/*
The sequence of triangle numbers is generated by adding the natural numbers.
So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.
The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?
*/
public static ArrayList<Long> check(long num) {
ArrayList<Long> divisors = new ArrayList<Long>();
for (long o = 1; o <= Math.sqrt(num); o++)
if (num % o == 0) {
divisors.add(o);
System.out.println(o + " is a current divisor of " + num);
}
for (Long m : divisors) {
return m;
}
}
public static void main(String[] args) {
long triangle = 0; //Triangle number
long total = 500; //Total divisors
long currenttotal = 0; //Amount of divisors
long i = 0; //Just to itterate
while (currenttotal <= total) {
if (check(i) > currenttotal) { //Finding if the
triangle = i;
if (currenttotal == total) break;
}
i++;
}
System.out.println("The value of the first triangle number to have over 500 divisors is " + triangle);
}
}
import java.util.ArrayList;
公营课程第12级{
/*
三角形数序列是通过添加自然数生成的。
所以第七个三角形的数字是1+2+3+4+5+6+7=28。
前十个任期为:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
让我们列出前七个三角形数字的系数:
1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
我们可以看到28是第一个有超过五个除数的三角形数。
第一个有500个以上除数的三角形数的值是多少?
*/
公共静态数组列表检查(长数值){
ArrayList除数=新的ArrayList();
对于(long o=1;o您在check()
方法中返回long
而不是ArrayList
,而方法的返回类型为ArrayList
,因此请按如下方式更新返回类型:
public static Long check(long num) {
ArrayList<Long> divisors = new ArrayList<Long>();
for (long o = 1; o <= Math.sqrt(num); o++)
if (num % o == 0) {
divisors.add(o);
System.out.println(o + " is a current divisor of " + num);
}
for (Long m : divisors) {
return m;
}
}
公共静态长检查(Long num){
ArrayList除数=新的ArrayList();
对于(长o=1;o而言,罪魁祸首是:
for (Long m : divisors) {
return m;
}
在这种情况下,您将返回一个长值,而方法签名指定返回ArrayList
public static **ArrayList<Long>** check(long num)
公共静态**数组列表**检查(长数值)
您需要更改方法签名或根据您想要完成的操作返回适当的值。您只将m
的除数的一半放入列表中。如果o
除以m
,则m/o
也除以m
/**
* Return a list of the divisors of an integer. If it is a perfect square, the square
* root will be repeated. The list will not be in order.
* @param num The number whose divisors to find.
* @return A list of the number's positive divisors.
*/
public List<Long> divisors(long num) {
final List<Long> divisors = new ArrayList<>();
final long limit = Math.sqrt(num);
for (long o = 1L; o <= limit; o++)
if (num % o == 0) {
divisors.add(o);
divisors.add(num / o);
}
return divisors;
}
/**
* Return the nth triangular number.
* @param n The index of the desired triangular number.
* @return The n-th triangular number 1 + 2 + ... + n
*/
public long triangle(long n) {
return n * (n + 1) / 2;
}
/**
*返回整数的除数列表。如果它是一个完美的平方,则为平方
*将重复根目录。列表将不按顺序排列。
*@param num要查找其除数的数字。
*@返回数字的正除数列表。
*/
公共列表除数(长数值){
最终列表除数=新的ArrayList();
最终长限=数学sqrt(num);
对于(long o=1L;o顺便说一句,错误在“check()”方法中。return m
不返回ArrayList,而是返回long
。这就是问题所在。可能您只是想返回除数
。您不能从一个方法返回多个值(正如您尝试使用return语句查看循环时所做的那样)。返回ArrayList。我已应用了更改,但收到以下错误:“需要类、接口或枚举”@DuelingWarrord您可以发布更改吗please@duelingwarlord您从错误的位置返回ArrayList。请在方法check()的最后一行返回它。谢谢,我没有注意到。
/**
* Return a list of the divisors of an integer. If it is a perfect square, the square
* root will be repeated. The list will not be in order.
* @param num The number whose divisors to find.
* @return A list of the number's positive divisors.
*/
public List<Long> divisors(long num) {
final List<Long> divisors = new ArrayList<>();
final long limit = Math.sqrt(num);
for (long o = 1L; o <= limit; o++)
if (num % o == 0) {
divisors.add(o);
divisors.add(num / o);
}
return divisors;
}
/**
* Return the nth triangular number.
* @param n The index of the desired triangular number.
* @return The n-th triangular number 1 + 2 + ... + n
*/
public long triangle(long n) {
return n * (n + 1) / 2;
}