java—需要从数组中找到特定的结果

mfpqipee  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(386)

我得到了一张由7名棋手组成的table,每个棋手都下了5盘棋,0=输,0.5=平,1=赢。我首先要问用户一个问题,他/她是否想用随机分数填充数组,如果用户输入y,它应该用随机分数填充数组如果用户输入n,它应该只显示最初插入的分数,另外,我必须找出并显示哪一个人至少3次。。。我已经坐了很长一段时间,只是不知道该做什么了,任何帮助将不胜感激

package dip107;

import java.util.Random;
import java.util.Scanner;

public class Md4_201rdb032 {

    public static void main(String[] args) {
        double A[][] = {{0.5, 0.5, 0.5, 0.5, 0.5},
                {0, 1, 0, 1, 1},
                {0.5, 1, 0.5, 0.5, 0},
                {0, 0.5, 0, 0.5, 0},
                {1, 1, 1, 1, 1},
                {0, 0, 0, 0.5, 0.5},
                {0, 0.5, 0, 0, 1}};

        int i, j;
        int loserCount, winnerCount;
        String ch = "n";

        System.out.println("Dev");
        System.out.print("Aizpildīt masīvu ar patvaļīgām vērtībām (y/n)? ");

        Scanner sc = new Scanner(System.in);
        if (sc.hasNext()) {
                ch = sc.next();

        }

        else {
                System.out.println("input-output error");
                sc.close();
                return;

        }

        sc.close();
        Random rnd = new Random();
        if (ch.equals("Y") || ch.equals("y")) {

            for (i=0; i<5; i++)
                for (j=0; j<7; j++)
                    A[i][j] = rnd.nextInt(3)/2.0;
                }
        else
            if (!ch.equals("N") && !ch.equals("n")) {
                System.out.println("input-output error");
                return;

            }

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

        }

        System.out.println("result:");
        for (i=0; i<5; i++) {
            loserCount = 0;
            winnerCount = 0;
            for (j=0; j<7; j++) {
                if (A[i][j] == 1) winnerCount++;
                if (A[i][j] < 0.5) loserCount++;

            }

    }

}
}
ou6hu8tu

ou6hu8tu1#

您应该在将值设置为之前创建数组,并在迭代中切换行和列。此代码应生成随机变量或显示原始变量。

import java.util.Random;
import java.util.Scanner;

public class Md4_201rdb032 {

public static void main(String[] args) {
    double A[][] = new double[][] 
            {{0.5, 0.5, 0.5, 0.5, 0.5},
            {0, 1, 0, 1, 1},
            {0.5, 1, 0.5, 0.5, 0},
            {0, 0.5, 0, 0.5, 0},
            {1, 1, 1, 1, 1},
            {0, 0, 0, 0.5, 0.5},
            {0, 0.5, 0, 0, 1}};

    int i, j;
    int loserCount, winnerCount;
    String ch = "n";

    System.out.println("Dev");
    System.out.print("Aizpildīt masīvu ar patvaļīgām vērtībām (y/n)? ");

    Scanner sc = new Scanner(System.in);
    if (sc.hasNext()) {
            ch = sc.next();

    }

    else {
            System.out.println("input-output error");
            sc.close();
            return;

    }

    sc.close();
    Random rnd = new Random();
    if (ch.equals("Y") || ch.equals("y")) {

        for (i=0; i<7; i++)
            for (j=0; j<5; j++)
                try {
                A[i][j] = rnd.nextInt(3)/2.0;}
            catch(Exception e) {
                System.out.println(e);
            }
            }
    else
        if (!ch.equals("N") && !ch.equals("n")) {
            System.out.println("input-output error");
            return;

        }

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

    }

  System.out.println("result:");
    for (i=0; i<7; i++) {
        loserCount = 0;
        winnerCount = 0;
        for (j=0; j<5; j++) {
            if (A[i][j] == 1) winnerCount++;
            if (A[i][j] < 0.5) loserCount++;             
        }
    } 
  } 
}

相关问题