Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /**
- *
- * @author jordanpeck
- */
- public class SMALLESTfour {
- public static void main(String[] args)
- {
- Scanner kybd = new Scanner(System.in);
- System.out.println("Enter 4 Integers: ");
- int num1 = kybd.nextInt(); //user enter first number
- int num2 = kybd.nextInt(); //user enter second number
- int firstsmaller = num1; //the new 'firstsmaller' value is now num1
- int secondsmaller = num2; //the new value of second smaller is now num2
- int getsmall1 = (smaller(firstsmaller,secondsmaller));//run numbers through method smaller() returning smallest value
- int num3 = kybd.nextInt(); //user enter number 3
- int num4 = kybd.nextInt(); //user enter number 4
- firstsmaller = num3; //the new 'firstsmaller' value is now num3
- secondsmaller = num4; //the new value of second smaller is now num4
- int getsmall2 = (smaller(firstsmaller,secondsmaller)); //get smallest from num3 and num4 returned through smaller() method
- firstsmaller = getsmall1; //firstsmaller is now the smallest from the first section (num1 num2)
- secondsmaller = getsmall2; //secondsmaller is now smallest from the second section (num3 num4) run through method again
- System.out.println("Smallest Number is: " + smaller(firstsmaller, secondsmaller)); //output final values of first/secondsmaller
- }
- public static int smaller (int firstsmaller, int secondsmaller) //gets the smallest of firstsmaller and second smaller
- //everytime they're passed to it
- {
- int smallest = 0; //set smallest to 0
- if
- (firstsmaller < secondsmaller) //if firstsmaller is less than secondsmaller, return firstsmaller as the smallest value
- smallest = firstsmaller; //if secondsmaller is less than firstsmaller, return secondsmaller as the smallest value
- else
- smallest = secondsmaller; //else vice versa
- return smallest; //return this value, which has become the value of either firstsmaller or secondsmaller, whichever was smallest
- }
- }
Add Comment
Please, Sign In to add comment