简体   繁体   English

在 Java 中查找不带数组的最小值和最大值

[英]Find minimum and maximum value WITHOUT ARRAY in Java

Doing a project for AP Computer science, and I still haven't managed to figure out how to do this part:做AP Computer science的一个项目,这部分我还是没搞明白怎么做:

The program is supposed to ask the user for 5 test score inputs and then the code will "curve" the smallest value entered by taking the square root of the score and adding the integer value of the square root to the original score.该程序应该要求用户输入 5 个测试分数,然后代码将通过取分数的平方根并将平方根的整数值添加到原始分数来“弯曲”输入的最小值。 I have figured out how to do the math and everything else, but I just have no clue how to find the smallest score entered.我已经弄清楚如何做数学和其他所有事情,但我不知道如何找到输入的最小分数。 The teacher specifically said no arrays or collections.老师特别说没有数组或集合。

My int variables are score1, score2, score3, score4, and score5.我的 int 变量是 score1、score2、score3、score4 和 score5。 Say the user entered 75 for score1, 32 for score2, 42 for score3, 99 for score4, and 100 for score 5. How would I make it do the curve function (that I've already made) for score2 specifically, since it's the minimum value.假设用户为分数 1 输入 75,为分数 2 输入 32,为分数 3 输入 42,为分数 4 输入 99,为分数 5 输入 100。我将如何让它为分数 2 做曲线函数(我已经做了),因为它是最小值。 I've already set up the scanners and everything, I just don't know how to get the minimum (and maximum) values.我已经设置了扫描仪和所有东西,我只是不知道如何获得最小值(和最大值)。

Thank you!谢谢!

I've seen this before.我以前见过这个。 (hehe) (呵呵)

Key here is Math class.这里的关键是Math课。 Math.Min or Math.Max takes only two argument, but can be expanded to work for more than that, indefinitely. Math.MinMath.Max只接受两个参数,但可以无限期扩展为更多。

Here's an example with 3 variables.这是一个包含 3 个变量的示例。 int var1, var2, var3;

int max = Math.max(var1, Math.max(var2, var3));
int min = Math.min(var2, Math.max(var2, var3));
int middle = var1 + var2 + var3 - max - min

I can do this with even more than 3, but it gets a little messy.我什至可以用超过 3 个来做到这一点,但它会变得有点混乱。 Here's a helpful visual on imgur.这是一个关于imgur的有用的视觉效果。 Direct Album直接专辑

I'll shorten Math.Max to max() and Math.min() to min, from here.从这里开始,我将 Math.Max 缩短为 max() 并将 Math.min() 缩短为 min。


// a, b, c, d = int

max1 = max(a, b)
max2 = max(c, d)
min1 = min(a, b)
min2 = min(c, d)

max = max(max1, max2)
mid_high = max(min1, min2)
mid_low = min(max1, max2)
min = min(min1, min2)

This works with 3 arguments too.这也适用于 3 个参数。

max1 = max(a, b)
max2 = max(b, c) // or a, c : doesn't matter, as long as you have all three argument
min1 = min(a, b)
min2 = min(b, c) // or a, c : doesn't matter, as long as you have all three argument
mid = min(max1, max2)
mid = max(min1, min2)

I hope you learned a new technique today我希望你今天学到了一项新技术

int n1, n2, n3, n4, max = 0;

// get values of from input or assign it to n1, n2, n3 and n4

if (n1 > max)
max = n1;
if (n2 > max)
max = n2;
if (n3 > max)
max = n3;
if (n4 > max)
max = n4;

System.out.println(max);
        /*
    Problem Statement:- Enter many positive numbers & find Maximum & Minimum number entered & also total numbers entered.
    */
             import java.util.*;
             public class MathOperations {
                public static void main(String args[]) {
                    //Variables
                    double flag=0,count=0,max=0,min=0;
                    
                    //Creating Scanner class Object
                    Scanner scan = new Scanner(System.in);
                    
                    while (true) {
                    //Taking input from user
                    System.out.println("Enter a double number (negative to quit) : ");
                    double number = scan.nextDouble();
                        if (number > 0) {
                            count++;
                            max = Math.max(max,number);
                            if (flag == 0) {
                                min = number;
                                flag++;
                            } else {
                                min = Math.min(min,number);
                            }
                        } else {
                            break;
                        }
                    }
                    System.out.println("Total Numbers entered: "+count);
                    System.out.println("Maximum: "+max);
                    System.out.println("Minimum: "+min);        
                }
             }

it seems to me what the problem means is that you keep track the min and max value on each input without using array,在我看来问题的意思是你在不使用数组的情况下跟踪每个输入的最小值和最大值,

 Scanner sc = new Scanner(System.in);
 int min = -1, max = -1;
 int input;
 for(int i=0; i<5; i++) {
     input = sc.nextInt();
     if(input < min) min = input;
     if(input > max) max = input;
 }
 // here you have min and max number from the inputs and can perform the curve function

You can use following code snippet:您可以使用以下代码片段:

int min = Integer.MAX_VALUE; //As per given requirement 'min' can be set to any value >=100
int max = Integer.MIN_VALUE; //As per given requirement 'max' can be set to any value <=0

try(Scanner input = new Scanner(System.in))
{
     int inValue;
     for(int i=0; i<5; i++) 
     {
         inValue = input.nextInt();
         if(inValue < min) min = inValue;
         if(inValue > max) max = inValue;
     }
}
System.out.println("Minimum value is : " + min);
System.out.println("Maximum value is : " + max);

See it working here .看到它在这里工作。

You could try this:你可以试试这个:

int s1, s2, s3, s4, s5;
s1 = 75;
s2 = 32;
s3 = 42;
s4 = 99;
s5 = 100;

int min = s1;
min = (min < s2) ? min : s2;
min = (min < s3) ? min : s3;
min = (min < s4) ? min : s4;
min = (min < s5) ? min : s5;
System.out.println(min);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM