这是增加所指示的代码NumberOpsDriver.java:35:错误:找到的附加(INT)没有合适的方法
(在//注释描述),以便它下面的一个驱动器程序:提示用户列出的数字(整数)为 ,后跟数字0;读取列表中的第一个数字; 进入一个循环,当数字不为0时,创建一个NumberOperations对象,将NumberOperations对象添加到一个名为numOpsList的ArrayList中,然后读取 列表中的下一个数字(即迭代循环)。一旦从列表中读取0的值,循环终止。 现在使用第二个while循环,通过 打印ArrayList中的每个NumberOperations对象,其中“赔率下”和其“2下的幂”。
我收到线35的错误:
no suitable method found for add(int)
numOpsList.add(input);
^
method Collection.add(NumberOperations) is not applicable
(argument mismatch; int cannot be converted to NumberOperations)
method List.add(NumberOperations) is not applicable
(argument mismatch; int cannot be converted to NumberOperations)
method AbstractCollection.add(NumberOperations) is not applicable
(argument mismatch; int cannot be converted to NumberOperations)
method AbstractList.add(NumberOperations) is not applicable
(argument mismatch; int cannot be converted to NumberOperations)
method ArrayList.add(NumberOperations) is not applicable
(argument mismatch; int cannot be converted to NumberOperations)"
任何帮助吗?
import java.util.Scanner;
import java.util.ArrayList;
/**
* Demonstrates the NumberOperations class.
*/
public class NumberOpsDriver {
/**
* Reads a set of positive numbers from the user until the user enters 0.
* Prints odds under and powers of 2 under for each number.
*
* @param args - Standard commandline arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// declare and instantiate ArrayList with generic type <NumberOperations>
ArrayList<NumberOperations> numOpsList = new ArrayList<NumberOperations>();
// prompt user for set of numbers
System.out.println("Enter a list of positive integers separated "
+ "with a space followed by 0:");
// get first user input using in.nextInt()
int input = in.nextInt();
// add a while loop as described below:
// while the input is not equal to 0
// add a new NumberOperations object to numOpsList based on user input
// get the next user input using in.nextInt()
while (input != 0)
{
numOpsList.add(input);
input = in.nextInt();
}
int index = 0;
while (index < numOpsList.size()) {
NumberOperations num = numOpsList.get(index);
System.out.println("For: " + num);
System.out.println("\tOdds under:\t" + num.oddsUnder());
System.out.println("\tPowers of 2 under:\t" + num.powersTwoUnder());
index++;
}
}
}
列表是numberops类型和你正在使用INT作为输入 – emotionlessbananas
的指示说“添加基于用户输入一个新的NumberOperations反对numOpsList”,而不是“添加用户输入”。所以你必须根据输入创建一个新的NumberOperations对象。 – ajb