简体   繁体   中英

Keep getting error with If statement in java

I am taking a java course in college and one of the homework assignments is to write a program that reads an integer between 0 and 100[inclusive], representing the amount of purchase. The issue I am having is the if-else statement. Every time I compile, it would say the less than equal sign is an illegal start of type. Can someone explain to me why that is and how I can fix it? Thanks in advance!

    import java.util.Scanner;
    
    public class RefundAmount {
    
       public static void main(String [] args) {
        Scanner scan = new Scanner (System.in);
    
        int change;
        int dollars, quarters, dimes, nickels, pennies;
        
        System.out.println("Enter the purchase price [0-100]: ");
              int price = scan.nextInt();
        
        System.out.println("Enter the amount paid: ");
              int paid = scan.nextInt();
            
        
        change = price - paid;
    
        dollars = (change / 100);
        change = change % 100;
        quarters = (change / 25);
        change = change % 25;
        dimes = (change / 10);
        change = change % 10;
        nickels = (change / 5);
        change = change % 5;
        pennies =  change;
        
    
    
        if (change >= 0 || <= 100){
    
    
        
        System.out.print("Your change of " + purchase + " cents is given as:");
     }else
        System.out.println("Purchase must between 0 and 100 cents.");
        
    
    
        System.out.println(" " + dollars + " dollars");
        System.out.println(" " + quarters + " Quarters");
        System.out.println(" " + dimes + " Dimes");
        System.out.println(" " + nickels + " Nickels");
        System.out.println(" " + pennies + " Pennies");
    
    
     }
    }

There are some things wrong with your code.

if (change >= 0 || <= 100)

Should be

if (change >= 0 || change <= 100)

And that's why you get said error.

Besides that, when you're using || (or) the both conditions will be verified, but you really don't need that. Instead with would be better to use && (and). With and , if the first condition fails, the next won't be verified.

Another thing nothing is that purchase is never referenced in your code besides the print statement.

PS: Don't know if what IDE or text editor you're using, but I would advise Visual Studio Code or IntelliJ.

If you want to find numbers between code should be like;

if(change >= 0 && change <= 100){

}

|| is or statement you should use && to specify numbers larger than 0 and smaller than 100.

Each time you use an or operator you have to include the variable. And that's why it says you can't start with a <= sign. Hope this helps. =)

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