-2
import java.util.Scanner;

public class GirilenSayilardanMinveMaxDeğerleriBulma {

    public static void main(String[] args) {
        
        Scanner input=new Scanner(System.in);   
        
        System.out.print("kaç sayı gireceksiniz:  ");
        int a=input.nextInt();
        int max=0,min=0,b=0;
        
        for(int i=1;i<=a;i++) {
            System.out.print( i +".sayıyı giriniz:  ");
            b=input.nextInt();
            max=b;
            min=b;
        }if(b >max) {
            max=b;
        }else if(b<min) {
            min=b;
        }else {
            System.out.print( "sayılar eşit.");
        }
        System.out.print(max);
        System.out.print(min);
    }
}

I want to Find the Largest (MAX) and Smallest (MIN) Number Typed by the User in Java, but I'm making a mistake somewhere. i would be glad if you would support.by the way, I just want to use loop.

I took a number from the user and asked how many times to enter it. after entering the numbers, there is a calculation error and he gets the last number. max and min throw the last number to the value.thank you

harry-potter
  • 1,981
  • 5
  • 29
  • 55

2 Answers2

2
b=input.nextInt();
max=b;
min=b;

Look here, you're assigning b to the next int, then setting max and min to whatever b was.

Change to

b=input.nextInt();
if(b > max)
        max=b;
if(b < min)
        min=b;

And remove the ifs after the loop. Also, think about default parameters for Max and Min

M. Rogers
  • 367
  • 4
  • 18
  • What is the default parameter? I did not fully understand. i am new to this topic – mert sezgin Nov 07 '22 at 21:49
  • int max=0,min=0,b=0; is where your setting default max and mins. So think about it, "min" variable starts equaling 0, so if your number set contains no numbers less than 0, you won't properly track the minimum. You should start your min with a very high number, or better yet "Integer.MAX_VALUE", to ensure no matter what, your first comparison will store the first value as a minimum. Use the same logic for the maximum – M. Rogers Nov 08 '22 at 13:53
  • i still couldn't run the program. – mert sezgin Nov 15 '22 at 05:11
0
 public static void main(String[] args) {
    
    Scanner input=new Scanner(System.in);   
    
    System.out.print("kaç sayı gireceksiniz:  ");
    int a=input.nextInt();
    int max=0,min=0,b=0;
    
    for(int i=1;i<=a;i++) {
        System.out.print( i +".sayıyı giriniz:  ");
        b=input.nextInt();
        max=b;
        min=b;
    }if(b >max) {
        max=b;
    }else if(b<min) {
        min=b;
    }else {
        System.out.print( "sayılar eşit.");
    }
    System.out.print(max);
    System.out.print(min);
}
  1. The if else condition is outside the for loop. Move it inside the loop.
  2. The if else condition logic is wrong. It should be two if conditions. E.g.: Let's say a=3, numbers = {10, 11, 12}. The input will always satisfy b > max and never go in b < min. output {max:12, min: 0}
  3. The starting default number 0 is wrong. E.g.: a=3, numbers = {-1, -2, -6}, output: {max: 0, min: -6}

Remove if else condition and start with a proper default number.

Check below

public static void main(String[] args) {

        Scanner input=new Scanner(System.in);

        System.out.print("kaç sayı gireceksiniz:  ");
        int a=input.nextInt();

        System.out.print( "1.sayıyı giriniz:  ");
        int b = input.nextInt();
        //this is the 1st input, start with it as both max and min
        int max=b, min=b;

        //since we already took 1 input we run loop for a-1 times
        for(int i=2; i <= a; i++) {
            System.out.print( i +".sayıyı giriniz:  ");
            b = input.nextInt();

            if(b > max) {
                max = b;
            }
            if(b < min) {
                min = b;
            }
        }
        System.out.println("max:"+ max);
        System.out.println("min:"+ min);
    }
sanurah
  • 976
  • 1
  • 6
  • 16
  • why we said 'Integer.MIN_VALUE' and 'Integer MAX_VALUE'. values ​​are zero or one ? – mert sezgin Nov 07 '22 at 21:52
  • I used min and max int values initially so the input will be between those. But you can also start with the first input as default value like M.Rogers has mentioned in his answer. Check updated code above. Run in debug mode and see how it works. – sanurah Nov 07 '22 at 22:24
  • `enter how many numbers: 3 1.enter number: 10 2.enter number: 5 max:10 min:0` this is how the screen output works. After a long struggle, my situation is still the same. – mert sezgin Nov 08 '22 at 05:17
  • @mertsezgin I have updated the loop condition.check now – sanurah Nov 08 '22 at 07:33
  • Thank you for your comments, but I still can't write the minimum value – mert sezgin Nov 08 '22 at 18:53
  • @mertsezgin sorry, I don't understand. you copied the code from my answer and it doesn't work? take the code from end of my answer and run it. – sanurah Nov 09 '22 at 21:07
  • Sorry, I'm making a mistake. The program doesn't work. – mert sezgin Nov 15 '22 at 05:08