java - 表达式的类型必须是数组类型,但它解析为对象 java
问题描述
我想从每个bills[][]
数组中打印出索引 1 处的数字。例如,7 英寸bills[0][1] = new Bill("Carrot", 7, 2000)
。它说“表达式的类型必须是数组类型,但它解析为比尔。” 我应该怎么办?
package Module6;
public class Bill {
String name;
int numberCheckedOutPerItem;
int pricePerItem;
Bill (String name, int ...v){
this.name = name;
this.numberCheckedOutPerItem =numberCheckedOutPerItem;
this.pricePerItem = pricePerItem;
}
}
package Module6;
public class CashRegister {
private Bill[][] bills;
CashRegister(){
bills = new Bill[4][3];
bills[0][0] = new Bill("Cucumber", 4, 5000);
bills[0][1] = new Bill("Carrot", 7, 2000);
bills[0][2] = new Bill("Onion", 11, 1500);
bills[1][0] = new Bill("Cucumber", 8, 5000);
bills[1][1] = new Bill("Carrot", 18, 2000);
bills[1][2] = new Bill("Onion", 3, 1500);
bills[2][0] = new Bill("Cucumber", 2, 5000);
bills[2][1] = new Bill("Carrot", 7, 2000);
bills[2][2] = new Bill("Onion", 6, 1500);
bills[3][0] = new Bill("Cucumber", 9, 5000);
bills[3][1] = new Bill("Carrot", 13, 2000);
bills[3][2] = new Bill("Onion", 20, 1500);
}
**void getItems() {
int totalNum=0;
for (int i=0; i<bills.length; i++) {
for(int j=0; j<bills[i].length; j++) {
for(int k=0; k<bills[i][j].length; k++) {
totalNum = totalNum + (int) bills[i][j][2];
}**
}
}
}
解决方案
推荐阅读
- django - 如何在公司服务器 (Windows Server) 上部署 Django Web 应用程序
- reactjs - 使用 Nextjs SSR 将链接的 svg 图像转为内联
- wordpress - 如何在我的 EC2 实例中安装 simpleXm、XMLWriter、curl、zlib、OpenSSL(Amazon Linux 2 AMI (HVM)、SSD 卷类型)
- octave - 图未在 Cmder 调用 octave 脚本中显示
- r - 存储以下 R 代码的嵌套 for 循环的输出
- javascript - 尝试在javascript中更改RGB颜色
- azure - Azure Depolyment 的 Azure 标记不起作用
- java - 类 `OidcTenantConfig` 无法解析,为什么?
- android - Android 30 (11) 文件访问
- javascript - 如何使用 NestJs 创建服务的单个实例