Java Array

2007-03-24 8:55 am
點樣用一個整數去減一個Java array;當它小于零時就停止.并給出答案(余數)

100 - A[1][2] - A[1][3]- ......

回答 (1)

2007-03-24 12:49 pm
✔ 最佳答案
What is the size of the array? Assuming it's one-dimension.

int result = 100;
for (int ii=0; ii < a.length; ii++)
{
result -= a[ii];
if (result < 0) { return result; }
}

if it's 2 dimension, you need to wrap it with another loop.
Find the size of the 1st dimension and 2nd dimension.

int result = 100;
for (int ii=0; ii < SIZE1; ii++)
{
for (int jj=0; jj < SIZE2; jj++)
{
result -= a[ii][jj];
if (result < 0) { return result; }
}
}


收錄日期: 2021-04-13 15:36:15
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070324000051KK00188

檢視 Wayback Machine 備份