我想问一个关于readLine()
方法的问题。我的项目是读取list
中带有空格的数字,并计算算术平均余数,舍弃最小和最大数字。我尝试一些方法来阅读数字,但他们都是不正确的。如何在java中使用readLine()
方法读取数字?如何在java中使用readLine方法读取数字列表?
P.S:计划应与acm
Libary(控制台程序)工作
int [] inputArray ; ---> will this be helpful to me ?
感谢你的帮助。
import java.text.DecimalFormat;
import java.util.Scanner;
import acm.program.ConsoleProgram;
public class CCC extends ConsoleProgram {
public static void main(String[] args) {
/*
* For data input.
*/
Scanner kb = new Scanner(System.in);
/*
* Declare an array to store the judge's scores.
*/
double[] scores = new double[8];
/*
* Declare variables for lowest and highest scores.
*/
double lowestScore = Integer.MAX_VALUE;
double highestScore = Integer.MIN_VALUE;
/*
* Read 7 judge's scores.
*/
for (int i = 0; i < 7; i++) {
System.out.println(String.format("Judge Score #%d: ", i + 1));
scores[i] = kb.nextDouble();
/*
* Compare the current score with the lowest and the highest scores.
*/
if (scores[i] < lowestScore) {
lowestScore = scores[i];
}
if (scores[i] > highestScore) {
highestScore = scores[i];
}
}
/*
* Sum the scores, except the lowest and the highest scores.
*/
double total = 0.00;
for (int i = 0; i < 7; i++) {
if (scores[i] != lowestScore && scores[i] != highestScore) {
total = total + scores[i];
}
}
/*
* Display the output.
*/
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("Total points received: " + df.format(total));
System.out.println("Arithmetic Mean : " + df.format(total/7));
}
}
这是我的代码,我问stackedoverflow的人如何解决这个问题,但我没能解决它。在这个计划,我应该得到的号码列表来自用户。例如;用户写一个列表“11 23 4 55 66 7”(数字之间有空格)。我的程序应该丢弃最大和最小的数字来计算算术平均值。我用扫描器方法成功完成它,如你所见,但是老师希望我用ACM libary(consoleprogram)编写这个程序。这个程序像Java Application一样成功运行,但是当我想在Java Applet中运行它时,我无法做到这一点。
感谢您的帮助和理解。
有没有人能帮助我?
向我们展示您的方法和您遇到的困难。 – 2013-05-04 11:34:24
@AchintyaJha我添加了一些关于我的程序的信息。 – mertha 2013-05-04 13:07:31
@jlordo我添加了一些关于我的程序的信息,它解释了我昨天的问题。 – mertha 2013-05-04 13:08:19