查找数组中最大值的Java程序正在打印多个值

发布于 2024-08-13 02:59:28 字数 1223 浏览 4 评论 0原文

由于某种原因,当我尝试仅打印一个值(即 11.3)时,此代码会打印数组中最高值的三个值。有人可以向我解释一下为什么这样做吗?

import java.util.Scanner;

public class Slide24
{
    public static void main (String [] args)
    {
        Scanner in = new Scanner(System.in);
        
        double[] decMax = {-2.8, -8.8, 2.3, 7.9, 4.1, -1.4, 11.3, 10.4,
            8.9, 8.1, 5.8, 5.9, 7.8, 4.9, 5.7, -0.9, -0.4, 7.3, 8.3, 6.5, 9.2,
            3.5, 3, 1.1, 6.5, 5.1, -1.2, -5.1, 2, 5.2, 2.1};
        
        double total = 0, avgMax = 0;
        
        for (int counter = 0; counter < decMax.length; counter++)
        {
         total += decMax[counter];
        }
        
        avgMax = total / decMax.length;
                
        System.out.printf("%s %2.2f\n", "The average maximum temperature for December was: ", avgMax);
        
        //finds the highest value
        double max = decMax[0];
        
        for (int counter = 1; counter < decMax.length; counter++)
        {
         if (decMax[counter] > max)
         {
          max = decMax[counter];
          System.out.println("The highest maximum for the December is: " + max);
         }
         
        }        
    }
}

For some reason this code is printing three values for the highest value in the array when I'm trying to print just one (which is 11.3). Can someone please explain to me why it is doing this?

import java.util.Scanner;

public class Slide24
{
    public static void main (String [] args)
    {
        Scanner in = new Scanner(System.in);
        
        double[] decMax = {-2.8, -8.8, 2.3, 7.9, 4.1, -1.4, 11.3, 10.4,
            8.9, 8.1, 5.8, 5.9, 7.8, 4.9, 5.7, -0.9, -0.4, 7.3, 8.3, 6.5, 9.2,
            3.5, 3, 1.1, 6.5, 5.1, -1.2, -5.1, 2, 5.2, 2.1};
        
        double total = 0, avgMax = 0;
        
        for (int counter = 0; counter < decMax.length; counter++)
        {
         total += decMax[counter];
        }
        
        avgMax = total / decMax.length;
                
        System.out.printf("%s %2.2f\n", "The average maximum temperature for December was: ", avgMax);
        
        //finds the highest value
        double max = decMax[0];
        
        for (int counter = 1; counter < decMax.length; counter++)
        {
         if (decMax[counter] > max)
         {
          max = decMax[counter];
          System.out.println("The highest maximum for the December is: " + max);
         }
         
        }        
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

弱骨蛰伏 2024-08-20 02:59:28

每当它找到一个高于当前最大值的数字时,它就会打印出一个数字(在您的情况下,这种情况发生了三次。)将打印移到 for 循环之外,您应该会很好。

for (int counter = 1; counter < decMax.length; counter++)
{
     if (decMax[counter] > max)
     {
           max = decMax[counter];
     }
}

System.out.println("The highest maximum for the December is: " + max);

It's printing out a number every time it finds one that is higher than the current max (which happens to occur three times in your case.) Move the print outside of the for loop and you should be good.

for (int counter = 1; counter < decMax.length; counter++)
{
     if (decMax[counter] > max)
     {
           max = decMax[counter];
     }
}

System.out.println("The highest maximum for the December is: " + max);
单身狗的梦 2024-08-20 02:59:28

要从数组中查找最高(最大值)或最低(最小值)值,这可以为您提供正确的方向。以下是从原始数组中获取最高值的示例代码。

方法1:

public int maxValue(int array[]){
  List<Integer> list = new ArrayList<Integer>();
  for (int i = 0; i < array.length; i++) {
    list.add(array[i]);
  }
  return Collections.max(list);
}

要获得最低值,您可以使用

Collections.min(list)

方法 2:

public int maxValue(int array[]){
  int max = Arrays.stream(array).max().getAsInt();
  return max;
}

现在下面的行应该可以工作。

System.out.println("The highest maximum for the December is: " + maxValue(decMax)); 

To find the highest (max) or lowest (min) value from an array, this could give you the right direction. Here is an example code for getting the highest value from a primitive array.

Method 1:

public int maxValue(int array[]){
  List<Integer> list = new ArrayList<Integer>();
  for (int i = 0; i < array.length; i++) {
    list.add(array[i]);
  }
  return Collections.max(list);
}

To get the lowest value, you can use

Collections.min(list)

Method 2:

public int maxValue(int array[]){
  int max = Arrays.stream(array).max().getAsInt();
  return max;
}

Now the following line should work.

System.out.println("The highest maximum for the December is: " + maxValue(decMax)); 
情丝乱 2024-08-20 02:59:28

扫描完所有内容后,您需要打印出最大值:

for (int counter = 1; counter < decMax.length; counter++)
{
    if (decMax[counter] > max)
    {
        max = decMax[counter];
        // not here: System.out.println("The highest maximum for the December is: " + max);
    }
}  
System.out.println("The highest maximum for the December is: " + max);

You need to print out the max after you've scanned all of them:

for (int counter = 1; counter < decMax.length; counter++)
{
    if (decMax[counter] > max)
    {
        max = decMax[counter];
        // not here: System.out.println("The highest maximum for the December is: " + max);
    }
}  
System.out.println("The highest maximum for the December is: " + max);
司马昭之心 2024-08-20 02:59:28

如果您正在寻找最快、最简单的方法来执行有关数组的各种操作,那么使用 Collections 类非常有帮助(文档可从 https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html),操作范围包括查找最大值、最小值、排序、倒序等。

使用集合从数组中查找最大值的简单方法:

Double[] decMax = {-2.8, -8.8, 2.3, 7.9, 4.1, -1.4, 11.3, 10.4, 8.9, 8.1, 5.8, 5.9, 7.8, 4.9, 5.7, -0.9, -0.4, 7.3, 8.3, 6.5, 9.2, 3.5, 3.0, 1.1, 6.5, 5.1, -1.2, -5.1, 2.0, 5.2, 2.1};
List<Double> a = new ArrayList<Double>(Arrays.asList(decMax));
System.out.println("The highest maximum for the December is: " + Collections.max(a));

如果您有兴趣查找最小值,类似于查找最大值:

System.out.println(Collections.min(a));

最简单line 对列表进行排序:

Collections.sort(a);

或者使用 Arrays 类对数组进行排序:

Arrays.sort(decMax);

但是 Arrays 类没有直接引用最大值的方法,对其进行排序并引用最后一个索引是最大值,但是请记住,通过上述 2 种方法进行排序的复杂度为 O(n log n)。

If you are looking for the quickest and simplest way to perform various actions in regards to arrays, the use of the Collections class is extremely helpful (documentation available from https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html), actions ranges from finding the maximum, minimum, sorting, reverse order, etc.

A simple way to find the maximum value from the array with the use of Collections:

Double[] decMax = {-2.8, -8.8, 2.3, 7.9, 4.1, -1.4, 11.3, 10.4, 8.9, 8.1, 5.8, 5.9, 7.8, 4.9, 5.7, -0.9, -0.4, 7.3, 8.3, 6.5, 9.2, 3.5, 3.0, 1.1, 6.5, 5.1, -1.2, -5.1, 2.0, 5.2, 2.1};
List<Double> a = new ArrayList<Double>(Arrays.asList(decMax));
System.out.println("The highest maximum for the December is: " + Collections.max(a));

If you are interested in finding the minimum value, similar to finding maximum:

System.out.println(Collections.min(a));

The simplest line to sort the list:

Collections.sort(a);

Or alternatively the use of the Arrays class to sort an array:

Arrays.sort(decMax);

However the Arrays class does not have a method that refers to the maximum value directly, sorting it and referring to the last index is the maximum value, however keep in mind sorting by the above 2 methods has a complexity of O(n log n).

别在捏我脸啦 2024-08-20 02:59:28

与其他人的建议相同,只是提到更清洁的方法:

int max = decMax[0];
for(int i=1;i<decMax.length;i++)
    max = Math.max(decMax[i],max);
System.out.println("The Maximum value is : " + max);

Same as suggested by others, just mentioning the cleaner way of doing it:

int max = decMax[0];
for(int i=1;i<decMax.length;i++)
    max = Math.max(decMax[i],max);
System.out.println("The Maximum value is : " + max);
寄居人 2024-08-20 02:59:28

获得数组最大值的更短解决方案:

double max = Arrays.stream(decMax).max(Double::compareTo).get();

A shorter solution to have the max value of array:

double max = Arrays.stream(decMax).max(Double::compareTo).get();
合约呢 2024-08-20 02:59:28

您的 print() 语句位于 for() 循环中,它应该位于之后,以便只打印一次。按照目前的方式,每次最大值更改时都会打印一个 max

You have your print() statement in the for() loop, It should be after so that it only prints once. the way it currently is, every time the max changes it prints a max.

ゞ花落谁相伴 2024-08-20 02:59:28

如果你不想使用任何java预定义库那么下面是

最简单的方法

   public class Test {

    public static void main(String[] args) {
        double[] decMax = {-2.8, -8.8, 2.3, 7.9, 4.1, -1.4, 11.3, 10.4,
            8.9, 8.1, 5.8, 5.9, 7.8, 4.9, 5.7, -0.9, -0.4, 7.3, 8.3, 6.5, 9.2,
            3.5, 3, 1.1, 6.5, 5.1, -1.2, -5.1, 2, 5.2, 2.1};

        double maxx = decMax[0];

        for (int i = 0; i < decMax.length; i++) {
            if (maxx < decMax[i]) {
                maxx = decMax[i];
            }
        }
        System.out.println(maxx);

    }
}

If you don't want to use any java predefined libraries then below is the

simplest way

   public class Test {

    public static void main(String[] args) {
        double[] decMax = {-2.8, -8.8, 2.3, 7.9, 4.1, -1.4, 11.3, 10.4,
            8.9, 8.1, 5.8, 5.9, 7.8, 4.9, 5.7, -0.9, -0.4, 7.3, 8.3, 6.5, 9.2,
            3.5, 3, 1.1, 6.5, 5.1, -1.2, -5.1, 2, 5.2, 2.1};

        double maxx = decMax[0];

        for (int i = 0; i < decMax.length; i++) {
            if (maxx < decMax[i]) {
                maxx = decMax[i];
            }
        }
        System.out.println(maxx);

    }
}
断爱 2024-08-20 02:59:28

您可以使用接受数组并查找其中最大值的函数。我将其设为通用,因此它也可以接受其他数据类型

public static <T extends Comparable<T>> T findMax(T[] array){       
    T max = array[0];
    for(T data: array){
        if(data.compareTo(max)>0)
            max =data;                
    }
    return max;
}

You can use a function that accepts a array and finds the max value in it. i made it generic so it could also accept other data types

public static <T extends Comparable<T>> T findMax(T[] array){       
    T max = array[0];
    for(T data: array){
        if(data.compareTo(max)>0)
            max =data;                
    }
    return max;
}
吲‖鸣 2024-08-20 02:59:28

你可以这样写。

import java.util.Scanner;
class   BigNoArray{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter how many array element");
        int n=sc.nextInt();
        int[] ar= new int[n];
        System.out.println("enter "+n+" values");
        for(int i=0;i<ar.length;i++){
            ar[i]=sc.nextInt();
        }
        int fbig=ar[0];
        int sbig=ar[1];
        int tbig=ar[3];
            for(int i=1;i<ar.length;i++){
                if(fbig<ar[i]){
                    sbig=fbig;
                    fbig=ar[i];
                }
                else if(sbig<ar[i]&&ar[i]!=fbig){
                    sbig=ar[i];
                }
                else if(tbig<ar[i]&&ar[i]!=fbig){
                    tbig=ar[i];
                }
            }
        System.out.println("first big number is "+fbig);
        System.out.println("second big number is "+sbig);
        System.out.println("third big number is "+tbig);
    }
}

You can write like this.

import java.util.Scanner;
class   BigNoArray{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter how many array element");
        int n=sc.nextInt();
        int[] ar= new int[n];
        System.out.println("enter "+n+" values");
        for(int i=0;i<ar.length;i++){
            ar[i]=sc.nextInt();
        }
        int fbig=ar[0];
        int sbig=ar[1];
        int tbig=ar[3];
            for(int i=1;i<ar.length;i++){
                if(fbig<ar[i]){
                    sbig=fbig;
                    fbig=ar[i];
                }
                else if(sbig<ar[i]&&ar[i]!=fbig){
                    sbig=ar[i];
                }
                else if(tbig<ar[i]&&ar[i]!=fbig){
                    tbig=ar[i];
                }
            }
        System.out.println("first big number is "+fbig);
        System.out.println("second big number is "+sbig);
        System.out.println("third big number is "+tbig);
    }
}
迷离° 2024-08-20 02:59:28
void FindMax()
{
    int lessonNum;

    System.out.print("Enter your lesson numbers : ");
    lessonNum = input.nextInt();
    int[] numbers = new int[lessonNum];
    for (int i = 0; i < numbers.length; i++)
    {
        System.out.print("Please enter " + (i + 1) + " number : ");
        numbers[i] = input.nextInt();
    }
    double max = numbers[0];
    for (int i = 1; i < numbers.length; i++)
    {
        if (numbers[i] > max)
        {
            max = numbers[i];
        }
    }
    System.out.println("Maximum number is : " + max);
}
void FindMax()
{
    int lessonNum;

    System.out.print("Enter your lesson numbers : ");
    lessonNum = input.nextInt();
    int[] numbers = new int[lessonNum];
    for (int i = 0; i < numbers.length; i++)
    {
        System.out.print("Please enter " + (i + 1) + " number : ");
        numbers[i] = input.nextInt();
    }
    double max = numbers[0];
    for (int i = 1; i < numbers.length; i++)
    {
        if (numbers[i] > max)
        {
            max = numbers[i];
        }
    }
    System.out.println("Maximum number is : " + max);
}
吻安 2024-08-20 02:59:28

您只需将第零个元素与其余元素进行比较,以便它将打印它将包含的最后一个最大值,在您的情况下,会发生同样的问题。要比较每个元素,我们必须交换值,例如:

double max = decMax[0];
    for (int counter = 1; counter < decMax.length; counter++){
        if(max<decMax[i]){
            max=decMax[i]; //swapping
            decMax[i]=decMax[0];
        }
    }
    System.out.println("The max value is "+ max);

希望这对您有帮助

You are just Compare zeroth element with rest of the elements so it will print the value last largest value which it will contain, in you case, same problem is happening. To compare each and every elements we have to swap the values like:

double max = decMax[0];
    for (int counter = 1; counter < decMax.length; counter++){
        if(max<decMax[i]){
            max=decMax[i]; //swapping
            decMax[i]=decMax[0];
        }
    }
    System.out.println("The max value is "+ max);

Hope this will help you

梦里南柯 2024-08-20 02:59:28

我发现的最简单的方法,支持所有Android版本

Arrays.sort(series1Numbers);

int maxSeries = Integer.parseInt(String.valueOf(series1Numbers[series1Numbers.length-1]));

Easiest way which I've found, supports all android versions

Arrays.sort(series1Numbers);

int maxSeries = Integer.parseInt(String.valueOf(series1Numbers[series1Numbers.length-1]));
咽泪装欢 2024-08-20 02:59:28

不使用集合的简单方法

public void findHighestNoInArray() {
        int[] a = {1,2,6,8,9};
        int large = a[0];
            for(int num : a) {
                if(large < num) {
                    large = num;
                }
            }
            System.out.println("Large number is "+large+"");
        }

An easy way without using collections

public void findHighestNoInArray() {
        int[] a = {1,2,6,8,9};
        int large = a[0];
            for(int num : a) {
                if(large < num) {
                    large = num;
                }
            }
            System.out.println("Large number is "+large+"");
        }
此生挚爱伱 2024-08-20 02:59:28
import java.util.*;
class main9 //Find the smallest and 2lagest and  ascending and descending order of  elements in array//
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the array range");
        int no=sc.nextInt();
        System.out.println("Enter the array element");
        int a[]=new int[no];
        int i;
        for(i=0;i<no;i++)
        {
            a[i]=sc.nextInt();
        }
        Arrays.sort(a);
        int s=a[0];
        int l=a[a.length-1];
        int m=a[a.length-2];
        System.out.println("Smallest no is="+s);
        System.out.println("lagest 2 numbers are=");
        System.out.println(l);
        System.out.println(m);
        System.out.println("Array in ascending:");
        for(i=0;i<no;i++)
        {
            System.out.println(a[i]);
        }
        System.out.println("Array in descending:");
        for(i=a.length-1;i>=0;i--)
        {
            System.out.println(a[i]);
        }
    }
}
import java.util.*;
class main9 //Find the smallest and 2lagest and  ascending and descending order of  elements in array//
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the array range");
        int no=sc.nextInt();
        System.out.println("Enter the array element");
        int a[]=new int[no];
        int i;
        for(i=0;i<no;i++)
        {
            a[i]=sc.nextInt();
        }
        Arrays.sort(a);
        int s=a[0];
        int l=a[a.length-1];
        int m=a[a.length-2];
        System.out.println("Smallest no is="+s);
        System.out.println("lagest 2 numbers are=");
        System.out.println(l);
        System.out.println(m);
        System.out.println("Array in ascending:");
        for(i=0;i<no;i++)
        {
            System.out.println(a[i]);
        }
        System.out.println("Array in descending:");
        for(i=a.length-1;i>=0;i--)
        {
            System.out.println(a[i]);
        }
    }
}
何处潇湘 2024-08-20 02:59:28

找到最大数量的简单方法

   int arr[] = {10, 11, 12, 13, 55, 18, 20};
    int num = arr.length;
    int max = 0;

    for (int i = 0; i < num; i++) {
        if (arr[i] > max) {
            max = arr[i];

        }

    }
    System.out.println(max);

simple way find max number

   int arr[] = {10, 11, 12, 13, 55, 18, 20};
    int num = arr.length;
    int max = 0;

    for (int i = 0; i < num; i++) {
        if (arr[i] > max) {
            max = arr[i];

        }

    }
    System.out.println(max);
国际总奸 2024-08-20 02:59:28
    package Loops;
    import java.util.Scanner;
    public class LargestNumber {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("How many numbers you want to enter?\n");
            int x = sc.nextInt();
            int[] data = new int[x];
            for (int i = 0; i < data.length; i++) {
                System.out.println("Enter the number " + (i + 1));
                data[i] = sc.nextInt();
            }
            int max = data[0];
            int min = data[0];
            int i;
            for (i = 1; i < data.length; i++) {
                if (data[i] > max) {
                    max = data[i];
                } else if (data[i] < min) {
                    min = data[i];
                }
            }
            System.out.println("The highest number in array is: " + max);
            System.out.println("The smallest number in array is: " + min);
        }
    }
    package Loops;
    import java.util.Scanner;
    public class LargestNumber {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("How many numbers you want to enter?\n");
            int x = sc.nextInt();
            int[] data = new int[x];
            for (int i = 0; i < data.length; i++) {
                System.out.println("Enter the number " + (i + 1));
                data[i] = sc.nextInt();
            }
            int max = data[0];
            int min = data[0];
            int i;
            for (i = 1; i < data.length; i++) {
                if (data[i] > max) {
                    max = data[i];
                } else if (data[i] < min) {
                    min = data[i];
                }
            }
            System.out.println("The highest number in array is: " + max);
            System.out.println("The smallest number in array is: " + min);
        }
    }
萌吟 2024-08-20 02:59:28
    int[] data2 = {1,10,50,70};
    List<Integer> testing= new ArrayList<>();
    for(int s=0;s<data.length;s++){
        testing.add(data[s]);
                }
    int t= Collections.max(testing);
    int mini= Collections.min(testing);
    System.out.println("Max is:"+t);
    System.out.println("Minimum  is:"+mini);
    int[] data2 = {1,10,50,70};
    List<Integer> testing= new ArrayList<>();
    for(int s=0;s<data.length;s++){
        testing.add(data[s]);
                }
    int t= Collections.max(testing);
    int mini= Collections.min(testing);
    System.out.println("Max is:"+t);
    System.out.println("Minimum  is:"+mini);
寂寞清仓 2024-08-20 02:59:28
function maxElement(values){
  var max = values[0]; // Initialize maximum element
  for (value of values){ //Traverse array elements from second and compare every element with curr`enter code here`ent max 
    if (value > max) {
      max = value //new maximum
    }
  }
  return max;
}
function maxElement(values){
  var max = values[0]; // Initialize maximum element
  for (value of values){ //Traverse array elements from second and compare every element with curr`enter code here`ent max 
    if (value > max) {
      max = value //new maximum
    }
  }
  return max;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文