我如何用二维数组工作 [英] How do i work with two dimensional arrays

查看:169
本文介绍了我如何用二维数组工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构造(不良)的二维阵列,以容纳学生姓名和等级。它提示我的学生等于我想量的名字,但它不到风度的成绩。此外,它指出,我有一个索引越界,这点我不明白这是怎么发生的。

 私人无效studentArray(){
            INT行= 0;
            INT COL = 0;            扫描仪输入=新的扫描仪(System.in);            的System.out.println(有多少学生,你呢?);
            行= input.nextInt();            的System.out.println(有多少测试将要录制?);
            COL = input.nextInt();           的String [] [] = schoolArray新的String [行] [COL];           对于(INT行= 0;&行LT;排;排++){
            的System.out.println(请输入学生姓名。);
            schoolArray [行] [0] = input.next();
                对于(INT COLS = 1; COLS<西; COLS ++){
                的System.out.println(请输入成绩。);
                schoolArray [COLS] [0] = input.next();
                } //山坳循环结束
        } //行结束for循环
           printSchool(schoolArray,行,列);
        } // studentArray结束        私人无效printSchool(字符串[] [] schoolArray,诠释行,诠释山口){
            对于(INT行= 0;&行LT; schoolArray.length;排++){
                System.out.print(schoolArray [行] [COL + 1] +\\ t的);
            }
        }


解决方案

我想你已经错写了这个学校[COLS] [0] 它应该是学校[行] [COLS] 山坳内环路

I constructed (poorly) a two dimensional array to hold student names and the grades. It does prompt me for the names of students equal to the amount that i want, but it dosn't for grades. Also it states that i have a index out of bounds, which i dont see how that happened.

private void studentArray() {
            int rows = 0;
            int col = 0;

            Scanner input = new Scanner(System.in);

            System.out.println("How many students do you have?");
            rows = input.nextInt();

            System.out.println("How many tests would you like to record?");
            col = input.nextInt();

           String [][] schoolArray =  new String[rows][col];

           for (int row = 0; row < rows; row++ ){
            System.out.println("Please enter student name.");
            schoolArray[row][0] = input.next();
                for(int cols = 1; cols < col; cols++){
                System.out.println("Please enter grades.");
                schoolArray[cols][0] = input.next();
                }//end of col for loop
        }//end of row for loop
           printSchool (schoolArray, rows, col);


        }//end of studentArray

        private void printSchool(String[][]schoolArray, int rows, int col){
            for ( int row = 0; row < schoolArray.length; row ++) {
                System.out.print(schoolArray[rows][col + 1] + "\t");
            }
        }

解决方案

I think you have written this by mistake school[cols][0] it should be school[row][cols] inside col for loop

这篇关于我如何用二维数组工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆