出在对象数组方面的问题 不知道怎么解决 求助
//我是想做一个类似购买东西的例子,一个很基础的问题,我搞不清楚了import java.io.IOException;
public class gouwu { //声明购物类;
public static int f=20,now=0; //f数组长度,now数组游标
public static huowu hche[]; // 存放货物的 对象数组hche[],
class huowu { //声明货物类
String name;
int count;
int price;
public huowu(String n,int c,int p)
{
this.name=n;
this.count=c;
this.price=p;
}
public huowu(){
this.name=null;
this.count=0;
this.price=0;
}
public void addh(huowu huo)
{
hche[now].name=huo.name;
hche[now].count=huo.count;
hche[now].price=huo.price;
}
}
/**
* @param args
*/
public static void main(String[] args)
{
gouwu gou=new gouwu();
hche=new huowu[f]; //对象数组的创建
int m=1,n=0;
String mn;
for(;now<f&m==0;now++)
{
System.out.println("输入:");
try {
m=System.in.read();
}
catch (IOException e)
{
e.printStackTrace();
}
mn="货物"+String.valueOf(m);
System.out.println("输入价格:");
try {
n=System.in.read();
} catch (IOException e) {
e.printStackTrace();
}
huowu huo=gou.new huowu(mn,m,n); // 创建货物对象
huo.addh(huo); //添加到对象数组中
}
gou.jiaofei();
}
public void jiaofei() //讲数组中的货物总价值进行计算;
{
int sum=0;
for(int i=0;i<=now;i++)
{
sum=sum+(hche[i].count*hche[i].price); //提示是这里有错,我觉得可能是
//没有赋值的原因,该怎么解决?
}
System.out.println("总金额为"+sum);
}
}
----------------解决方案--------------------------------------------------------
莫非是层次太低了吗?大伙看了看也不回。。。我在描述一下 :对象数组就好比一个购物车 每种货物代表一个元素, 每种货物有 名字name 数量count 单价price 程序的过程就是将货物装入购物车(输入0时结束),最后将购物车中所以货物的总价值进行计算。 --程序是非常简单的,只是不知道问题出在哪,我纠结了很久了 望大伙指点迷津啊,不胜感激
----------------解决方案--------------------------------------------------------
好冷清。。为什么没人回呢?我是真找不到问题所在
----------------解决方案--------------------------------------------------------
程序代码:
import java.util.*;
public class Commodity {
private String name;
private int count;
private double price;
public Commodity(String name, int count, double price) {
this.name = name;
this.count = count;
this.price = price;
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
String name;
int count;
double price;
ArrayList<Commodity> cart = new ArrayList<>();
for (int i = 1; ; ++i) {
System.out.print("输入第" + i + "个商品的数量:");
count = reader.nextInt();
if (count == 0)
break;
System.out.print("输入第" + i + "个商品的名字:");
name = reader.next();
System.out.print("输入第" + i + "个商品的单价:");
price = reader.nextDouble();
cart.add(new Commodity(name, count, price));
}
Iterator<Commodity> iter = cart.iterator();
Commodity c;
while (iter.hasNext()) {
c = iter.next();
System.out.println(c.name + ": $" + (c.count * c.price));
}
System.out.println("总价: $" + calc(cart));
}
public static double calc(ArrayList<Commodity> cart) {
double total = 0.0;
Iterator<Commodity> iter = cart.iterator();
Commodity c;
while (iter.hasNext()) {
c = iter.next();
total += c.count * c.price;
}
return total;
}
}
public class Commodity {
private String name;
private int count;
private double price;
public Commodity(String name, int count, double price) {
this.name = name;
this.count = count;
this.price = price;
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
String name;
int count;
double price;
ArrayList<Commodity> cart = new ArrayList<>();
for (int i = 1; ; ++i) {
System.out.print("输入第" + i + "个商品的数量:");
count = reader.nextInt();
if (count == 0)
break;
System.out.print("输入第" + i + "个商品的名字:");
name = reader.next();
System.out.print("输入第" + i + "个商品的单价:");
price = reader.nextDouble();
cart.add(new Commodity(name, count, price));
}
Iterator<Commodity> iter = cart.iterator();
Commodity c;
while (iter.hasNext()) {
c = iter.next();
System.out.println(c.name + ": $" + (c.count * c.price));
}
System.out.println("总价: $" + calc(cart));
}
public static double calc(ArrayList<Commodity> cart) {
double total = 0.0;
Iterator<Commodity> iter = cart.iterator();
Commodity c;
while (iter.hasNext()) {
c = iter.next();
total += c.count * c.price;
}
return total;
}
}
----------------解决方案--------------------------------------------------------