本文介绍了如何用ACM库(ConsoleProgram)编写我的Java程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的代码示例:

  public static void main(String [] args){
int q;
int p;
int最大;
int最小;

扫描仪输入=新的扫描仪(System.in);
System.out.println(输入数字列表:);
String input2 = input.nextLine();

String [] numbers = input2.split();

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

为(q = 0; q System.out.print();
System.out.println(result [q]);
}

System.out.println(最大数字:+最大数字(结果));
System.out.println(Smallest Number:+ SmallestNumber(result));
thelargest = LargestNumber(result);
thesmallest = SmallestNumber(result);
System.out.println(算术平均值
+ AirthmeticMean(结果,最小,最大));

}

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

thesmallest = series [i] ;
}
}
返回最小;
}

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

thelargest = series [i] ;
}
}
返回最大;
}

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

}

sum - =最小;
sum - =最大;

return(float)sum / result.length;
}

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



我开始:

 code> public class ArithmeticMean extends ConsoleProgram {

}

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

解决方案

在acm库中没有主要方法,但需要使用以下构造:

  public void run(){} 

以下是该图书馆的API
选择acm.program package ConsoleProgram类并找到适当的方法
另请参见acm.io / class IOConsole



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



其余的与你的代码一样。



好的,这里你是你的代码在acm:(有点丑,但工作正常) / p>

  import acm.program.ConsoleProgram; 

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

public void run()
{
int q;
int p;
int最大;
int最小;
String input2 =;
String [] numbers = null;

println(输入号码列表);
while(true){
String input = readLine();
if(input.equals())
break;
input2 + = input +;
}
numbers = input2.split();

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

}


println(result [q]);
}

println(最大的数字+最大的数字(结果));
println(Smallest Number:+ SmallestNumber(result));
thelargest = LargestNumber(result);
thesmallest = SmallestNumber(result);
println(算术平均值
+ AirthmeticMean(结果,最小,最大));

}

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

thesmallest = series [i] ;
}
}
返回最小;
}

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

thelargest = series [i] ;
}
}
返回最大;
}

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

}

sum - =最小;
sum - =最大;

return(float)sum / result.length;
}

}

并以JavaApplet运行


I want ask a little question about my program.

This is my code sample:

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

How can I convert this code sample to the ConsoleProgram (which is in the ACM library)?Which parts must I change or add?

I started with:

public class ArithmeticMean extends ConsoleProgram {

}

But I do not know what to do next.

解决方案

In acm library no main method though you need to use instead the following construction:

public void run() {}

Here is an API of this library http://jtf.acm.org/javadoc/student/Select acm.program package ConsoleProgram class and find appropriate methodssee also acm.io / class IOConsole

e.g. System.out.println() --> println() Scanner (String input) --> readLine(String prompt) etc.

the rest is the same as you in your code.

Ok, here you are your code in acm: (a bit ugly but works fine:)

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

}

And Run as JavaApplet

这篇关于如何用ACM库(ConsoleProgram)编写我的Java程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 13:05