You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java正负奇偶判断程序异常排查及循环实现咨询

Fixing Your Java Number Checker & Implementing Loops

Hey there! Let's tackle your two main issues one by one—first fixing the broken positive odd check, then figuring out the best loop for your program.

1. Fixing the Positive Odd Judgment Issue

Looking at your code, there are three key problems causing the positive odd check to fail:

  • Input reversal bug: You're assigning number = -input.nextInt(); which flips the sign of the user's input. So a positive number becomes negative and vice versa—this breaks all your sign checks right from the start.
  • Mismatched condition-return pairs: Your if/else if blocks have comments that don't match the actual logic. For example, you check for positive even numbers but return the "Negative Even" string, which is completely backwards.
  • Negative modulo behavior: In Java, negative numbers mod 2 return -1 instead of 1. So a condition like number < 0 && number % 2 == 1 will never be true for negative odd numbers.

Here's the fixed version of your code with these issues resolved:

package weekeleven;
import java.util.Scanner;

public class week111 {
    static int number;
    static String pE = "Positive Even";
    static String pO = "Positive Odd";
    static String nE = "Negative Even";
    static String nO = "Negative Odd";
    static String eR = "Error";

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        // Remove the negative sign here to get the actual user input
        number = input.nextInt();
        String result = getSignAndParity();
        System.out.println(result);
    }

    // No need to pass an unused Object parameter
    public static String getSignAndParity() {
        if ((number > 0) && (number % 2 == 0)) {
            return pE; // Positive even
        } else if ((number > 0) && (number % 2 != 0)) {
            return pO; // Positive odd
        } else if ((number < 0) && (number % 2 == 0)) {
            return nE; // Negative even
        } else if ((number < 0) && (number % 2 != 0)) {
            return nO; // Negative odd
        } else {
            return eR; // Handles 0 case
        }
    }
}

I also cleaned up unused parameters and moved string initializations to the declaration for better readability.

2. Implementing Loops: For vs. While

For your program, since you want to repeatedly let users input numbers until they choose to stop (you probably don't know the exact number of iterations upfront), a while loop is the better choice. For loops are ideal when you have a fixed number of iterations (like running 5 times).

Here's how to add a while loop that keeps running until the user enters 0 (you can change this exit condition to whatever you want):

package weekeleven;
import java.util.Scanner;

public class week111 {
    static String pE = "Positive Even";
    static String pO = "Positive Odd";
    static String nE = "Negative Even";
    static String nO = "Negative Odd";
    static String eR = "Error";

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int number;

        System.out.println("Enter a number (enter 0 to exit):");
        while ((number = input.nextInt()) != 0) {
            String result = getSignAndParity(number);
            System.out.println(result);
            System.out.println("Enter another number (enter 0 to exit):");
        }
        System.out.println("Program exited.");
        input.close();
    }

    // Pass number as a parameter instead of using static variable (better practice)
    public static String getSignAndParity(int number) {
        if ((number > 0) && (number % 2 == 0)) {
            return pE;
        } else if ((number > 0) && (number % 2 != 0)) {
            return pO;
        } else if ((number < 0) && (number % 2 == 0)) {
            return nE;
        } else if ((number < 0) && (number % 2 != 0)) {
            return nO;
        } else {
            return eR;
        }
    }
}

This version also avoids using static variables for the input number, which is a cleaner coding practice.

内容的提问来源于stack exchange,提问作者n0rad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:14:05