【问题标题】:Need to find a the specific result from an array需要从数组中查找特定结果
【发布时间】: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++;
                
            }
            
            


    }

}
}

【问题讨论】:

标签: java arrays random


【解决方案1】:

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

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++;             
        }
    } 
  } 
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    相关资源
    最近更新 更多