Advertisement
grodek118

Index of smallest

May 26th, 2022
830
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3.  
  4. import java.util.ArrayList;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         ArrayList<Integer> numbers = new ArrayList<>();
  11.  
  12.         while (true) {
  13.             int input = Integer.valueOf(scanner.nextLine());
  14.  
  15.             if (input == 9999) {
  16.                 break;
  17.             }
  18.             numbers.add(input);
  19.         }
  20.  
  21.         int smallest = numbers.get(0);
  22.  
  23.         for(int i = 0; i < numbers.size(); i++) {
  24.             int number = numbers.get(i);
  25.             if (smallest > number) {
  26.                 smallest = number;
  27.             }
  28.         }
  29.         System.out.println("smallest number: " + smallest);
  30.        
  31.         for (int i = 0; i < numbers.size(); i++) {
  32.                 if(numbers.get(i) == smallest) {
  33.                     System.out.println("found at index " + i);
  34.                 }
  35.             }
  36.     }
  37. }
Advertisement
RAW Paste Data Copied
Advertisement