【问题标题】:How to search the highest value with Java如何使用 Java 搜索最大值
【发布时间】:2015-06-19 03:20:38
【问题描述】:

我在下面写了代码来搜索一维数组中的最大值。但由于某些错误,它无法正常工作。下面是代码:

   import java.io.*;

public class theHigest{
    int max = 0;
    int[] value = new int[5];
    BufferedReader objInput = new BufferedReader(new InputStreamReader(System.in));

    public static void main(String[]args){
        theHigest obj6 = new theHigest();
        obj6.input();
        }

    void input(){
        try{
            for(int i=0;i<=4;i++){
                    System.out.println("===========================");
                    System.out.print("Value input-"+(i+1));
                    value[i]= Integer.parseInt(objInput.readLine());
                    System.out.println("===========================");
                }
            }
        catch(Exception e){
            System.out.println("Error "+ e);
            }
        }
    }

【问题讨论】:

    标签: java arrays search


    【解决方案1】:

    您尚未实现任何搜索数组中最高元素的功能。您可以在输入函数本身中添加一小段代码。不需要排序。它会导致你 nlogn 而你可以做得更好,只需简单地遍历你的数组一次。它将花费 O(n)。

    void input(){
        try{
            for(int i=0;i<=4;i++){
                    System.out.println("===========================");
                    System.out.print("Value input-"+(i+1));
                    value[i]= Integer.parseInt(objInput.readLine());
                    System.out.println("===========================");
                }
         // searching for highest element in array
            int highest = value[0];
            for(int i=1;i<=4;i++){
                   if(value[i]>highest){
                        highest = value[i];
                     }
                }
            System.out.println("The Highest is :: "+ highest);
            }
        catch(Exception e){
            System.out.println("Error "+ e);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      这是解释概念的参考代码。请参考它并相应地调试您的代码。

      public class FindLargestSmallestNumber {
      
          public static void main(String[] args) {
      
                  //array of 10 numbers
                  int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
      
                  //assign first element of an array to largest and smallest
                  int smallest = numbers[0];
                  int largest = numbers[0];
      
                  for(int i=1; i< numbers.length; i++)
                  {
                          if(numbers[i] > largest)
                                  largest = numbers[i];
                          else if (numbers[i] < smallest)
                                  smallest = numbers[i];
      
                  }
      
                  System.out.println("Largest Number is : " + largest);
                  System.out.println("Smallest Number is : " + smallest);
          }
      

      }

      这个程序的输出是

      最大数是:98

      最小的数字是:23

      【讨论】:

        【解决方案3】:

        您可以使用以下代码找到最大值:

        import java.io.BufferedReader;
        import java.io.InputStreamReader;
        import java.util.Arrays;
        
        public class TheMax {
        
            int max = 0;
            int[] values = new int[5];
            BufferedReader objInput = new BufferedReader(new InputStreamReader(
                    System.in));
        
            public static void main(String[] args) {
                TheMax obj6 = new TheMax();
                obj6.input();
            }
        
            void input() {
                try {
                    for (int i = 0; i <= 4; i++) {
                        System.out.println("===========================");
                        System.out.print("Value input [" + (i + 1) +"] :: ");
                        values[i] = Integer.parseInt(objInput.readLine());
                        System.out.println("===========================");
                    }
                    Arrays.sort(values);
                    System.out.println("The Max is :: "+ values[values.length - 1]);
                } catch (Exception e) {
                    System.out.println("Error " + e);
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2014-11-03
          • 1970-01-01
          • 1970-01-01
          • 2014-01-12
          • 2019-08-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多