Java 获取无法找到符号错误。数组列表,迭代器
超市想要奖励当天的顶级客户,即销售额最大的topN客户,其中topN是程序用户提供的值,在超市的屏幕上显示客户的姓名。为此,客户的购买金额存储在ArrayList中 实现一个方法:publicstaticarraylist 编写一个程序,提示出纳输入所有价格和名称,将它们添加到两个数组列表中,调用您实现的方法,并显示结果。使用0的价格作为哨兵 我在编译时的错误是:Java 获取无法找到符号错误。数组列表,迭代器,java,arraylist,iterator,Java,Arraylist,Iterator,超市想要奖励当天的顶级客户,即销售额最大的topN客户,其中topN是程序用户提供的值,在超市的屏幕上显示客户的姓名。为此,客户的购买金额存储在ArrayList中 实现一个方法:publicstaticarraylist 编写一个程序,提示出纳输入所有价格和名称,将它们添加到两个数组列表中,调用您实现的方法,并显示结果。使用0的价格作为哨兵 我在编译时的错误是: ------ Compile ---------- hw1num2.java:44: error: cannot find symb
------ Compile ----------
hw1num2.java:44: error: cannot find symbol
double check = iter.nextDouble();
^
symbol: method nextDouble()
location: variable iter of type Iterator
hw1num2.java:45: error: cannot find symbol
if (check>=sorted(topN-1))
^
symbol: method sorted(int)
location: class hw1num2
hw1num2.java:47: error: no suitable method found for add(double)
topCust.add(check);
^
method ArrayList.add(int,String) is not applicable
(actual and formal argument lists differ in length)
method ArrayList.add(String) is not applicable
(actual argument double cannot be converted to String by method invocatio>n conversion)
3 errors
Output completed (1 sec consumed) - Normal Termination
以下是我所拥有的:
import java.util.*;
public class hw1num2
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
ArrayList<Double> sales = new ArrayList<Double>();
ArrayList<String> customers = new ArrayList<String>();
boolean end = true;
while (end) //Continues to take input for customer names and purchases until done
{
System.out.println("Enter the first name of the customer.");
String cust = in.next();
System.out.println("Enter the purchase total of that customer.");
Double total = in.nextDouble();
sales.add(total);
customers.add(cust);
System.out.println("Enter 1 if you have another customer to add, or 2 if you are done.");
int choice = in.nextInt();
if (choice != 1)
{
end=false;
}
}
System.out.println("How many top customers would you like to display?");
int topN = in.nextInt();
System.out.println(nameOfBestCustomers(sales, customers, topN)); //calls method that computes top custs
}
public static ArrayList<String> nameOfBestCustomers(ArrayList<Double> sales, ArrayList<String> customers,
int topN) //Finds out who the topN customers were
{
ArrayList<Double> sorted = new ArrayList<Double>(sales); //create copy of sales ArrayList
ArrayList<String> topCust = new ArrayList<String>(); //create ArrayList to hold top cust names
Collections.sort(sorted); //sort the copied ArrayList.
Iterator iter = sales.iterator();
while (iter.hasNext()) //iterate through sales ArrayList to find indexes of top purchases
{
for (int i=0;i<sales.size();i++)
{
double check = (double)iter.next();
if (check>=sorted.get(topN-1)) //checks if each index is >= the top Nth customer
{
topCust.add(customers.get(i)); //if so, adds it to topCust list
}
}
}
return topCust; //returns the list with the top customer names
}
}
import java.util.*;
公共类hw1num2
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(系统输入);
ArrayList sales=新建ArrayList();
ArrayList客户=新的ArrayList();
布尔结束=真;
while(end)//继续输入客户名称和购买,直到完成
{
System.out.println(“输入客户的名字”);
字符串cust=in.next();
System.out.println(“输入该客户的购买总额”);
双倍总计=英寸下一个双倍();
销售额。加(合计);
添加(cust);
System.out.println(“如果要添加另一个客户,请输入1;如果完成了,请输入2”);
int choice=in.nextInt();
如果(选项!=1)
{
结束=假;
}
}
System.out.println(“您希望显示多少顶级客户?”);
int topN=in.nextInt();
System.out.println(bestcustomers(sales,customers,topN))的名称;//调用计算顶级客户的方法
}
贝斯特客户的公共静态ArrayList名称(ArrayList销售、ArrayList客户、,
int-topN)//找出topN客户是谁
{
ArrayList sorted=新建ArrayList(销售);//创建销售ArrayList的副本
ArrayList topCust=new ArrayList();//创建ArrayList以保存顶级客户名称
Collections.sort(sorted);//对复制的ArrayList进行排序。
迭代器iter=sales.Iterator();
while(iter.hasNext())//遍历sales ArrayList以查找顶级采购的索引
{
for(int i=0;i=sorted.get(topN-1))//检查每个索引是否>=前n个客户
{
topCust.add(customers.get(i));//如果是,则将其添加到topCust列表中
}
}
}
return topCust;//返回包含最前面的客户名称的列表
}
}
1-出现错误是因为迭代器类没有名为nextDouble()的方法。
请查看支持的方法列表
2-topCust是一个字符串ArrayList,不能直接向该列表中添加double
3-
P:S:您的代码在这里:
int choice = 1;
if (choice != 1)
{
end=false;
}
将在主方法中导致无止境的while循环
希望这能有所帮助。啊,是的,杜尔,我应该与名称arraylist匹配,而不是发回号码。我会先解决这个问题。我也不能对迭代器使用next(),因为它不允许将其分配给双变量…那么使用类型转换。。您可以将iter.next()中的任何内容输入到double。double var=(double)iter.next();尽管如此,如果您有一个Double ArrayList,那么只有在使用双迭代器(如:Iterator