我得到一张由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++;
}
}
}
}
您应该在设置值之前创建数组,然后在迭代中切换行和列。此代码应生成随机变量或显示原始变量。
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++;
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句