简体   繁体   中英

Find Smallest of Two numbers using Java -

So I have an assignment where I need to find the smallest of two numbers entered using java. Here are the instructions:

Input a set of positive integers, ending with -1 as a sentinel. Print the smallest and second smallest of these numbers in that order. These two numbers may be equal to each other – see the second example below.

You should check that there are at least two numbers input before the -1, so that there always will be a smallest and second smallest number. (Hint: you may want to input the first two numbers separately, before you start a loop that inputs the rest of the numbers.) If the first or second number is -1, you should call IO.reportBadInput and halt the program.

Examples:

java TwoSmall
32
11
19
7
-1
RESULT: 7
RESULT: 11

I have done the assignment but I was wondering if there is a simpler way of doing this:

Here is my code:

public class TwoSmall
{
public static void main(String[]args){
int smallest,secondSmallest,temp = 0;


System.out.println("Please enter a number");
smallest = IO.readInt();
System.out.println("Please enter a number");
secondSmallest = IO.readInt();

    // Used to sort intial two numbers
    if (smallest>secondSmallest){

        temp = smallest;
        smallest = secondSmallest;
        secondSmallest = temp;

    }

    if (smallest !=-1 && secondSmallest !=-1){
        while (temp != -1){
            System.out.println("Please enter a number");
            temp = IO.readInt();
                if (temp<smallest && temp != -1){
                    secondSmallest = smallest;
                    smallest = temp;
                }else if (temp<secondSmallest && temp != -1){
                    secondSmallest = temp;
                }

        }
        IO.outputIntAnswer(smallest);
        IO.outputIntAnswer(secondSmallest);
    }else {
        IO.reportBadInput();
    }       
}
}

I feel like I have written too much code for a simple problem.

As you have stated in the question this is a homework assignment so people will be reluctant to give you the answer. Your question is a little confusing whether you want to find the smallest 2 numbers in a set or if you want to compare two numbers and return the smallest one. Here is a high level approach to each

Find 2 smallest numbers in set

  • Put all numbers in array.
  • Sort array.
  • return array[0] and array[1]

Compare two numbers and return smallest

  • Declare function that takes in two arguments a and b
  • if (a<b) return a;
  • if (b<a) return b;
  • else they are the same!

for whatever is worth, this gives you the smaller of any amount of numbers while ignoring nulls (just make sure your list is made of the same type of numbers, Integer, Long, Float, etc)

smaller = Arrays.asList(3,6).stream().filter(Objects::nonNull).sorted().findFirst().orElse(null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM