2

我想问一个关于我的程序的小问题。

这是我的代码示例:

public static void main(String[] args) {
    int q;
    int p;
    int thelargest;
    int thesmallest;

    Scanner input = new Scanner(System.in);
    System.out.println("Enter the list of number : ");
    String input2 = input.nextLine();

    String[] numbers = input2.split(" ");

    int[] result = new int[numbers.length];
    for (p = 0; p < numbers.length; p++) {
        result[p] = Integer.parseInt(numbers[p]);

    }

    for (q = 0; q < result.length; q++) {
        System.out.print("");
        System.out.println(result[q]);
    }

    System.out.println("Largest Number :  " + LargestNumber(result));
    System.out.println(" Smallest Number :  " + SmallestNumber(result));
    thelargest = LargestNumber(result);
    thesmallest = SmallestNumber(result);
    System.out.println("The Arithmetic Mean : "
            + AirthmeticMean(result, thesmallest, thelargest));

}

public static int SmallestNumber(int[] series) {
    int thesmallest = series[0];
    for (int i = 1; i < series.length; i++) {
        if (series[i] < thesmallest) {

            thesmallest = series[i];
        }
    }
    return thesmallest;
}

public static int LargestNumber(int[] series) {
    int thelargest = series[0];
    for (int i = 1; i < series.length; i++) {
        if (series[i] > thelargest) {

            thelargest = series[i];
        }
    }
    return thelargest;
}

public static float AirthmeticMean(int[] result, int thesmallest,
        int thelargest) {
    int sum = 0;
    for (int i = 0; i < result.length; i++) {
        sum += result[i];
    }

    sum -= thesmallest;
    sum -= thelargest;

    return (float) sum / result.length;
}

如何将此代码示例转换为 ConsoleProgram(位于 ACM 库中)?我必须更改或添加哪些部分?

我开始:

public class ArithmeticMean extends ConsoleProgram {

}

但我不知道下一步该做什么。

4

1 回答 1

2

在 acm 库中没有 main 方法,尽管您需要使用以下构造:

public void run() {}

这是这个库的 API http://jtf.acm.org/javadoc/student/ 选择 acm.program 包 ConsoleProgram 类并找到合适的方法参见 acm.io / class IOConsole

例如 System.out.println() --> println() Scanner (String input) --> readLine(String prompt) 等。

其余的与您的代码相同。

好的,这是您在 acm 中的代码:(有点难看,但工作正常:)

import acm.program.ConsoleProgram;

public class StackOverflow extends ConsoleProgram
{
    private static final long serialVersionUID = 1L;

    public void run()
    {
        int q;
        int p;
        int thelargest;
        int thesmallest;
        String input2 = "";
        String[] numbers = null;

        println("Enter the list of number : ");
        while (true) {
            String input = readLine();
            if (input.equals(""))
                break;
            input2 += input + " ";
        }
        numbers = input2.split(" ");

        int[] result = new int[numbers.length];
        for (p = 0; p < numbers.length; p++) {
            result[p] = Integer.parseInt(numbers[p]);

        }

        for (q = 0; q < result.length; q++) {
            print("");
            println(result[q]);
        }

        println("Largest Number :  " + LargestNumber(result));
        println(" Smallest Number :  " + SmallestNumber(result));
        thelargest = LargestNumber(result);
        thesmallest = SmallestNumber(result);
        println("The Arithmetic Mean : "
                + AirthmeticMean(result, thesmallest, thelargest));

    }

    public static int SmallestNumber(int[] series)
    {
        int thesmallest = series[0];
        for (int i = 1; i < series.length; i++) {
            if (series[i] < thesmallest) {

                thesmallest = series[i];
            }
        }
        return thesmallest;
    }

    public static int LargestNumber(int[] series)
    {
        int thelargest = series[0];
        for (int i = 1; i < series.length; i++) {
            if (series[i] > thelargest) {

                thelargest = series[i];
            }
        }
        return thelargest;
    }

    public static float AirthmeticMean(int[] result, int thesmallest,
            int thelargest)
    {
        int sum = 0;
        for (int i = 0; i < result.length; i++) {
            sum += result[i];
        }

        sum -= thesmallest;
        sum -= thelargest;

        return (float) sum / result.length;
    }

}

并作为 JavaApplet 运行

于 2013-05-05T19:52:47.013 回答