英文:
Why doesn't the scanner class recognize the other numbers?
问题
public class Hello {
public static void main(String[] args) {
int number, count = 0, sum = 0;
int largest = Integer.MIN_VALUE, largestEvenNumber = Integer.MIN_VALUE;
Scanner console = new Scanner(System.in);
while (console.hasNext()) {
number = console.nextInt(); // read an integer entered by a user
if (number > largest) { // Condition for computing the largest number
largest = number;
}
if (number < 0) { // Condition for computing the number of negative integers in the sequence
count = count + 1;
}
if (number % 2 == 0) { // Condition for computing the largest even integer in the sequence
if (largestEvenNumber < number) {
largestEvenNumber = number;
}
}
if (number % 3 == 0) { // Condition for computing the sum of numbers divisible by 3
sum += number;
}
}
System.out.println("The largest integer is " + largest);
System.out.println("The number of negative integers in the sequence is " + count);
System.out.println("The largest even integer in the sequence is " + largestEvenNumber);
System.out.printf("The sum of numbers divisible by 3 is %d", sum);
}
}
(Note: The corrected code snippet provided above ensures that the program reads multiple input numbers by using a loop with console.hasNext()
condition. This allows the code to achieve the expected output as described.)
英文:
public class Hello {
public static void main(String [] args){
int number, count = 0, sum = 0;
int Largest= 0, largestEvenNumber = 0;
Scanner console = new Scanner(System.in);
number = console.nextInt(); // read an integer entered by a user
if (number > Largest) { // Condition for computing the largest number
Largest = number;
}
if (number < 0) { // Condition for computing the number of negative integers in the sequence
count = count + 1;
}
if (number % 2 == 0) { // Condition for computing the largest even integer in the sequence
if (largestEvenNumber < number) {
largestEvenNumber = number;
}
}
if (number % 3 == 0) { // Condition for computing the sum of numbers divisible by 3
sum += number;
}
System.out.println("\nThe largest integer is " + Largest);
System.out.println("The number of negative integers in the sequence is " + count);
System.out.println("The largest even integer in the sequence is " + largestEvenNumber);
System.out.printf("The sum of numbers divisible by 3 is %d", sum);
}
}
I would like to get the expected output given below. But, the Scanner class is reading only the first number. How do I correct this without creating multiple objects?
Output:
2
-1
-5
-3
9
8
0
The largest integer is 2
The number of negative integers in the sequence is 0
The largest even integer in the sequence is 2
The sum of numbers divisible by 3 is 0
Process finished with exit code 0
expected Output:
The largest integer is 9
The number of negative integers in the sequence is 3
The largest even integer in the sequence is 8
The sum of numbers divisible by 3 is 6
Thank you!
答案1
得分: 2
你只调用了console.nextInt()
一次,因此只读取了一个数字。如果你想要调用,需要循环调用console.hasNext()
。由于你在使用System.in
。例如:
while (console.hasNextInt()) {
number = console.nextInt();
// 计算
}
英文:
You only call console.nextInt()
once, so only one number is read. If you want to call you need to loop over calls to console.hasNext()
. Since you're using System.in
. E.g.:
while (console.hasNextInt()) {
number = console.nextInt();
// calculations
}
答案2
得分: 1
你只是一次性读取输入。我在你的代码中没有看到循环,所以 number = console.nextInt();
只会运行一次。你应该将其放入一个循环中,当你获得所有数字时退出循环(如何检查可以有多种方式),在循环内部将收到的任何输入放入数组或其他数据结构中。在收集完输入后,对数据结构中的所有数字进行检查。
英文:
You are only reading input once. I don't see a loop in your code, so number = console.nextInt();
only runs once. What you should do is put it inside a loop, exit the loop when you have all the numbers (how you check that can be done in multiple ways), and while you're inside the loop put whatever input you receive into an array or another data structure. After you're done collecting input, do your checks over all the numbers on your data structure.
答案3
得分: 1
<br>
1- 首先,您必须从用户那里接收数据,然后进行计算并生成输出。您可以使用数组,在完成数据输入后,在数组上进行计算。例如:
private static final int DATA_SIZE = 5;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> data = new ArrayList<>();
// 将数据放入数组
while (data.size() < DATA_SIZE){
data.add(scanner.nextInt());
}
// 对数组中的数据进行计算 ...
}
<br>
2- 当您调用 Scanner 类的 nextInt() 字段时,它只会执行一次,然后将其放在循环中,以便多次重复 ...
当然,我还有其他建议可以实现这一点
例如,您可以在 `main` 方法中使用数组(需要知识)
<br> <br> 或者 <br> <br>
首先询问用户要输入的数据量,然后获取数据,然后进行计算
<br> <br> 或者 ...
英文:
<br>
1- You must first receive the data from the user and then calculate it and generate the output.<br> You can do this using the arrays and after finishing put your data, calculate on them.<br>
for example :<br>
private static final int DATA_SIZE = 5;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> data = new ArrayList<>();
// put data in array
while (data.size() == DATA_SIZE){
data.add(scanner.nextInt());
}
// calculate data from array ...
}
<br>
2- When you call a field like nextInt() Scanner class , it is done only once, then you put it in a loop to be repeated several times ...
Of course, I have other suggestions for doing this
For example, you can use the array available in the main
method (with knowledge, of course)
<br> <br> OR <br> <br>
First ask the user for the amount of data you have, then take it and then calculate
<br> <r>
OR....
答案4
得分: 1
如果您想一次性输入所有数字,您应该设定一个终止数字。当您输入完所有数字后,您应该添加终止数字以表示输入结束。
例如:
public static void main(String [] args){
int number, count = 0, sum = 0;
int Largest= 0, largestEvenNumber = 0;
Scanner console = new Scanner(System.in);
int endNumber = -1; // 设置终止数字
do {
number = console.nextInt(); // 读取用户输入的整数
if (number > Largest) { // 计算最大数的条件
Largest = number;
}
if (number < 0) { // 计算序列中负整数的数量的条件
count = count + 1;
}
if (number % 2 == 0) { // 计算序列中最大的偶数的条件
if (largestEvenNumber < number) {
largestEvenNumber = number;
}
}
if (number % 3 == 0) { // 计算能被3整除的数之和的条件
sum += number;
}
}while (number!=endNumber);
System.out.println("最大的整数是 " + Largest);
System.out.println("序列中负整数的数量是 " + count);
System.out.println("序列中最大的偶数是 " + largestEvenNumber);
System.out.printf("能被3整除的数之和为 %d", sum);
}
英文:
If you want to type all number at once ,you should set a terminal number. when you input all you number,you shoud add the terminal number to indicate input is over.
For example:
public static void main(String [] args){
int number, count = 0, sum = 0;
int Largest= 0, largestEvenNumber = 0;
Scanner console = new Scanner(System.in);
int endNumber = -1; //set the terminal number
do {
number = console.nextInt(); // read an integer entered by a user
if (number > Largest) { // Condition for computing the largest number
Largest = number;
}
if (number < 0) { // Condition for computing the number of negative integers in the sequence
count = count + 1;
}
if (number % 2 == 0) { // Condition for computing the largest even integer in the sequence
if (largestEvenNumber < number) {
largestEvenNumber = number;
}
}
if (number % 3 == 0) { // Condition for computing the sum of numbers divisible by 3
sum += number;
}
}while (number!=endNumber);
System.out.println("\nThe largest integer is " + Largest);
System.out.println("The number of negative integers in the sequence is " + count);
System.out.println("The largest even integer in the sequence is " + largestEvenNumber);
System.out.printf("The sum of numbers divisible by 3 is %d", sum);
}
答案5
得分: 0
代码行只被执行一次。因此,扫描器(Scanner)仅接收第一个输入。使用 while 循环来接收多个输入。
英文:
The line if code is only being executed once. Thus, the Scanner is only taking in the first in put. Use a while loop to take in multiple inputs.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论