<tfoot id='lO86B'></tfoot>

    • <bdo id='lO86B'></bdo><ul id='lO86B'></ul>
  1. <legend id='lO86B'><style id='lO86B'><dir id='lO86B'><q id='lO86B'></q></dir></style></legend>

    1. <small id='lO86B'></small><noframes id='lO86B'>

    2. <i id='lO86B'><tr id='lO86B'><dt id='lO86B'><q id='lO86B'><span id='lO86B'><b id='lO86B'><form id='lO86B'><ins id='lO86B'></ins><ul id='lO86B'></ul><sub id='lO86B'></sub></form><legend id='lO86B'></legend><bdo id='lO86B'><pre id='lO86B'><center id='lO86B'></center></pre></bdo></b><th id='lO86B'></th></span></q></dt></tr></i><div id='lO86B'><tfoot id='lO86B'></tfoot><dl id='lO86B'><fieldset id='lO86B'></fieldset></dl></div>
    3. 打印二维数组中的最大数字 - 为什么我的代码打印三个数字

      Print largest number in a 2d array - why do my code print three numbers(打印二维数组中的最大数字 - 为什么我的代码打印三个数字)
        <bdo id='8nwsH'></bdo><ul id='8nwsH'></ul>
        <tfoot id='8nwsH'></tfoot>
      • <legend id='8nwsH'><style id='8nwsH'><dir id='8nwsH'><q id='8nwsH'></q></dir></style></legend>

            • <i id='8nwsH'><tr id='8nwsH'><dt id='8nwsH'><q id='8nwsH'><span id='8nwsH'><b id='8nwsH'><form id='8nwsH'><ins id='8nwsH'></ins><ul id='8nwsH'></ul><sub id='8nwsH'></sub></form><legend id='8nwsH'></legend><bdo id='8nwsH'><pre id='8nwsH'><center id='8nwsH'></center></pre></bdo></b><th id='8nwsH'></th></span></q></dt></tr></i><div id='8nwsH'><tfoot id='8nwsH'></tfoot><dl id='8nwsH'><fieldset id='8nwsH'></fieldset></dl></div>

                <small id='8nwsH'></small><noframes id='8nwsH'>

                  <tbody id='8nwsH'></tbody>
                本文介绍了打印二维数组中的最大数字 - 为什么我的代码打印三个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试打印二维数组中的最大数字.我的问题是我的输出是三个数字而不是一个 - 最大的.为什么?

                I am trying to print out the largest number in a 2D array. My problem is that my output are three numbers instead of one - the largest. Why?

                这是我的代码:

                public class Main {
                
                /**
                 * @param args the command line arguments
                 */
                public static void main(String[] args) {
                
                    int maxRows = 3;
                    int maxCols = 4;
                
                    int [] onedArray = new int [maxRows];
                        for (int i = 0; i < maxRows; i++){
                        onedArray[i] = (int) ((Math.random() * 100) * maxCols);
                    }
                
                    int [][] twodArray = new int[maxRows][];
                        for (int i = 0; i < maxRows; i++){
                        twodArray[i] = new int[maxCols];
                    }
                
                        for (int i = 0; i < twodArray.length; i++){
                        for (int j = 0; j < twodArray[i].length; j++){
                            twodArray[i][j] = (int) (Math.random() * 100);
                        }
                    }
                
                    System.out.println("2 - The 2D array: ");
                    for (int i = 0; i < twodArray.length; i++){
                        for (int j = 0; j < twodArray[i].length; j++){
                            System.out.print(twodArray[i][j] + " ");
                        }
                        System.out.println("");
                    }
                    int maxValue = 1;
                    System.out.println("
                Max values in 2D array: ");
                    for (int i = 0; i < twodArray.length; i++) {
                        for (int j = 0; j < twodArray.length; j++)
                        if (twodArray[i][j] > maxValue) {
                        maxValue = twodArray[i][j];
                        }
                            System.out.println(maxValue);
                        }
                
                
                
                }
                

                }

                推荐答案

                直到最后一个指令序列为止的一切都是正确的(尽管格式很差).

                Everything up until the last sequence of instructions is correct (although poorly formatted).

                这是原文:

                int maxValue = 1;
                System.out.println("
                Max values in 2D array: ");
                for (int i = 0; i < twodArray.length; i++) {
                    for (int j = 0; j < twodArray.length; j++)
                    if (twodArray[i][j] > maxValue) {
                    maxValue = twodArray[i][j];
                    }
                        System.out.println(maxValue);
                    }
                

                这里有更好的版本:

                int maxValue = 0;
                System.out.println("
                Max values in 2D array: ");
                for (int i = 0; i < twodArray.length; i++) {
                    for (int j = 0; j < twodArray[i].length; j++) {
                        if (twodArray[i][j] > maxValue) {
                           maxValue = twodArray[i][j];
                        }
                    }
                    System.out.println("Max value of row " + i + ": " + maxValue);
                }
                

                仔细看,你会发现我在第二个 for 循环之后添加了 { 字符.

                Look carefully and you'll see that I added the { character after the second for-loop.

                如果您想找到总最大值,并最小化打开和关闭花括号,这里是另一个版本:

                If you wanted to find total max, and minimize open and close curly-braces here is another version:

                int maxValue = 0;
                
                System.out.println("
                Max values in 2D array: ");
                for (int i = 0; i < twodArray.length; i++)
                    for (int j = 0; j < twodArray[i].length; j++)
                        if (twodArray[i][j] > maxValue)
                           maxValue = twodArray[i][j];
                
                System.out.println("Maximum value: " + maxValue);
                

                祝你好运.

                这篇关于打印二维数组中的最大数字 - 为什么我的代码打印三个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Compiling C++ for the JVM(为 JVM 编译 C++)
                Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)

                  <legend id='x0HkM'><style id='x0HkM'><dir id='x0HkM'><q id='x0HkM'></q></dir></style></legend>

                      <bdo id='x0HkM'></bdo><ul id='x0HkM'></ul>
                        <tbody id='x0HkM'></tbody>
                    • <small id='x0HkM'></small><noframes id='x0HkM'>

                        1. <tfoot id='x0HkM'></tfoot>
                          <i id='x0HkM'><tr id='x0HkM'><dt id='x0HkM'><q id='x0HkM'><span id='x0HkM'><b id='x0HkM'><form id='x0HkM'><ins id='x0HkM'></ins><ul id='x0HkM'></ul><sub id='x0HkM'></sub></form><legend id='x0HkM'></legend><bdo id='x0HkM'><pre id='x0HkM'><center id='x0HkM'></center></pre></bdo></b><th id='x0HkM'></th></span></q></dt></tr></i><div id='x0HkM'><tfoot id='x0HkM'></tfoot><dl id='x0HkM'><fieldset id='x0HkM'></fieldset></dl></div>