我必须接收一些身份证号码以及ID号码所售出的盒子数量......将它们分成两个阵列......我已经完成了......然后对阵列进行排序,报告最大的销售量盒子卖了......(这是我的问题)如果输入相同的ID号,添加一起销售的盒子数量,使它只占阵列中的一个点
...示例
idNum 1 = 100
idNum 3 = 500
idNum 1 = 200
so idNum 1 = 300
//declarations
int [][] stupidKids = new int[10][2];//10 rows....2 columns
int entry = 0;
int tempNum;
int tempTotal = 0;
int tempId;
int found = -1;
//user input section
for(int i = 0; i < 2; i++)
{
System.out.print("Please enter the class ID");
Scanner scan = new Scanner(System.in);
stupidKids[i][0] = scan.nextInt();
System.out.print("Please enter the amount of cookies sold");
stupidKids[i][1] = scan.nextInt();
System.out.println(stupidKids[i][0] + "Cookies " + stupidKids[i][1]);
}
//sorting process
for(int i = 0; i < 2; i++)
{
if(stupidKids[i][1] > stupidKids[i+1][1])
{
tempNum = stupidKids[i][1];
stupidKids[i][1] = stupidKids[i + 1][1];
stupidKids[i + 1][1] = tempNum;
tempId = stupidKids[i][0];
stupidKids[i][0] = stupidKids[i+1][0];
stupidKids[i+1][0] = tempId;
}
}
//comparing for same ID Num
for(int i = 0; i < 1; i++)
{
if(stupidKids[i][0] == stupidKids[i+1][0])
{
tempTotal = (stupidKids[i][1] + stupidKids[i+1][1]);
}
}
System.out.println("Final num " + tempTotal);
答案 0 :(得分:0)
你必须循环你的for循环10次而不是两次,这样你就可以通过整个数组。
for(int i = 0; i < 10; i++)
以下是在2D阵列中比较ID的代码。虽然我不太清楚为什么你会希望它每个ID只占用一个空格,因为你最终会在数组中输入很多NULL值,因为你不能轻易地调整数组的大小(为此需要实现基本算法)
for(int i = 0; i < 10; i++)
{
tempID = stupidKids[i][0];
for(int j = 0; j < 10; j++)
{
if( i != j)
{
if(stupidKids[i][0] == stupidKids[j][0])
{
tempTotal = (stupidKids[i][1] + stupidKids[j][1]);
}
}
}
}
System.out.println("Final num " + tempTotal);