Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在二维数组中搜索最大元素_Java_Arrays - Fatal编程技术网

Java 在二维数组中搜索最大元素

Java 在二维数组中搜索最大元素,java,arrays,Java,Arrays,我试图找出如何在用户输入数组中搜索最大的元素。我的代码如下: ======主要===== package location; import java.util.Scanner; import java.util.Random; /** * * @author daelynghelardini */ public class Location { public static void main(String[] args) { Scanner userI

我试图找出如何在用户输入数组中搜索最大的元素。我的代码如下:

======主要=====

package location;

import java.util.Scanner;
import java.util.Random;

/**
 *
 * @author daelynghelardini
 */

public class Location {
    public static void main(String[] args)
    {
        Scanner userInput = new Scanner(System.in);//scanner for user input

        locationMethod();//instantiates location classes methods

        System.out.println("Would you like to repeat?" + "\n(1) Yes\n(2) No ");//asks user if they wan to repeat

        String theChoice = userInput.next();

        if(theChoice == "1")
        {                               
            locationMethod();//repeats program
        }
        else if (theChoice == "2") 
        {
            System.out.println("Bye");    // does not repeat program
        }            
        else 
        {
            System.out.print("");

            locationMethod();    // repeats program
        }
    }

    public static void locationMethod()
    {
        Scanner userInput = new Scanner(System.in);//scanner for user input
        Random randomThing = new Random();//setting up array
        int columnTotals=0;//instantites column totals
        int rowTotals=0;//instantites row totals

        System.out.println("Please eneter a row amount:");//Requesting row amount
        rowTotals = userInput.nextInt();

        System.out.println("Please enter a column amount: ");//Requesting column amount
        columnTotals=userInput.nextInt();

        int[][] myArray = new int [rowTotals][columnTotals];//sets how rows and columns will print

        for(int rowDigit = 0; rowDigit < myArray.length; rowDigit++)
        {
            for(int columnDigit = 0; columnDigit < myArray[rowDigit].length; 
                                                     columnDigit++)
            {
                myArray[rowDigit][columnDigit]= randomThing.nextInt(10) ;
                System.out.print(myArray[rowDigit][columnDigit] + "") ;//prints multi-dimensional array
            }

            System.out.println();   




        }


    }



}


public class Locator 
{

         public int rows;//instantiates rows
         public int columns;//insantiates colums
     public double maxValue;//instantiates maxValue


     public double locateLargest(int[][] myArray)
     {
         Locator loc = new Locator();
         columns = 0;    // sets columns
         rows = 0;       // sets rows

         maxValue = myArray[0][0];     // sets max value for arrays

         for(int rowtotal=0; rowtotal < myArray.length; rowtotal++)
         {
           for(int columntotal=0; columntotal < myArray.length; columntotal++)
           {
               if(myArray[rowtotal][columntotal]>loc.maxValue)
               {
                   loc.columns=columntotal;//column total
                   loc.rows=rowtotal;//row total
                   loc.maxValue=myArray[rowtotal][columntotal];//sets up array with row total and column total
               }
           }
         }

         System.out.print(loc.maxValue);     
         return loc.maxValue;//returns array

     }



}
包装位置;
导入java.util.Scanner;
导入java.util.Random;
/**
*
*@作者Daelyngheladini
*/
公共类位置{
公共静态void main(字符串[]args)
{
Scanner userInput=新扫描仪(System.in);//用于用户输入的扫描仪
locationMethod();//实例化位置类方法
System.out.println(“是否要重复?”+“\n(1)是\n(2)否”);//询问用户是否要重复
字符串theChoice=userInput.next();
如果(选择==“1”)
{                               
locationMethod();//重复程序
}
否则,如果(选择==“2”)
{
System.out.println(“Bye”);//不重复程序
}            
其他的
{
系统输出打印(“”);
locationMethod();//重复程序
}
}
公共静态空隙定位方法()
{
Scanner userInput=新扫描仪(System.in);//用于用户输入的扫描仪
Random randomThing=new Random();//设置数组
int columnTotals=0;//实例列总计
int rowTotals=0;//实例行总计
System.out.println(“请输入行金额:”;//请求行金额
rowTotals=userInput.nextInt();
System.out.println(“请输入列金额:”;//请求列金额
columnTotals=userInput.nextInt();
int[][]myArray=new int[rowTotals][columnTotals];//设置行和列的打印方式
对于(int rowDigit=0;rowDigitloc.maxValue)
{
loc.columns=columntotal;//列总数
loc.rows=rowtotal;//行总数
loc.maxValue=myArray[rowtotal][columntotal];//使用行总计和列总计设置数组
}
}
}
系统输出打印(定位最大值);
return loc.maxValue;//返回数组
}
}

您可能希望通过在内部循环中获取myarray[0].length来获取列数


for(int columntotal=0;columntotal

这段代码有什么问题?您应该在LocatRangest函数中执行columntotal