Search code examples
javaarraysmultidimensional-arraydoublenames

how would I use a 2d array to print the quarter and total


Write a program that will display eight players scores in a 4-quarter game. The program should display the players first and last names, uppercasing the first and first letters of the last name. The score is to be entered by the user, but the entry can only hold two digits. The program should total the game score for each player. The output should display the entire multidimensional array

import java.util.Scanner;
import java.util.Arrays;


public class finalScore {

    public static void main(String[] args) {
        
        
        Scanner scan = new Scanner (System.in);
        
        System.out.print("Enter the number of game players: ");
        // using array
        int numOfPlayers = scan.nextInt();
        String[] names = new String[numOfPlayers];
        double[] scores = new double[numOfPlayers];





        
      // using for loop to all the names of the player 

        String list="";
        for (int i=0; i<names.length; i++){
             list += " Quarter " + (i+1) + ": " + scores[i] + " points |||";

        }

        //return;

            // asking user to enter players name
         System.out.print("Enter a players name for quarter 1, " );
          String name;
          name= scan.next();
          // UpperCasing the first letter of the first name
          String result = name.substring(0, 1).toUpperCase()+name.substring(1);       
          String lastName=scan.next();
          // UpperCasing last name 
          String result2=lastName.substring(0, 1).toUpperCase()+lastName.substring(1);
          System.out.println(result + " " +result2);
          
          
          
          
          

       // print name for scores
          names[i] = name;
          // enter name score
          System.out.print("Enter " + result + " "+ result2 +"\'s score: ");
          System.out.println("\n");
          //System.out.println( "Name: \t" + "Q1. ");

         
          
          while(scan.hasNext()) {
            if(scan.hasNextDouble()) {
              double score = scan.nextDouble();
              scores[numOfPlayers] = score;
              break;
            } else {
                  System.out.println( "Name: \t" + "Q1. ");

              System.out.println("ERROR: Invalid Input");
              scan.next();
            }
          }
        }
              
              
              
              

                 
                //System.out.println("      \tQ1. " );

                  
                  //System.out.println(  Arrays.toString(names  ) + "\t" +Arrays.toString(scores));
                  //
                  
        }

this is what the output is supposed to look like and I can't figure it out I'm new do this please help me out

            Q1.    Q2.       Q3     Q4.     Total
Bob Smith.  1       2       3      4        10
Allien Heart 1      2       3      4        10
                  

Solution

  • How about try this out? And you may add some validations by yourself.

    public class finalScore {
    
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            
            System.out.print("Enter the number of game players: ");
            int numOfPlayers = scan.nextInt();
            
            String[] names = new String[numOfPlayers];
            double[][] scores = new double[numOfPlayers][4];
    
            for (int i = 1; i < names.length + 1; i++) {
                System.out.println("Enter a players name for quarter " + i + ": ");
                String name = scan.next();
                String result = name.substring(0, 1).toUpperCase() + name.substring(1);
                String lastName = scan.next();
                String result2 = lastName.substring(0, 1).toUpperCase() + lastName.substring(1);
                System.out.println(result + " " + result2);
                name = result + " " + result2;
                names[i - 1] = name;
                
                // enter scores
                System.out.println("Enter " + names[i - 1] + "\'s score: ");
                System.out.print("Q1: ");
                scores[i-1][0] = scan.nextInt();
                System.out.print("Q2: ");
                scores[i-1][1] = scan.nextInt();
                System.out.print("Q3: ");
                scores[i-1][2] = scan.nextInt();
                System.out.print("Q4: ");
                scores[i-1][3] = scan.nextInt();
                /*
                System.out.print( "Name: " + name + "\t");
                System.out.print( "Q1: " + scores[i-1][0] + "\t");
                System.out.print( "Q2: " + scores[i-1][1] + "\t");
                System.out.print( "Q3: " + scores[i-1][2] + "\t");
                System.out.print( "Q4: " + scores[i-1][3] + "\t");
                System.out.println();
                */
            }
            System.out.format("%15s%15s%15s%15s%15s%15s%n", new String[] {"Name", "Q1", "Q2", "Q3", "Q4", "Total"});
            for (int i = 0; i < names.length; i++) {
                double total = scores[i][0] + scores[i][1] + scores[i][2] + scores[i][3];
                System.out.format("%15s%15s%15s%15s%15s%15s%n", new String[] {names[i], 
                        String.valueOf(scores[i][0]), 
                        String.valueOf(scores[i][1]), 
                        String.valueOf(scores[i][2]), 
                        String.valueOf(scores[i][3]),
                        String.valueOf(total)
                        });
            }
        }
        
    }