简体   繁体   English

为什么我的程序在计算偶数时会忽略零?

[英]Why does my program ignore zero when counting even numbers?

I'm having an issue when trying to count the number of even integers. 尝试计算偶数个整数时遇到问题。

This is the code I'm working with: 这是我正在使用的代码:

int input=0, numeven=0;
Scanner scan = new Scanner(System.in);

input = scan.nextInt();

while (input != 0)
{
    //calculates the total number of even integers
    if (input%2 != 1)
    {
        numeven = numeven+1;
    }
}

I don't know how to set up the while loop: while (input! = 0) 我不知道如何设置while循环: while (input! = 0)

Given the test input 6, 4, -2, 0 it says that I have three even numbers, but the expected outcome is 4 (because 0 is even). 给定测试输入6, 4, -2, 0它表示我有三个偶数,但预期结果为4(因为0为偶数)。

If you want your loop to work on zero, and treat it as the exit mark too, switch from while to do / while : 如果您希望循环从零开始工作,并且也将其视为退出标记,请从while切换为do / while

do {
    input = scan.nextInt();
    //calculates the total number of even integers
    if (input%2 != 1)
    {
        numeven = numeven+1;
    }
} while (input != 0);

This way your code will process zero along with regular inputs, and stop reading further input upon reaching the end of the loop. 这样,您的代码将与常规输入一起处理零,并在循环结束时停止读取其他输入。

You don't want the loop to break when the user enters a 0 or any other integer incase you want to put 0 multiple times. 您不希望在用户输入0或任何其他整数的情况下中断循环,以防您想多次输入0。

int numeven=0;
Scanner scan = new Scanner(System.in);

while (true) {
    String input = scan.next();
    try {
        int val = Integer.parseInt(input);
        if (val % 2 == 0)
            numeven++;

    } catch (NumberFormatException e) {
        //enter any input besides an integer and it will break the loop
        break;
    }
}

System.out.println("Total even numbers: " + numeven);

Alternatively this does the same thing. 或者,这也做同样的事情。 Except it won't consume the last value. 除非它不会消耗最后的值。

int numeven=0;
Scanner scan = new Scanner(System.in);

while (scan.hasNextInt()) {
    int val = scan.nextInt();
    if (val % 2 == 0)
        numeven++;
}

System.out.println("Total even numbers: " + numeven);

Just make the condition of your while loop to be 只要使您的while循环的条件为

while( scan.hasNextInt() )

Then it will only loop as long as there are numbers. 然后,只有存在数字时,它才会循环播放。 Inside the loop you can 在循环内您可以

input = scan.nextInt()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 Java 程序在另一个 html 中忽略我的 List? - Why does my Java program ignore my List in another html? 为什么我的程序在循环时不打印偶数? - Why is my program not printing even numbers do while loop? 当我尝试添加ArrayList元素时,即使它是新的,为什么我的程序仍然存在? - Why does my program say the ArrayList element exists when I try to add it even though it's new? 当我运行我的程序来计算所有偶数斐波那契数的总和时,为什么会得到一个负数 output? - Why do I get a negative output when I run my program to compute the sum of all even Fibonacci numbers? 程序计数 0 为偶数 - Program counting 0 as even 为什么即使我编写的代码不会产生零或重复的数字,我的程序的输出也总是以两个连续的零开头? - Why does the output for my program always start with two consecutive zeros even though I wrote a code that doesn't produce zeros or repeated numbers? 为什么我的程序从 IntelliJ 中的文本文件中读取时不计算单词 hello - Why is my program not counting word hello when it reads from a text file in IntelliJ 计算偶数和奇数 - Counting amount of even and odd numbers 为什么我的程序将数组中的数字打印为零,但仍然打印出数组中的最大值? - why does my program print the numbers in my array as zeros but still print the largest value out of the array? 为什么我的程序声音在eclipse中起作用,但在jar中生成却不能起作用 - Why does the sound of my program work in eclipse but not when generated in a jar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM