简体   繁体   中英

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:

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. 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. 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. 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.Min or Math.Max takes only two argument, but can be expanded to work for more than that, indefinitely.

Here's an example with 3 variables. 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. Here's a helpful visual on imgur. Direct Album

I'll shorten Math.Max to max() and Math.min() to min, from here.


// 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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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