我想编写一个从Time数组中选择特定年份(比如2009年)的方法。
这将指(8001),这基本上是2009年的一个关键。
在FactTable中,我想添加所有代表数量( 1s)的数字。因此,它将显示2009年的销售总额。任何人都能知道我是怎么做到的。
Time数组存储{timekey, year, month, week, day}
FactTable存储{clientid, Vehicleid, branchid, timekey, quantity, profit}
int[][] Time = new int[][]
{{8001,2009,1,1,1},
{8002,2010,1,1,7},
{8003,2011,1,1,5},
{8004,2012,1,1,5}};
int[][] FactTable = new int [][]
{{1,125,20,8001,1,2000},
{2,126,40,8001,1,1500},
{3,127,50,8001,1,2500},
{4,128,10,8001,1,2500}};
int sum = 0;
int year = 8001;
for (int i = 0; i< FactTable.length-1; i++)
{
for (int j = 1; j < FactTable[i].length-1; j++)
{
year = year + FactTable + [0][4];
}
}
System.out.println (year); 发布于 2013-12-19 22:06:28
我希望这能帮到你
int year = 2009;
int timeKey = -1;
for (int array[] : Time) {
if (array[1] == year) {
timeKey = array[0];
break;
}
}
int sum = 0;
for (int array[] : FactTable) {
if (array[3] == timeKey) {
sum += array[4];
}
}
System.out.println(sum);发布于 2013-12-19 21:45:02
下面的代码演示如何在多维数组中寻址字段
int sum = 0;
int year = 8001;
for (int i = 0; i< FactTable.length; i++)
{
if (FactTable[i][3] == year) {
sum = sum + FactTable[i][4];
}
}
System.out.println(sum);请注意在for循环中第二个表达式的更正。
发布于 2013-12-19 21:48:37
public class Test1 {
public static void main(String[] args) {
int[][] timeTable = new int[][] { { 8001, 2009, 1, 1, 1 },
{ 8002, 2010, 1, 1, 7 }, { 8003, 2011, 1, 1, 5 },
{ 8004, 2012, 1, 1, 5 } };
int[][] factTable = new int[][] { { 1, 125, 20, 8001, 1, 2000 },
{ 2, 126, 40, 8001, 1, 1500 }, { 3, 127, 50, 8001, 1, 2500 },
{ 4, 128, 10, 8001, 1, 2500 } };
meth(factTable, 2009, timeTable);
}
private static void meth(int[][] factTable, int year, int[][] timeTable) {
int timeKey = -1;
// find the timeKey from the year
for (int[] is : timeTable) { // for each row in timeTable
if (is[1] == year) { // if the year column (duh) is the year
timeKey = is[0]; // get the timekey
break; // we're done
}
}
if (timeKey == -1) return; //timeKey still -1: no timeKey for year
int sum = 0;
for (int[] js : factTable) { // for each row in factTable
if (js[3] == timeKey) { // if the timeKey column is right
sum += js[4]; // add to the sum the quantity
}
}
System.out.println(sum); // done
}
}虽然我真的不知道这些数组是为了什么
https://stackoverflow.com/questions/20692007
复制相似问题