程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何找到一组变量的最大值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何找到一组变量的最大值?

开发过程中遇到如何找到一组变量的最大值的问题如何解决?下面主要结合日常开发的经验,给出你关于如何找到一组变量的最大值的解决方法建议,希望对你解决如何找到一组变量的最大值有所启发或帮助;

在Java中,您可以像这样使用Math.max:

double maxStock = Math.max( firstQuarter, Math.max( secondQuarter, Math.max( thirdQuarter, fourthQuarter ) ) );

不是最优雅的,但它会起作用。

另外,为获得更强大的解决方案,请定义以下功能:

private double findMax(double... vals) {
   double max = Double.NEGATIVE_INFINITY;

   for (double d : vals) {
      if (d > max) max = d;
   }

   return max;
}

您可以通过以下方式致电给您:

double maxStock = findMax(firstQuarter, secondQuarter, thirdQuarter, fourthQuarter);

解决方法

我想知道是否有人可以帮助我找到一组变量的最大值并将它们分配给另一个变量。这是我的代码段,可能有助于理解我在说什么。

// Ask for quarter values.
    System.out.println("What is the value of the first quarter?");
    firstQuarter = input.nextDouble();

    System.out.println("What is the value of the second quarter?");
    secondQuarter = input.nextDouble();

    System.out.println("What is the value of the third quarter?");
    thirdQuarter = input.nextDouble();

    System.out.println("What is the value of the fourth quarter?");
    fourthQuarter = input.nextDouble();

    //Tell client the maximum value/price of the stock during the year.     
    //maxStock = This is where I need help 
    System.out.println("The maximum price of a stock share in the year is: $" + maxStock + ".");

大佬总结

以上是大佬教程为你收集整理的如何找到一组变量的最大值全部内容,希望文章能够帮你解决如何找到一组变量的最大值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: