Question:
How to find the smallest of three ints?
Answer:
Use the method Math.min()
Code:
import java.util.Scanner; public class SmallestInt { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter 1st int: "); int i1 = sc.nextInt(); System.out.print("Enter 2nd int: "); int i2 = sc.nextInt(); System.out.print("Enter 3rd int: "); int i3 = sc.nextInt(); int small = Math.min(i1,Math.min(i2,i3)); System.out.println("Smallest: " + small); } }
Output:
$ java SmallestInt Enter 1st int: 100 Enter 2nd int: 55 Enter 3rd int: 300 Smallest: 55