我想问一个关于我的程序的问题。
这是我的代码示例:
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 {
}
但是我不知道下一步该怎么做。
最佳答案
在acm库中,没有主要方法,尽管您需要使用以下构造:
public void run() {}
这是此库http://jtf.acm.org/javadoc/student/的API
选择acm.program程序包ConsoleProgram类并找到适当的方法
另请参见acm.io/ IOConsole类
例如System.out.println()-> println()
扫描器(字符串输入)-> readLine(字符串提示)等
其余部分与您的代码相同。
好的,这是您在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的身份运行
关于java - 如何使用ACM库(ConsoleProgram)编写Java程序?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16387867/