简体   繁体   中英

Retrieving the two smallest inputs from an array in java

I'm taking an Intro to CS and one of the assignments asks for the 2 smallest numbers based off of user input. I've tried for about 3 days and just can't figure out why my code isn't working, and the more I work at it the more frustrated I get. public class TwoSmallest { public static void main(String[] args){

System.out.print("How many numbers will you be inputing? ");
int howManyNums = IO.readInt();

int[] arrayScores = new int[howManyNums];

for(int j = 0;j < howManyNums;j++){
    System.out.print("Inuput number "+(j+1)+": ");
    arrayScores[j]=IO.readInt();

int tinyNum1 = arrayScores[0];
int tinyNum2 = arrayScores[1];
    for(int m = 0;tinyNum1 < arrayScores[m];m++){
        //if (tinyNum1 < m) {
            tinyNum1 = arrayScores[m];

            }
    for (int n = 1;tinyNum2 < arrayScores[n];n++){
        //if (tinyNum2 < n) {
            tinyNum2 = arrayScores[n];
        }
        if (tinyNum2 < tinyNum1){
            int swapTinyNum1 = tinyNum1;
            tinyNum2 = swapTinyNum1;
            }
        System.out.println("Smallest number: "+tinyNum1);
        System.out.println("Followed by: "+tinyNum2);
    }

We use the IO.readInt() for user input which I use to define the size of the array. I use it again at arrayScores[j]=IO.readInt(); to load the array. It kind of works when the user inputs the lower numbers first, but not when the higher numbers are input first. I think I'm having problems with retrieving the value at the designated index. It's probably a mess, but if anyone can help me out, it would definitely be appreciated. And here is the IO module we use, if this helps. I'm going to continue my endless battle at making this thing work..

import java.io.*;

public class IO
{
private static BufferedReader kb =
    new BufferedReader(new InputStreamReader(System.in));

private static BufferedReader fio = null;

public static boolean openFile(String filename){

    try{
        fio = new BufferedReader(new FileReader(filename));
        return true;
    }catch (IOException e){ return false;}
}

public static String readLine(){
    if (fio == null)
        return null;

    try{
         return fio.readLine();
    }catch(IOException e){ return null;}
}

public static String readString()
{
    while (true) {
        try {
            return kb.readLine();
        } catch (IOException e) {
            // should never happen
        }
    }
}

public static int readInt()
{
    while (true) {
        try {
            String s = kb.readLine();
            return Integer.parseInt(s);
        } catch (NumberFormatException e) {
            System.out.print("That is not an integer.  Enter again: ");
        } catch (IOException e) {
            // should never happen
        }
    }
}

public static double readDouble()
{
    while (true) {
        try {
            String s = kb.readLine();
            return Double.parseDouble(s);
        } catch (NumberFormatException e) {
            System.out.print("That is not a number.  Enter again: ");
        } catch (IOException e) {
            // should never happen
        }
    }
}

public static char readChar()
{
    String s = null;

    try {
        s = kb.readLine();
    } catch (IOException e) {
        // should never happen
    }

    while (s.length() != 1) {
        System.out.print("That is not a single character.  Enter again: ");
        try {
            s = kb.readLine();
        } catch (IOException e) {
            // should never happen
        }
    }

    return s.charAt(0);
}

    public static boolean readBoolean()
    {
            String s = null;

            while (true) {
                    try {
                            s = kb.readLine();
                    } catch (IOException e) {
                            // should never happen
                    }

                    if (s.equalsIgnoreCase("yes") ||
            s.equalsIgnoreCase("y") ||
            s.equalsIgnoreCase("true") ||
            s.equalsIgnoreCase("t")) {
                            return true;
                    } else if (s.equalsIgnoreCase("no") ||
                   s.equalsIgnoreCase("n") ||
                   s.equalsIgnoreCase("false") ||
                   s.equalsIgnoreCase("f")) {
                            return false;
                    } else {
                            System.out.print("Enter \"yes\" or \"no\": ");
                    }
            }
    }

public static void outputStringAnswer(String s)
{
    if (s != null) {
        System.out.println("RESULT: \"" + s + "\"");
    } else {
        System.out.println("RESULT: null");
    }
}

public static void outputIntAnswer(int i)
{
    System.out.println("RESULT: " + i);
}

public static void outputDoubleAnswer(double d)
{
    System.out.println("RESULT: " + d);
}

public static void outputCharAnswer(char c)
{
    System.out.println("RESULT: '" + c + "'");
}

public static void outputBooleanAnswer(boolean b)
{
    System.out.println("RESULT: " + b);
}

public static void reportBadInput()
{
    System.out.println("User entered bad input.");
}
}

To find the two smallest number:

int[] numbers = {}; // whatever.
int min1 = Integer.MAX_VALUE, min2 = Integer.MAX_VALUE;
for (int i = 0; i < numbers.length; i++) {
    if (numbers[i] < min1) {
        min2 = min1;
        min1 = numbers[i];
    } else if (numbers[i] < min2) {
        min2 = numbers[i];
    }
}

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