我将如何使用二维数组来打印季度和总计

Posted

技术标签:

【中文标题】我将如何使用二维数组来打印季度和总计【英文标题】:how would I use a 2d array to print the quarter and total 【发布时间】:2022-01-16 03:18:29 【问题描述】:

编写一个程序,在 4 节比赛中显示八名球员的得分。该程序应 显示玩家的名字和姓氏,姓氏的首字母和首字母大写。 分数由用户输入,但条目只能包含两位数。该程序 应该合计每个玩家的游戏得分。输出应显示整个 多维数组

导入 java.util.Scanner; 导入 java.util.Arrays;

公共类 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));
              //
              
    

这是输出的样子,我不知道我是新人,请帮帮我 Q1。 Q2。第三季度第四季度。全部的 鲍勃·史密斯。 1 2 3 4 10 异形之心 1 2 3 4 10

【问题讨论】:

你能澄清你必须做什么吗?每个玩家都有4个分数吗?所以你必须读取 8 名球员,每人 4 分,并将他们保存在一个数组中并打印出来? 【参考方案1】:

试试这个怎么样?您可以自己添加一些验证。

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)
                    );
        
    
    

【讨论】:

非常感谢我能弄明白。 您可以将此答案标记为正确。

以上是关于我将如何使用二维数组来打印季度和总计的主要内容,如果未能解决你的问题,请参考以下文章

如何在 cocos2dx 中打印二维数组

如何从文件中加载二维字符数组并使用 ncurses.h 在 C 中使用 mvprintw() 打印它?

c# - 使用 for 循环打印相关行的二维数组

如何将二维字符串数组打印为字符串

如何从 Angular 5 中的二维数组打印表格?

如何在python中打印二维对象数组? [复制]