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

时间:2017-01-31 17:46:35

标签: java arrays

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

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

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

     }



}

1 个答案:

答案 0 :(得分:0)

可能你想通过在内循环中使用myarray [0] .length获取列数

for(int columntotal=0; columntotal < myArray[0].length; columntotal++)