I did so

public static void main(String[] args) { int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); int d = in.nextInt(); int x =0; if (a<b){ if (a<c){ if (a<d)x =a; } else if (b<a){ if (b<c) if (b<d)x =b; } else if (c<a){ if (c<b) if (c<d)x =c; } else if (d<b){ if (d<c) if (d<a)x =d; } System.out.println(x); } } } 

when entering numbers 5,7,3,9 gives 5, and should 3. Tell me what the error is. 5 7 3 9 5

  • one
    and where do you at least c or d assign it? .. In short, if - else 4 times more should be - pavel
  • one
    In the array, they are placed and sorted - the most elegant solution in my opinion - ilyaplot

5 answers 5

I look at the answers and am amazed. Here is the easiest solution:

 public static void main(String[] args) { int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); int d = in.nextInt(); int answer = Math.min( Math.min(a,b), Math.min(c,d) ); System.out.println(answer); } 

    I would do this:

     public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int count = scanner.nextInt(); List<Integer> numbers = new ArrayList<>(count); for (int i = 0; i < count; i++) numbers.add(scanner.nextInt()); Collections.sort(numbers); if (count != 0) System.out.println(numbers.get(0)); } 

    The first time you need to enter the number of characters, then the numbers themselves are entered. As a result, the minimum of them is output to the console.

      It is easier and more correct to do this using collections. For example - ArrayList.

       int tmp; ArrayList<Integer> inputList = new ArrayList<>(); for(int I = 0; I < 4; I++){ inputList.add(in.nextInt()); } //сортировка пузырек //можно еще вместо ручной использовать метод класса Collections - sort //Collections.sort(inputList); for(int I = 0; I < inputList.size()-1; I++){ for(int J = 0; J < inputList.size()-I-1; J++){ if(inputList.get(j) > inputList.get(j+1)){ tmp = inputList.get(j); inputList.add(j,inputList.get(j+1)); inputList.add(j+1,tmp); } } } for(Integer item : inputList){ System.out.print(' ' + item); } 

      If a teacher makes you manually compare 4 or more numbers like this, then it’s most likely that something is damn wrong with him ...

         public static void main(String[] args) { int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); int d = in.nextInt(); int x =0; if (a<b &a<c&a<d)x =a; else if (b<a&b<c&b<d)x =b; else if (c<a&c<b&c<d)x =c; else if (d<b&d<c&d<a)x =d; System.out.println(x); } } 
        • only instead of & you can (or often even need to) use && - while if any of the conditions are not met, the rest will not be checked - Alex Chermenin
         public static void main(String[] args) { int a = 0; int b = 0; int c = 0; int d = 0; int result = 0; Scanner in = new Scanner(System.in); System.out.print("Введите a: "); a = in.nextInt(); System.out.print("Введите b: "); b = in.nextInt(); System.out.print("Введите c: "); c = in.nextInt(); System.out.print("Введите d: "); d = in.nextInt(); if ((a < b) && (a < c) && (a < d)) { System.out.println("А - наименьшее"); } else if ((b < a) && (b < c) && (b < d)) { System.out.println("B - наименьшее"); } else if ((c < a) && (c < b) && (c < d)) { System.out.println("С - наименьшее"); } else if ((d < a) && (d < b) && (d < c)) { System.out.println("D наименьшее"); } } 
        • Ohoho Colleagues, do not quarrel. ) - Nick Volynkin
        • @ Michael welcome to Stack Overflow. The first time you used the tool to format the code in Js + html + css - it does not work with other languages. But with this sort of sorted out already. That Eugene hinted at a non-working code - most likely it was about a missing bracket } . - Nick Volynkin
        • In essence, the question: what do you think will happen if among the entered numbers there are at least two identical smallest ones? What will the program return? (cons for sure for this reason) - Nick Volynkin
        • You also print the number of the smallest number, and in the task the value. ) - Nick Volynkin