2017-02-21 67 views
1

这是增加所指示的代码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++; 
     } 
    } 
} 
+0

列表是numberops类型和你正在使用INT作为输入 – emotionlessbananas

+0

的指示说“添加基于用户输入一个新的NumberOperations反对numOpsList”,而不是“添加用户输入”。所以你必须根据输入创建一个新的NumberOperations对象。 – ajb

回答

1
numOpsList.add(input); 

您使用的是通用的ArrayList存储的NumberOperations实例的列表。你正在做的是试图添加一个int

您的输入转换为NumbersOperations实例,并添加,或干脆使用ArrayList<Integer>

而且你不必强制使用0标记输入的结束。 Scanner.hasNext()将返回布尔值false,如果没有什么阅读

编辑1

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

'NumberOperations'是类和方法的名称,当我将'NumberOperations'更改为'Integer'时,它告诉我它找不到方法的符号 –

+0

是的,他必须使用0来标记输入。这就是说明所要做的。 – ajb

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++; 
     } 
    } 
} 
相关问题