这是一个添加指定代码的驱动程序
(在//注释中描述),以便它执行以下操作:提示用户输入列表 数字(整数)用空格分隔后跟数字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++;
}
}
}
答案 0 :(得分:1)
*ppstrId
您正在使用通用ArrayList来存储numOpsList.add(input);
的实例列表。你正在做的是尝试添加NumberOperations
。
将您的输入转换为NumbersOperations实例并添加它,或者只使用int
此外,您无需强制使用ArrayList<Integer>
来标记输入的结尾。如果没有要阅读的内容,0
将返回布尔值false
编辑1
Scanner.hasNext()
答案 1 :(得分:0)
我让它正常工作,我必须做@ajb所说的。
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)
{
NumberOperations newOp = new NumberOperations(input);
numOpsList.add(newOp);
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++;
}
}
}