If statement always giving the same answer

前端 未结 7 534
-上瘾入骨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:12

    You need to change these two statements

    if (enoughTreats = true) 
    
    else if (enoughTreats = false)
    

    into

    if (enoughTreats == true) 
    
    else if (enoughTreats == false)
    

    You could also shorten the code and get the exact same effect by simply typing this below:

    if (enoughTreats)
    
    else
    

    If you put a boolean variable inside of the parenthesis of an if statement by itself, the if statement will check to see if it's true, thus you wouldn't need to type '== true.' You could also replace the else if statement in your code with a simple 'else' because if the enoughTreats variable is not equal to true, the else code will automatically execute without you having to specifically state a condition.

提交回复
热议问题