从java中的数组中检索两个最小的输入

时间:2015-03-09 03:05:30

标签: java arrays min

我正在对CS进行简介,其中一个作业根据用户输入询问2个最小的数字。我已经尝试了大约3天,只是无法弄清楚为什么我的代码不能正常工作,而且我工作的越多,我就越沮丧。     公共课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);
    }

我们将IO.readInt()用于用户输入,我用它来定义数组的大小。我在arrayScores [j] = IO.readInt()中再次使用它;加载数组。当用户首先输入较低的数字时,它会起作用,但是当首先输入较高的数字时则不行。我认为我在检索指定索引处的值时遇到问题。这可能是一团糟,但如果有人能帮助我,那肯定会受到赞赏。这是我们使用的IO模块,如果这有帮助的话。我将继续我的无休止的战斗,使这件事工作..

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.");
}
}

1 个答案:

答案 0 :(得分:1)

找到两个最小的数字:

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];
    }
}