0

I'm given a table of 7 chess players, each of which have played five games of chess 0 = loss, 0.5 = tie, 1 = win. i first have to ask the user a question whether or not he/she wants to fill the arrays with random scores or not, if the user inputs y it should fill in the array with random scores if the user inputs n it should just show the originally inserted scores, additionally, i have to find out and display which people one at least 3 times... i've been sitting with this for quite some time and just don't know what to do anymore, any help would be appreciated

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


    }

}
}
jtwalters
  • 1,024
  • 7
  • 25
Tregan
  • 3
  • 3
  • Hi What is exactly the part that you are having problems? – dreamcrash Dec 29 '20 at 17:44
  • When I run the program, it works up until i have to enter y or n, entering either will result in an error ```Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 at dip107.Md4_201rdb032.main(Md4_201rdb032.java:44)``` – Tregan Dec 29 '20 at 17:54
  • Does this answer your question? [How do I generate random integers within a specific range in Java?](https://stackoverflow.com/questions/363681/how-do-i-generate-random-integers-within-a-specific-range-in-java) – fnklstn Dec 29 '20 at 17:56
  • check this for fixing your out of bound error : https://stackoverflow.com/questions/12231453/syntax-for-creating-a-two-dimensional-array – David Vincent Dec 29 '20 at 18:13

1 Answers1

0

You should create array before set value into, and you switched rows and colums in your iteration. This code should generate random variables or show orignal variables.

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++;             
        }
    } 
  } 
}
Cebul
  • 40
  • 6