[英]How to use Java to write a program that read a sequence of integer inputs?
What is the simplest way to write a program using Java that will read the inputs and print out the smallest and largest number.使用 Java 编写程序的最简单方法是什么,该程序将读取输入并打印出最小和最大数字。 What about the number of even and odd inputs?
偶数和奇数输入的数量如何?
For loop is required, and it needs to allow user to quit the loop. For 循环是必需的,它需要允许用户退出循环。 Here is what I got so far (it may be wrong):
这是我到目前为止得到的(可能是错误的):
import java.util.Scanner;
public class E62a
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter numbers");
int number = in.nextInt();
int number2 = in.nextInt();
for(int counter = counter % 2; counter <= 6; counter++)
{
System.out.println(number);
System.out.println(number2);
}
}
} }
What is the simplest way (...)
最简单的方法是什么(...)
In programming, there is hardly ever a "simplest way" to do anything that requires more than a couple of commands.在编程中,几乎没有一种“最简单的方法”可以完成需要多于几个命令的任何事情。 What "simple" means depends on circumstances.
“简单”的含义取决于具体情况。
How to write a Java program that will read (console) inputs?
如何编写将读取(控制台)输入的 Java 程序?
You have already found a way:你已经找到了一种方法:
Scanner in = new Scanner(System.in);
//...
in.nextLine(); //or nextInt(), or whatever else...
There is also...还有...
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//...
String input = br.readLine();
...which is what I usually use. ...这是我通常使用的。 And there are probably other methods...
可能还有其他方法......
How to find/print out the smallest and largest number (between two numbers)?
如何找到/打印出最小和最大数字(两个数字之间)?
A simple IF-THEN-ELSE
structure will do.一个简单的
IF-THEN-ELSE
结构就可以了。 In Java this would be:在 Java 中,这将是:
if (numA <= numB) { min = numA; max = numB; } else {min = numB; min = numA; }
You can also use Java's (very clean and convenient) built-in methods:您还可以使用 Java 的(非常干净和方便)内置方法:
min = Math.min(numA,numB); max = Math.max(numA,numB);
You already know how to print... System.out.print()
or .println()
.您已经知道如何打印...
System.out.print()
或.println()
。
What about the number of even and odd inputs?
偶数和奇数输入的数量如何?
I am not sure about what you mean, but if you mean multiple inputs (more than two), you will first have to decide on how the user will enter the inputs;我不确定你的意思,但如果你的意思是多个输入(超过两个),你首先必须决定用户将如何输入; like in a single line, separated by spaces or commas or other kind of
separator character
, or as multiple lines, in which case you will need a trigger string
to tell the program that the inputs have ended and that processing should continue/follow.就像在单行中,由空格或逗号或其他类型的
separator character
,或作为多行,在这种情况下,您将需要一个trigger string
来告诉程序输入已结束,处理应继续/跟随。
In case of single-line + separators, your program will be clean and highly maintainable, because processing of input is almost completely decoupled from the interface (console), but you will need to parse the resulting String to separate inputs from each-other.在单行 + 分隔符的情况下,您的程序将是干净且高度可维护的,因为输入的处理几乎与界面(控制台)完全分离,但您需要解析生成的 String 以将输入彼此分开。
In case of multi-line + trigger, you will be able to populate an array with individual inputs as they are typed, but the console gets a lot of clutter, and is considerably more coupled to the processing.在多行 + 触发器的情况下,您将能够在键入时使用单个输入填充数组,但控制台会变得很混乱,并且与处理的耦合要大得多。
Last but not least, the best way to learn a programming language is by:最后但并非最不重要的是,学习编程语言的最佳方法是:
Cheers, and good luck!干杯,祝你好运!
What are you trying to do with int counter = counter % 2
?你想用
int counter = counter % 2
什么? This essentially tells the program, "Create an integer that is equal to the remainder of its current value divided by 2."这实质上是告诉程序,“创建一个整数,它等于当前值除以 2 的余数”。 That is not possible.
这是不可能的。
counter = counter % 2
can only work if counter
has previously been initialized. counter = counter % 2
只有在counter
之前已经初始化的情况下才能工作。
Your for
loop does not provide the user with a way to manually exit.您的
for
循环没有为用户提供手动退出的方法。 To allow the user to manually exit the loop, you'll need to write it to accept user input within the loop and provide an exit condition.要允许用户手动退出循环,您需要编写它以接受循环内的用户输入并提供退出条件。 Here's one example of how you could do that:
以下是您如何做到这一点的一个示例:
Scanner in = new Scanner(System.in);
Integer input;
ArrayList<Integer> numbers = new ArrayList<>();
for (int i=0; i<=6; i++) {
System.out.print("Enter a number or enter -1 to exit: ");
input = in.nextInt();
if (input != -1) {
numbers.add(input);
} else {
break;
}
}
This will allow the user to input up to six numbers and have the option to exit.这将允许用户输入最多六个数字并可以选择退出。 Since
numbers
is an ArrayList
in this example, items can be retrieved from it using the get()
function.由于在此示例中
numbers
是一个ArrayList
,因此可以使用get()
函数get()
检索项目。 For instance, System.out.println(numbers.get(0))
would display the first number added to numbers
.例如,
System.out.println(numbers.get(0))
将显示添加到numbers
的第一个numbers
。
Determining the smallest and largest numbers could be done like this:确定最小和最大数字可以这样完成:
int largest = -1;
int smallest = -1;
for (int i=0; i<numbers.size(); i++) {
if (numbers.get(i) > largest) {
largest = numbers.get(i);
}
if ((numbers.get(i) < smallest) || (smallest == -1)) {
smallest = numbers.get(i);
}
}
This section of code will count the number of even and odd inputs:这部分代码将计算偶数和奇数输入的数量:
int evenNumbers = 0;
int oddNumbers = 0;
for (int i=0; i<numbers.size(); i++) {
if ((numbers.get(i) % 2) == 0) {
evenNumbers++;
} else {
oddNumbers++;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.