Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在if循环中排除负数?_Java_Loops_If Statement - Fatal编程技术网

Java 如何在if循环中排除负数?

Java 如何在if循环中排除负数?,java,loops,if-statement,Java,Loops,If Statement,对于我的项目,用户输入一组数字,然后执行一些计算以给出所需的输出。计算方法之一是查找输入的奇数之和。查看给定的测试用例,我注意到我的代码添加了负数奇数,而正确的测试用例没有。有什么简单的解决办法吗 谢谢 这是我写的一些代码,减去一些部分 import java.util.Scanner; public class Test { public static void main (String[] args) { int min_interger = 0; int sum_of_

对于我的项目,用户输入一组数字,然后执行一些计算以给出所需的输出。计算方法之一是查找输入的奇数之和。查看给定的测试用例,我注意到我的代码添加了负数奇数,而正确的测试用例没有。有什么简单的解决办法吗

谢谢

这是我写的一些代码,减去一些部分

import java.util.Scanner;

public class Test 
{
public static void main (String[] args)
{
    int min_interger = 0;
    int sum_of_pos = 0;
    int sum_of_odd = 0;
    int count_of_pos = 0;
    int userInput;

    Scanner consoleInput = new Scanner(System.in);

    do {userInput = consoleInput.nextInt();


    if(userInput % 2 != 0)
    {
        sum_of_odd = sum_of_odd + userInput;
    }

    while (userInput != 0);

    System.out.print("The sum of the odd integers is " + sum_of_odd + "\n")
}

}

确保添加奇数和大于零的数字

if(userInput % 2 != 0 && userInput > 0)
    {
        sum_of_odd = sum_of_odd + userInput;
    }
谢谢:)我以前试过,但忘了添加第二个userInput。非常感谢!