英文:
Input and store an arbitrarily long sequence of numbers
问题
这基本上是我要做的事情:
要求用户输入尽可能多的整数,告诉他们在输入数字完成后键入“done”。
将这些数字存储在数据结构中。(仔细考虑使用哪种数据结构。)
编写一个计算数据结构中所有元素之和的方法。
编写一个查找数据结构中最小正数的方法(如果没有正数,则返回9999)。
使用用户的数字在数据结构上调用这些方法。
我已经编写了一些代码,但是没有进展。
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
if (in.hasNextInt())
System.out.println(in.nextInt());
else
in.next();
}
}
}
英文:
So, this is basically what I have to do:
Ask the user to enter as many ints as they want, and tell them to type “done” when they are done entering the numbers.
Store these numbers in a data structure. (Think carefully about which one to use.)
Write a method that computes the sum of all the elements in the data structure.
Write a method that finds the smallest positive number in the data structure (return 9999 if there aren’t any positive numbers).
Call these methods on the data structure with the user’s numbers.
I have already written some of the code, but it's taking me no where.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
if (in.hasNextInt())
System.out.println(in.nextInt());
else
in.next();
}
}
}
答案1
得分: 1
这是一个会引导你朝正确方向前进的更改:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
if (in.hasNextInt()) {
addNumber(in.nextInt());
} else {
final String string = in.next();
if ("done".equals(string.toLowerCase())) {
compute();
} else {
System.err.println("无效输入 " + string + "。中止...");
}
return;
}
}
}
private static void compute() {
// 在这里进行相应的计算
}
private static void addNumber(int number) {
// 将这部分代码更改为将数字存储起来的代码
System.out.println(number);
}
编辑:一些变更的见解。你最初的基本结构是可以的。我的意思是,你理解得很对。我添加了 addNumber()
方法,每次在输入中得到一个数字时将会调用它。现在,在这个方法内部,你可以编写代码来将这个值存储在一个集合或列表中。
然后,如果输入不是一个整数,我们会将它作为字符串获取,并将其与 "done" 进行比较。如果相等,我们调用 compute()
方法。在这个方法内部,你应该编写代码来执行被要求做的计算。使用集合/列表中的值进行计算。
如果字符串不是 "done",我们视为一个错误。
英文:
Here is a change that will lead you into the right direction:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
if (in.hasNextInt()) {
addNumber(in.nextInt());
} else {
final String string = in.next();
if ("done".equals(string.toLowerCase())) {
compute();
} else {
System.err.println("Invalid input " + string + ". Aborting...");
}
return;
}
}
}
private static void compute() {
// Do your magic here
}
private static void addNumber(int number) {
// Change this to code that stores the number
System.out.println(number);
}
EDIT: Some insight in what has changed. The very basic structure you had was alright. I mean, you got the right idea. I added the addNumber() method that will be called every time you get a number in the input. Now, inside that method you can write code for storing the value in a set or list maybe.
Then, if the input doesn't get a integer we just get whatever it is as a string a compare to "done". If it's equal we call compute() method. Inside that method you should write code that computed whatever calculation you are requested to do. Using the values in the set/list.
If the string isn't "done" we consider that an error.
答案2
得分: 1
使用Java8
的stream
,我们可以比使用等价的for
循环更高效地编写这段代码,并且只需更少的代码行数。
import java.util.*;
class Main {
// 返回包含所有输入整数的数组。
public static int[] getInts() {
List<Integer> list = new ArrayList<Integer>();
System.out.println("输入整数,输入 done 退出并按回车:");
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
if (in.hasNextInt())
list.add(in.nextInt());
else if (in.next().equals("done"))
break;
}
return list.stream().mapToInt(i -> i).toArray();
}
// 返回整数的和,如果数组为空,则返回 0。
public static int sum(int[] arr) {
return Arrays.stream(arr).sum();
}
// 如果数组为空或仅包含负整数,则返回 9999。
public static int min(int[] arr) {
return Arrays.stream(arr).filter(i -> i > 0).min().orElse(9999);
}
public static void main(String[] args) {
int[] arr = getInts();
System.out.println(Arrays.toString(arr));
System.out.println(sum(arr));
System.out.println(min(arr));
}
}
输出:
输入整数,输入 done 退出并按回车:
5 3 1 done // 我们的输入
[5, 3, 1]
9
1
英文:
By using the Java8
stream
we could write this more efficiently and in a fewer lines of code than using the equivalent for
loop.
import java.util.*;
class Main {
// Returns an array containing all inputted integers.
public static int[] getInts() {
List<Integer> list = new ArrayList<Integer>();
System.out.println("Enter integers or done to exit and press enter: ");
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
if (in.hasNextInt())
list.add(in.nextInt());
else if (in.next().equals("done"))
break;
}
return list.stream().mapToInt(i->i).toArray();
}
// Returns the sum of the integers, if the array is empty it returns 0.
public static int sum(int[] arr) {
return Arrays.stream(arr).sum();
}
// Returns 9999 if the array is empty or only has negative integers.
public static int min(int[] arr) {
return Arrays.stream(arr).filter(i -> i > 0).min().orElse(9999);
}
public static void main(String[] args) {
int[] arr = getInts();
System.out.println(Arrays.toString(arr));
System.out.println(sum(arr));
System.out.println(min(arr));
}
}
Output:
Enter integers or done to exit and press enter:
5 3 1 done // Our Input.
[5, 3, 1]
9
1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论