If statement always giving the same answer

前端 未结 7 518
-上瘾入骨i
-上瘾入骨i 2020-12-12 03:26
import java.util.Scanner;

class Practice {

public static void main(String args[]) {

    System.out.println(\"Enter the number of treats you have:\");
    Scanner          


        
7条回答
  •  既然无缘
    2020-12-12 04:18

    You should use == instead of =

        if (enoughTreats == true) {
            System.out.println("There are enough treats for all the hamsters!");
        }
        else {
            System.out.println("Oh no! There aren't enough treats!");        
        }
    

    Remember that == is the comparison operator and = is the assignment operator.

    And as Mike mentioned, just having if(enoughTreats) will do the trick for you. No need to use == operator!

    As a matter of fact, you don't need the boolean variable enoughTreats at all. You could just write your condition like so:

    if (treatsPerHamster >= neededTreats) {
        // do one thing
    }
    else {
        // do other
    }
    

提交回复
热议问题