Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 数组中的正结果计数_Java_Arrays - Fatal编程技术网

Java 数组中的正结果计数

Java 数组中的正结果计数,java,arrays,Java,Arrays,好的,所以我有这个问题,我必须输出表中所有值小于5且大于-1的计数行。现在它只输出表和1和2(这是正确的)。代码工作,但我如何让它显示正结果的计数(在本例中为2) 导入java.io.BufferedReader; 导入java.io.IOException; 导入java.io.InputStreamReader; 导入java.util.Random; 公共类Ld4151rdb258{ 公共静态void main(字符串[]args){ int A[][]={ {0,1,2,0,2}, {4

好的,所以我有这个问题,我必须输出表中所有值小于5且大于-1的计数行。现在它只输出表和1和2(这是正确的)。代码工作,但我如何让它显示正结果的计数(在本例中为2)

导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.Random;
公共类Ld4151rdb258{
公共静态void main(字符串[]args){
int A[][]={
{0,1,2,0,2},
{4,4,4,4,4},
{0,-1,8,10,-1},
{0,3,-1,2,1},
{4,8,4,8,12},
{-1,-1,2,0,1},
{1,8,2,4,-1},
{8,16,-1,4,0}
};
int i,j;
int fiveCount,低计数;
char ch='n';
系统输出打印(“填写随机数(y/n)?”;
BufferedReader br=新的BufferedReader(
新的InputStreamReader(System.in)
);
试一试{
ch=(char)br.read();
}
捕获(IOE异常){
System.out.println(“输入输出错误”);
回来
}
随机rnd=新随机();
if(ch=='Y'| ch=='Y'){
对于(i=0;i
int numRows=0;
对于(i=0;i
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Random;

public class Ld4151rdb258 {
    public static void main(String[] args) {
        int A[][] = {
            {0,1,2,0,2},
            {4,4,4,4,4},
            {0,-1,8,10,-1},
            {0,3,-1,2,1},
            {4,8,4,8,12},
            {-1,-1,2,0,1},
            {1,8,2,4,-1},
            {8,16,-1,4,0}
        };
        int i, j;
        int fiveCount, lowCount;
        char ch = 'n';

        System.out.print("fill with random numbers(y/n)? ");
        BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in)
        );

        try {
            ch = (char)br.read();
        }
        catch (IOException e) {
            System.out.println("input-output error");

            return;
        }

        Random rnd = new Random();

        if (ch=='Y' || ch=='y') {
            for (i=0; i<8; i++)
                for (j=0; j<5; j++) 
                    A[i][j] = rnd.nextInt(21);
        } else if (ch != 'N' && ch != 'n') {
            System.out.println("input-output error");

            return;
        }

        for (i=0; i<8; i++) {
            for (j=0; j<5; j++)
                System.out.print(A[i][j] + "\t");
                System.out.println();
        }

        System.out.println("results: ");

        for (i=0; i<8; i++) {
            fiveCount = 0;
            lowCount = 0;

            for (j=0; j<5; j++) {
                if (A[i][j] <= 5) fiveCount++;
                if (A[i][j] == -1)  lowCount++;
            }

            if (fiveCount == 5 && lowCount == 0)
                System.out.print((i+1) + " ");
        }
    }
}    
    int numRows = 0;
    for (i=0; i<8; i++) {
        fiveCount = 0;
        lowCount = 0;

        for (j=0; j<5; j++) {
            if (A[i][j] <= 5) fiveCount++;
            if (A[i][j] == -1)  lowCount++;
        }

        if (fiveCount == 5 && lowCount == 0) {
            System.out.print((i+1) + " ");
            numRows++;
        }
    }
    System.out.println();
    System.out.println("Count of the positive results " + numRows);