如何将数组对象的值相加?

发布于 2024-12-16 21:00:13 字数 462 浏览 0 评论 0原文

我有一个创建了 5 个对象的数组。每个对象都有两个字符串和一个 int。我们将 int 称为“数字”。我如何将每个对象的“数字”添加到最终数字中,假设数字发生变化,所以我不能简单地输入 5 + 3 等。例如

          Question question[] = new Question[5];

 public Constructor()
{
    String1 = "null";
    Sting2 = "null";
    number = 0;
}

,我有五个看起来像这样的对象,它们都有一个不同的价值。数字指的是分数,所以如果用户做对了,数字将被添加到一个变量中,我需要知道当我以类似的方式执行 5 个对象时如何将 5 个变量相加。

  for (i=0; i < Question.length; i++)
 {
   object.dostuff
}

I have an array that created 5 objects. Each object has two strings and a int. Lets call the int "number". How can i add up the "number's" of each object into a final number, assume that the numbers change so i cannot simply just put 5 + 3 etc.. For example

          Question question[] = new Question[5];

 public Constructor()
{
    String1 = "null";
    Sting2 = "null";
    number = 0;
}

SO i have five objects that look like this, they all have a different value. Number refers to a score, So if the user does something right, the number will be added to a variable, i need to know how to add up the 5 variables when i execute the 5 objects in something like.

  for (i=0; i < Question.length; i++)
 {
   object.dostuff
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怼怹恏 2024-12-23 21:00:13

许多事情必须首先发生:

  1. 初始化数组:看来您已经解决了这一问题。
  2. 初始化数组中的对象:确保数组的每个单元格实际上包含一个问题实例(或更准确地说:对问题实例的引用)。
  3. 迭代数组:这里你的循环似乎遍历了类(问题,大写的 Q),但你需要迭代数组(问题的小 q)。一条建议,由于这里的变量问题代表一个问题数组,因此如果您将名称设为复数(问题)以帮助说明这是一个数组,则会更有意义。基本规则是使名称尽可能明确,因此 QuestionArray 会是一个更好的名称。过了某个点,那就是品味问题了。经验法则是,如果您必须查看变​​量的声明,那么它可能没有正确命名。
  4. 访问对象的方法、属性等:当迭代数组时,您需要访问正确的索引(questions[i]),然后访问该对象的成员(questions[i].doStuff)。如果您的目标是面向对象编程(我认为这就是这里的重点),那么您可能希望将明显的操作作为您的 Question 类的函数。然后只需使用适当的参数(questions[i].setNumber(i))调用该函数即可。这完全取决于您需要它做什么。

希望这会有所帮助(如果这是一个与作业相关的问题,您应该这样标记它,这将最大化您在这里获得帮助的机会)。

Many things have to happen first:

  1. Initialize the array: seems you got that one covered.
  2. Initialize objects within the array: Make sure every cell of your array actually contains a question instance (or to be more precise: a reference to a Question instance).
  3. Iterate over the array: here your loop seems to go over the class (Question, with capital Q) but you need to iterate over the array (question with a small q). Piece of advice, since the variable question here represents an array of question it would make more sense if you make your name plural (questions) to help illustrate that this is an array. Basic rule is to make the name as explicit as possible, so questionArray would be an even better name. Past a certain point it's a question of taste. Rule of thumb is that if you have to look at the declaration of the variable then it's probably not named correctly.
  4. access methods, properties etc of the objects: when iterating over the array you need to access the right index (questions[i]) then access the members of this object (questions[i].doStuff). If you aim for OOP (which I assume is the point here) then you may want to make the obvious operations as functions of your Question class. Then simply call this function with the proper parameter (questions[i].setNumber(i)). It all depends on what you need it to do.

Hope this helps (if this is a homework related question you should tag it as such, that would maximize your chance to get help here).

囚你心 2024-12-23 21:00:13

不要使用 Question.length,使用 Question.length

添加一个访问器方法和一个增加分数的方法。

Don't use Question.length, use question.length

Add an accessor method and a method to increment the scores.

戏剧牡丹亭 2024-12-23 21:00:13

使用map从元组列表中提取数字,然后使用reduce对数字进行累加求和。

list=[("1 this is sentence 1","1 this is sentence 2",1),("2 this is sentence 1","2 
this is sentence 2",2),("3 this is sentence 1","3 this is sentence 2",3)]
numbers=map(lambda x: x[2],list)
result=reduce(lambda x,y: x+y,numbers)
print(result)

输出:

6

use map to extract the numbers from the list of tuples then use reduce to accumulatively sum the numbers.

list=[("1 this is sentence 1","1 this is sentence 2",1),("2 this is sentence 1","2 
this is sentence 2",2),("3 this is sentence 1","3 this is sentence 2",3)]
numbers=map(lambda x: x[2],list)
result=reduce(lambda x,y: x+y,numbers)
print(result)

output:

6
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文