java:35:错误:找不到适合add(int)的方法
这是一个添加指示代码的驱动程序 (在//comments中描述)以便它执行以下操作:提示用户输入 数字(整数)由空格和数字0分隔;读取列表中的第一个数字; 输入循环,当数字不是0时,创建NumberProperties对象,并添加 NumberProperties对象创建一个名为numOpsList的ArrayList,然后读取 列表(即,循环迭代)。从列表中读取值0后,循环终止。 现在使用第二个while循环,打印ArrayList中的每个NumberProperties对象 “赔率低于”和“2的权力低于” 我在第35行收到错误:java:35:错误:找不到适合add(int)的方法,java,arraylist,Java,Arraylist,这是一个添加指示代码的驱动程序 (在//comments中描述)以便它执行以下操作:提示用户输入 数字(整数)由空格和数字0分隔;读取列表中的第一个数字; 输入循环,当数字不是0时,创建NumberProperties对象,并添加 NumberProperties对象创建一个名为numOpsList的ArrayList,然后读取 列表(即,循环迭代)。从列表中读取值0后,循环终止。 现在使用第二个while循环,打印ArrayList中的每个NumberProperties对象 “赔率低于”和“
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++;
}
}
}
import java.util.Scanner;
导入java.util.ArrayList;
/**
*演示NumberProperties类。
*/
公共类编号驱动程序{
/**
*从用户处读取一组正数,直到用户输入0为止。
*打印每个数字的赔率和2次幂。
*
*@param args-标准命令行参数
*/
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
//使用泛型类型声明并实例化ArrayList
ArrayList numosList=新的ArrayList();
//提示用户输入一组数字
System.out.println(“输入分隔的正整数列表”
+“后跟空格0:”;
//使用in.nextInt()获取第一个用户输入
int input=in.nextInt();
//添加while循环,如下所述:
//而输入不等于0
//根据用户输入将新NumberProperties对象添加到numOpsList
//使用in.nextInt()获取下一个用户输入
while(输入!=0)
{
添加(输入);
input=in.nextInt();
}
int指数=0;
而(索引
您正在使用通用ArrayList存储NumberProperties
的实例列表。您正在尝试添加一个int
将您的输入转换为NumberOperations实例并添加它,或者只需使用ArrayList
此外,您不需要强制使用0
来标记输入的结束<如果没有可读取的内容,则code>Scanner.hasNext()将返回布尔值false
编辑1
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(2);
System.out.print(list.get(0));
ArrayList list=new ArrayList();
增加(2);
系统输出打印(list.get(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++;
}
}
}
import java.util.Scanner;
导入java.util.ArrayList;
/**
*演示NumberProperties类。
*/
公共类编号驱动程序{
/**
*从用户处读取一组正数,直到用户输入0为止。
*打印每个数字的赔率和2次幂。
*
*@param args-标准命令行参数
*/
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
//使用泛型类型声明并实例化ArrayList
ArrayList numOpsList
=新的ArrayList();
//提示用户输入一组数字
System.out.println(“输入分隔的正整数列表”
+“后跟空格0:”;
//使用in.nextInt()获取第一个用户输入
int input=in.nextInt();
//添加while循环,如下所述:
//而输入不等于0
//根据用户输入将新NumberProperties对象添加到numOpsList
//使用in.nextInt()获取下一个用户输入
while(输入!=0)
{
NumberProperties newOp=新的NumberProperties(输入);
numOpsList.add(newOp);
input=in.nextInt();
}
int指数=0;
而(索引
您的列表是numberops类型,并且您使用int作为输入。说明中说的是“根据用户输入向numOpsList添加新的NumberProperties对象”,而不是“添加用户输入”。因此,您必须根据输入创建一个新的numberroperties
对象。numberroperties
是类和方法的名称,当我将numberroperties
更改为Integer
时,它告诉我找不到方法的符号。是的,他必须使用0来标记输入的结束。这是说明书上说的。
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++;
}
}
}