简体   繁体   English

else if 语句在给定条件时不起作用

[英]Else if statement not functioning when given a condition

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter price in cents:");
        int money = 100;
        int itemPrice = scan.nextInt();
        money = money - itemPrice;
        int quarter = money / 25;
        money = money % 25;
        int dime = money / 10;
        money = money % 10;
        int nickel = money / 5;
        money = money % 5;

        if (itemPrice < 25 || itemPrice > 100) {
            System.out.println("Invalid price!");
            return;
        } else if ((itemPrice % 5 == 1)) {
            System.out.println("Invalid price!");
            return;
        }

        System.out.println("Your change is " + quarter + " quarters, " + dime + " dimes, and " + nickel + " nickels.");
    }
}

This is my code, whenever I try to get the else if statement to run, it doesnt.这是我的代码,每当我尝试让 else if 语句运行时,它都没有。 So if I were to enter 54, it would still make the change and not print out "Invalid Price"!因此,如果我输入 54,它仍然会进行更改并且不会打印出“无效价格”!

如果您想检查itemPrice不能被 5 整除,您需要使用itemPrice%5!=0而不是itemPrice%5==1

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

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