【发布时间】:2021-04-06 07:40:39
【问题描述】:
给了我一张由 7 名棋手组成的桌子,每名棋手都下过 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++;
}
}
}
}
【问题讨论】:
-
嗨,您遇到问题的具体部分是什么?
-
当我运行程序时,它会一直运行,直到我必须输入 y 或 n,输入任何一个都会导致错误
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 at dip107.Md4_201rdb032.main(Md4_201rdb032.java:44) -
检查这个以修复您的越界错误:stackoverflow.com/questions/12231453/…