将数组中所有数字相加的方法

发布于 2024-12-15 05:13:18 字数 1986 浏览 1 评论 0原文

我需要创建一种方法,将所有衣服、学费、交通、食物、住房和书籍数组加起来达到这一点。 例如,打印输出必须看起来像这样

截至 2011 年 11 月 4 日的费用:

学费:$3200

食物:2600 美元

衣服:600美元

书籍:450 美元

总费用:6850 美元

^这些数字仅作为示例给出,而不是我下面的数字。

这是我的代码

public class Budget{

  ///////////////fields////////////////




  int clothes[]= {100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210};
  int tuition[] = {200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200};
  int transportation[]={100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210};
  int food[]={80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80};
  int housing[]={150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150};
  int books[]= {200, 0, 0, 0, 0, 0, 0, 300, 0, 0, 0, 0};
    int i=0; // this is arbitrary. Never hard code numbers unless that number is never going to change. in that case you make a variable and define it.

  private int expenseName[][] = {clothes, tuition, transportation, food, housing, books};

/////////constructors///////////////
    public Budget() {}

  public Budget(int name) {this.expenseName[i][i] = name;}

    public Budget(int name[], int clothes, int tuition, int transportation, int food, int housing, int books)
    {
      this.expenseName[i] = name;
      this.clothes[i] = clothes;
      this.tuition[i] = tuition;
      this.transportation[i] = transportation;
      this.food[i] = food;
      this.housing[i] = housing;
      this.books[i] = books;
    }


 /////////////methods///////////
public int getexpenseName() {return expenseName[i][i];}

public int getclothes() {return clothes[i];}//change the I
public int gettuition() {return tuition[i];}
public int gettransporation() {return transportation[i];}
public int getfood() {return food[i];}
public int gethousing() {return housing[i];}
public int books() {return books[i];}

public void setExpenseName(int name)
{
  this.expenseName[i][i] = name;
}

I need to create a method where all clothes, tuiton, transportation, food, housing and books arrays add up to that point.
For example a print out has to look something like this

Expenses as of Nov 4, 2011:

Tuition: $3200

Food: $2600

Clothes: $600

Books: $450

Total expenses: $6850

^those numbers are given as an example not the ones i have below.

this is my code

public class Budget{

  ///////////////fields////////////////




  int clothes[]= {100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210};
  int tuition[] = {200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200, 200};
  int transportation[]={100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210};
  int food[]={80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 80};
  int housing[]={150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150};
  int books[]= {200, 0, 0, 0, 0, 0, 0, 300, 0, 0, 0, 0};
    int i=0; // this is arbitrary. Never hard code numbers unless that number is never going to change. in that case you make a variable and define it.

  private int expenseName[][] = {clothes, tuition, transportation, food, housing, books};

/////////constructors///////////////
    public Budget() {}

  public Budget(int name) {this.expenseName[i][i] = name;}

    public Budget(int name[], int clothes, int tuition, int transportation, int food, int housing, int books)
    {
      this.expenseName[i] = name;
      this.clothes[i] = clothes;
      this.tuition[i] = tuition;
      this.transportation[i] = transportation;
      this.food[i] = food;
      this.housing[i] = housing;
      this.books[i] = books;
    }


 /////////////methods///////////
public int getexpenseName() {return expenseName[i][i];}

public int getclothes() {return clothes[i];}//change the I
public int gettuition() {return tuition[i];}
public int gettransporation() {return transportation[i];}
public int getfood() {return food[i];}
public int gethousing() {return housing[i];}
public int books() {return books[i];}

public void setExpenseName(int name)
{
  this.expenseName[i][i] = name;
}

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

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

发布评论

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

评论(1

暮凉 2024-12-22 05:13:19

这是对二维数组中所有整数求和的代码。

int sum = 0;
for (int[] a : expenseName) {
    for (int n : a) {
        sum += n;
    }
}

This is the code to sum all integers in a 2D array.

int sum = 0;
for (int[] a : expenseName) {
    for (int n : a) {
        sum += n;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文