简体   繁体   中英

Java. Output errors

package assignmentone;

import java.util.Scanner;

public class AssignmentOne {

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

        System.out.println("Welcome to the University Credit Checker");
        System.out.println("Enter Pass Credits");

        if (in.hasNextInt()) {
            int passCredits = in.nextInt(); //Correct

            if (passCredits != 0 || passCredits != 20 || passCredits != 40
                    || passCredits != 60 || passCredits != 80 || passCredits != 100 || passCredits != 120)
            {
                System.out.println("Error: Enter Valid Input Type: 0, 20, 40, 60, 80, 100 or 120");
            } // Correct

            else if (passCredits == 0 || passCredits == 20 || passCredits == 40 || passCredits == 60
                    || passCredits == 80 || passCredits == 100)
            {
                System.out.println("Enter Defer Credits");
            }
           // Get rid of output Error: Enter Valid Input Type: 0, 20, 40, 60, 80, 100 or 120

            else if (passCredits == 120){
                System.out.println("Progress");
            }
            // Get rid of output Error: Enter Valid Input Type: 0, 20, 40, 60, 80, 100 or 120

        } else {
            System.out.println("Error: Enter Valid Input Type: Integer");
        } //Correct

    }
}

Hello all.

I am new to Java and am trying to create a program that uses user input, alongside user validation. I have created the first part.

For this part I have a user validation to make sure they enter an integer type. Which is working correctly

After this, I have a user validation to make sure they enter 0, 20, 40, 60, 80, 100 and 120. This will come up with an error. This too also works correctly.

What is not working correctly is when they enter a number that is 0, 20, 40, 60, 80 or 100, I wish for a simple output of "Enter Defer Credits" to display. With this, I want it when the user enters 120 a simple output of "Progress" displays.

With both parts, "Error: Enter Valid Input Type: 0, 20, 40, 60, 80, 100 or 120" displays instead of the intended "Enter Defer Credits" or "Progress".

Looking at my code above, I was wondering why this would be? As at the moment I am completely stuck to why this is the case.

Thanks in advance. :)

As explain by Faithiun answer, you have an error in you logical operators.

But if you use some mathematic logic, you can reduce the number of boolean expression reducing the risk of mistakes.

Your condition checks for the first multiple of 20. Using a % 20 , you can get a simpler soltuion

if( passCredits % 20 == 0){ //Is it a multiple
    int multPass = passCredits / 20; 
        //divide by 20 to the position of that multiple (
        //0 = 0
        //20 = 1
        //40 = 2
        //...
    if( multiPass >= 0 && muttipass <= 6){
        // Enter Defer Credits
    } else { 
        //Progress
    }
} else {
    //"Error: Enter Valid Input Type: 0, 20, 40, 60, 80, 100 or 120"
}

replace

if (passCredits != 0 || passCredits != 20 || passCredits != 40
                || passCredits != 60 || passCredits != 80 || passCredits != 100 || passCredits != 120)
        {
            System.out.println("Error: Enter Valid Input Type: 0, 20, 40, 60, 80, 100 or 120");
        }

By

if (passCredits != 0 && passCredits != 20 && passCredits != 40
                && passCredits != 60 && passCredits != 80 && passCredits != 100 && passCredits != 120)
        {
            System.out.println("Error: Enter Valid Input Type: 0, 20, 40, 60, 80, 100 or 120");
        } // Correct

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM