gpt4 book ai didi

java - NumberOpsDriver.java :35: error: no suitable method found for add(int)

转载 作者:行者123 更新时间:2023-11-30 06:53:07 26 4
gpt4 key购买 nike

这是一个添加指定代码的驱动程序

(在//注释中描述),以便执行以下操作:提示用户输入列表数字(整数)以空格分隔,后跟数字 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++;
}
}
}

最佳答案

numOpsList.add(input);

您正在使用通用ArrayList来存储NumberOperations的实例列表。您正在做的就是尝试添加 int

将您的输入转换为 NumbersOperations 实例并添加它或仅使用 ArrayList<Integer>

而且您不必强制使用0来标记输入的结束。 Scanner.hasNext()如果没有任何内容可读取,将返回 boolean 值 false

编辑 1

ArrayList<Integer> list = new ArrayList<Integer>();
list.add(2);
System.out.print(list.get(0));

关于java - NumberOpsDriver.java :35: error: no suitable method found for add(int),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42358557/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com