Java 如何在一行上有多个输入,所有输入都被传递到一个带有可变参数的方法中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19171579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to have multiple inputs on one line,all being passed into a method with variable arguments
提问by Iccirrus
Alright,so I've got this assignment that requires me to have a method with a variable number of inputs as well as a string input. The inputs all have to be on one line in the scanner, and the method has to return the number of values entered,the average value,the max value,the min value,and the string that was entered.
好的,所以我得到了这个任务,它要求我有一个具有可变数量输入以及一个字符串输入的方法。输入都必须在扫描仪的一行上,并且该方法必须返回输入的值的数量、平均值、最大值、最小值和输入的字符串。
This is an example of that the terminal window should look like.
这是终端窗口的示例。
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
The Average score has to be rounded to 2 decimal places(which I think I can handle) The only problem for me is getting the variable number of inputs to be scanned on a single line,and how to have the program count the number of inputs if I'm not hitting enter between the inputs. This is what I have so far.but I have no idea where to go from here. I can get it to ask for sequential values,but they aren't all on the same line
平均分数必须四舍五入到小数点后 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;
}
回答by Mr KP
First of all with the arg[0], you will need to use substring to chop the string into a set of array variable. Then use array.len or count to do the rest. Hope this help.
首先,对于 arg[0],您需要使用 substring 将字符串切割成一组数组变量。然后使用 array.len 或 count 来完成剩下的工作。希望这有帮助。
回答by MadProgrammer
The answer depends on the input to the method. Are you receiving a String
and an array of int
's or a String
, String
. That is, is the input from the user already broken up for you?
答案取决于方法的输入。您是否收到 aString
和一组int
's 或 a String
, String
. 也就是说,用户的输入是否已经为您分解了?
For example...
例如...
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;
}
}
I did think about using String#split(" ")
, but then you could need to convert each element to an int
yourself, which might or might not be suitable (it's a little more messy)
我确实考虑过使用String#split(" ")
,但是您可能需要将每个元素转换为您int
自己的元素,这可能适合也可能不适合(这有点混乱)
回答by Michael Yaworski
Once you have the "variable number of inputs" inputted to a String
, use the .split()
command on the String
to split the String
into parts, and assign it to an array. Then calculate the avg, max, min, etc. values with the array.
将“可变数量的输入”输入到 a 后String
,使用 上的.split()
命令将String
其拆分为String
多个部分,并将其分配给一个数组。然后用数组计算平均值、最大值、最小值等值。
Example (tested and works perfectly):
示例(经过测试并完美运行):
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
Next, just display the values to the console: that's a simple Google search. Same with inputting the line of input.
接下来,只需将值显示到控制台:这是一个简单的 Google 搜索。与输入输入行相同。