我的循环不起作用 [英] My loop isn't working

查看:69
本文介绍了我的循环不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为课堂制作库存计划。它有一个循环,应循环遍历数组并打印信息。部分循环应该将每个库存项目的值相加,以获得库存的总价值。但是,程序只是简单地将最后输入的值添加到自身。没有编译器错误,但程序没有做它应该做的事情。


这是我的Product类的代码。

I''m making an inventory program for class. It has a loop that should cycle through the array and print the information. Part of the loop is supposed to add the value from each inventory item together to get the total value of the inventory. However, instead the program simply adds the last value entered to itself repeatedly. There is no compiler error, but the program isn''t doing what it should.

Here is my code for the Product class.

展开 < span class =codeDivider> | 选择 | Wrap | Line数字

推荐答案

这个CD变量是什么?你只有一个CD变量,所以当你只在任何时候存储一个时,你如何得到所有记录的总和?


将总数移到前一个循环中它应该总罚款。但是你仍然只能一次存储一张CD。
What is this CD variable? You only have one CD variable so how do you expect to get a sum of all the records when you only store one at any time?

Move the total into the prior loop and it should total fine. But you will still only be storing one CD at a time.


CD是一个由Product类制作的对象。库存[]充满了CD''。我尝试将总计算移动到另一个循环,但它没有工作。


如果你试图将数据一起添加到阵列中,你会怎么做呢。也许我正在以错误的方式解决问题。
CD is an object made from Product class. Inventory[] is filled with CD''s. I tried moving the total calculation to the other loop, but it didn''t work.

If you were trying to add data in array together, how would you go about it. Perhaps I''m tackling the problem the wrong way.


方法很好。只是你使用单张CD来获得一个数组。您没有以任何方式将其链接到库存数组。


您应该总计库存数组中的每个项目或将CD设置为库存数组中的单个项目之前获取价值。


将计算结果移动到另一个循环时使用的代码发布,应该有效。
The method is fine. It''s just that you''re using the single CD to get a total of an array. You haven''t linked it to the inventory array in any way.

You should either total each item in the inventory array or set the CD to a single item in the inventory array before getting the value.

Post the code that you used when you moved the calculation to the other loop, that should have worked.


这篇关于我的循环不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆