好吧,所以我有这个赋值,要求我有一个带有可变数量输入的方法以及一个字符串输入。输入都必须在扫描仪的一行上,并且方法必须返回输入的值的数量,平均值,最大值,最小值和输入的字符串。
这是终端窗口看起来像的一个例子。
Please enter the name of the course: CourseNameHere
Please enter the scores for CSC 201 on a single line and type a -1 at the end
71 02 81 44 84 17 38 11 20 05 93 -1
The course name : CourseNameHere
Number of Scores : 11
The Average Score : 42.37
The Minimum Score : 02
The Maximum Score : 93
平均分数必须四舍五入到小数点后2位(我认为我可以处理) 对我来说唯一的问题是在一行上扫描可变数量的输入,以及如果我没有在输入之间输入,如何让程序计算输入的数量。 这是我到目前为止所做的。但我不知道从哪里开始。我可以让它询问顺序值,但它们不是全部在同一行
public static int calculate(int again)
{
Scanner input = new Scanner(System.in);
int numberOfValues = 0;
int max = -9999;
int min = 100000;
int sum = 0;
double value;
System.out.print("What is the name of the course you wish to evaluate? : ");
String courseName = input.next();
System.out.println("Please enter the scores for the class, press enter between each value." + "\n" + "Enter -1 to finish and calculate.");
for(value = 0; value != 1; numberOfValues++)
{
value = input.nextDouble();
sum += value;
}
答案 0 :(得分:1)
答案取决于方法的输入。您收到的是String
和一系列int
还是String
,String
。也就是说,用户的输入是否已经为您分解?
例如......
Scanner kb = new Scanner(System.in);
String courseName = kb.nextLine();
String scoreLine = kb.nextLine();
// courseName and scoreLine now become the parameters to you method...
// You could then use another scanner to break the line down...
Scanner scoreLineScanner = new Scanner(scoreLine);
while (scoreLineScanner.hasNextInt()) {
int score = scoreLineScanner.nextInt();
if (score != -1) {
// Calculate other values...
} else {
break;
}
}
我确实考虑过使用String#split(" ")
,但是你可能需要自己将每个元素转换为int
,这可能适合也可能不适合(它有点混乱)
答案 1 :(得分:0)
首先使用arg [0],您需要使用substring将字符串切割成一组数组变量。然后使用array.len或count来完成剩下的工作。希望这有帮助。
答案 2 :(得分:0)
输入String
的“可变输入数量”后,使用.split()
上的String
命令将String
拆分为多个部分,然后分配它到一个数组。然后用数组计算avg,max,min等值。
示例(测试并完美运行):
String input; // assign the String to be the input
// split the input by spaces and assign every part to an element in the array
String inputArray[] = input.split(" ");
// get the number of input numbers
double num = inputArray.length - 1; // don't include the last number, -1
double avg = 0; double max = 0; double min = Double.parseDouble(inputArray[0]);
// get the average and min/max numbers
for (int i = 0; i < inputArray.length - 1; i++) // i < inputArray.length - 1 so you don't count the last input, which is -1
{
avg += Double.parseDouble(inputArray[i]); // for now, set the avg to be the sum of all the numbers
// if this input is greater than the last greatest one, set the max to be the this input
if (Double.parseDouble(inputArray[i]) > max) {max = Double.parseDouble(inputArray[i]);}
// if this input is lesser than the last least one, set the min to be the this input
if (Double.parseDouble(inputArray[i]) < min) {min = Double.parseDouble(inputArray[i]);}
}
avg = avg / num; // complete the average calculation by diving by total number of inputs
avg = (double)Math.round(avg * 100) / 100; // round the average to two decimal places
接下来,只需将值显示到控制台:这是一个简单的Google搜索。与输入输入线相同。