Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 方法将数组中的所有数字相加_Java_Arrays_Methods - Fatal编程技术网

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

Java 方法将数组中的所有数字相加,java,arrays,methods,Java,Arrays,Methods,我需要创建一个方法,将所有衣服、推顿、交通、食物、住房和书籍数组相加到这一点。 例如,打印输出必须如下所示 截至2011年11月4日的费用: 学费:3200美元 食品:2 600美元 衣服:600美元 书籍:450美元 费用总额:6 850美元 ^这些数字是作为例子给出的,而不是我下面的数字 这是我的密码 public class Budget{ ///////////////fields//////////////// int clothes[]= {100, 110, 12

我需要创建一个方法,将所有衣服、推顿、交通、食物、住房和书籍数组相加到这一点。 例如,打印输出必须如下所示

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

学费:3200美元

食品:2 600美元

衣服:600美元

书籍:450美元

费用总额:6 850美元

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

这是我的密码

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;
}

这是对2D数组中的所有整数求和的代码

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

这真是糟糕的代码。首先,正确地缩进,其次,必须在类的顶部声明变量。请做这些事情,这样我们才能做出更好的反应。@DhaivatPandya也许一个不同的方法更合适?我们并非都是Java开发者的超级明星。您不必在顶部声明您的属性,这样做既传统又方便。虽然我同意原始代码是。。。次优的,看起来更像导师式的方法可能会更好。