0

I need to crate a loop which sums the arrays values which are within the interval (for instance 2-5). My main problem is getting from the first checked value of the array to the next one and so on. Thankyou in advance.

int x=0,y=0,s=0;
int[][] myArray = { {0,1,2,3}, {3,2,1,0}, {3,5,6,1}, {3,8,3,4} };  
Scanner scan = new Scanner(System.in); 
int b = scan.nextInt();//lowest value
int c = scan.nextInt(); //highest value
if (myArray[x][y]>b || myArray[x][y]<c)
    s=s+myArray[x][y]  
//then check next one
4

1 回答 1

0
if (myArray[x][y]>b || myArray[x][y]<c)
    s=s+myArray[x][y]

您需要将它们放在嵌套的 for 循环中。否则,它们只执行一次,使用x=0and y=0。所以你所做的基本上是:

if (myArray[0][0]>b || myArray[0][0]<c)
    s=s+myArray[0][0]

所以s只能是0二维数组中的第一个元素。

现在,这个条件:

if (myArray[x][y]>b || myArray[x][y]<c)

并不意味着你认为它意味着什么。只要小于,这将对所有数字评估为真。对于您正在寻找的语义,您需要一个运算符 ( )。bcAND&&

于 2013-10-08T23:59:43.413 回答