将数组中所有数字相加的方法
我需要创建一种方法,将所有衣服、学费、交通、食物、住房和书籍数组加起来达到这一点。 例如,打印输出必须看起来像这样
截至 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是对二维数组中所有整数求和的代码。
This is the code to sum all integers in a 2D array.